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) |
| 44 | * special variables (done: PWD, PPID, RANDOM) |
| 45 | * tilde expansion |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 46 | * aliases |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 47 | * follow IFS rules more precisely, including update semantics |
| 48 | * builtins mandated by standards we don't support: |
| 49 | * [un]alias, command, fc, getopts, newgrp, readonly, times |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 50 | * make complex ${var%...} constructs support optional |
| 51 | * make here documents optional |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 52 | * |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 53 | * Bash compat TODO: |
| 54 | * redirection of stdout+stderr: &> and >& |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 55 | * reserved words: function select |
| 56 | * advanced test: [[ ]] |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 57 | * process substitution: <(list) and >(list) |
| 58 | * =~: regex operator |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 59 | * let EXPR [EXPR...] |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 60 | * Each EXPR is an arithmetic expression (ARITHMETIC EVALUATION) |
| 61 | * If the last arg evaluates to 0, let returns 1; 0 otherwise. |
| 62 | * 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] | 63 | * ((EXPR)) |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 64 | * The EXPR is evaluated according to ARITHMETIC EVALUATION. |
| 65 | * This is exactly equivalent to let "EXPR". |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 66 | * $[EXPR]: synonym for $((EXPR)) |
Denys Vlasenko | bbecd74 | 2010-10-03 17:22:52 +0200 | [diff] [blame] | 67 | * |
| 68 | * Won't do: |
| 69 | * In bash, export builtin is special, its arguments are assignments |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 70 | * and therefore expansion of them should be "one-word" expansion: |
| 71 | * $ export i=`echo 'a b'` # export has one arg: "i=a b" |
| 72 | * compare with: |
| 73 | * $ ls i=`echo 'a b'` # ls has two args: "i=a" and "b" |
| 74 | * ls: cannot access i=a: No such file or directory |
| 75 | * ls: cannot access b: No such file or directory |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 76 | * Note1: same applies to local builtin. |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 77 | * Note2: bash 3.2.33(1) does this only if export word itself |
| 78 | * is not quoted: |
| 79 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 80 | * aaa bbb |
| 81 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 82 | * aaa |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 83 | */ |
Denys Vlasenko | 8da415e | 2010-12-05 01:30:14 +0100 | [diff] [blame] | 84 | #if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ |
| 85 | || defined(__APPLE__) \ |
| 86 | ) |
| 87 | # include <malloc.h> /* for malloc_trim */ |
| 88 | #endif |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 89 | #include <glob.h> |
| 90 | /* #include <dmalloc.h> */ |
| 91 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 92 | # include <fnmatch.h> |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 93 | #endif |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 94 | #include <sys/utsname.h> /* for setting $HOSTNAME */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 95 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 96 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
| 97 | #include "unicode.h" |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 98 | #include "shell_common.h" |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 99 | #include "math.h" |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 100 | #include "match.h" |
Denys Vlasenko | cbe0b7f | 2009-10-09 22:00:58 +0200 | [diff] [blame] | 101 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 102 | # include "random.h" |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 103 | #else |
| 104 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 105 | #endif |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 106 | #ifndef F_DUPFD_CLOEXEC |
| 107 | # define F_DUPFD_CLOEXEC F_DUPFD |
| 108 | #endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 109 | #ifndef PIPE_BUF |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 110 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 111 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 112 | |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 113 | //config:config HUSH |
| 114 | //config: bool "hush" |
| 115 | //config: default y |
| 116 | //config: help |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 117 | //config: hush is a small shell (25k). It handles the normal flow control |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 118 | //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, |
| 119 | //config: case/esac. Redirections, here documents, $((arithmetic)) |
| 120 | //config: and functions are supported. |
| 121 | //config: |
| 122 | //config: It will compile and work on no-mmu systems. |
| 123 | //config: |
Denys Vlasenko | e2069fb | 2010-10-04 00:01:47 +0200 | [diff] [blame] | 124 | //config: It does not handle select, aliases, tilde expansion, |
| 125 | //config: &>file and >&file redirection of stdout+stderr. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 126 | //config: |
| 127 | //config:config HUSH_BASH_COMPAT |
| 128 | //config: bool "bash-compatible extensions" |
| 129 | //config: default y |
| 130 | //config: depends on HUSH |
| 131 | //config: help |
| 132 | //config: Enable bash-compatible extensions. |
| 133 | //config: |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 134 | //config:config HUSH_BRACE_EXPANSION |
| 135 | //config: bool "Brace expansion" |
| 136 | //config: default y |
| 137 | //config: depends on HUSH_BASH_COMPAT |
| 138 | //config: help |
| 139 | //config: Enable {abc,def} extension. |
| 140 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 141 | //config:config HUSH_HELP |
| 142 | //config: bool "help builtin" |
| 143 | //config: default y |
| 144 | //config: depends on HUSH |
| 145 | //config: help |
| 146 | //config: Enable help builtin in hush. Code size + ~1 kbyte. |
| 147 | //config: |
| 148 | //config:config HUSH_INTERACTIVE |
| 149 | //config: bool "Interactive mode" |
| 150 | //config: default y |
| 151 | //config: depends on HUSH |
| 152 | //config: help |
| 153 | //config: Enable interactive mode (prompt and command editing). |
| 154 | //config: Without this, hush simply reads and executes commands |
| 155 | //config: from stdin just like a shell script from a file. |
| 156 | //config: No prompt, no PS1/PS2 magic shell variables. |
| 157 | //config: |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 158 | //config:config HUSH_SAVEHISTORY |
| 159 | //config: bool "Save command history to .hush_history" |
| 160 | //config: default y |
| 161 | //config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY |
| 162 | //config: help |
| 163 | //config: Enable history saving in hush. |
| 164 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 165 | //config:config HUSH_JOB |
| 166 | //config: bool "Job control" |
| 167 | //config: default y |
| 168 | //config: depends on HUSH_INTERACTIVE |
| 169 | //config: help |
| 170 | //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current |
| 171 | //config: command (not entire shell), fg/bg builtins work. Without this option, |
| 172 | //config: "cmd &" still works by simply spawning a process and immediately |
| 173 | //config: prompting for next command (or executing next command in a script), |
| 174 | //config: but no separate process group is formed. |
| 175 | //config: |
| 176 | //config:config HUSH_TICK |
| 177 | //config: bool "Process substitution" |
| 178 | //config: default y |
| 179 | //config: depends on HUSH |
| 180 | //config: help |
| 181 | //config: Enable process substitution `command` and $(command) in hush. |
| 182 | //config: |
| 183 | //config:config HUSH_IF |
| 184 | //config: bool "Support if/then/elif/else/fi" |
| 185 | //config: default y |
| 186 | //config: depends on HUSH |
| 187 | //config: help |
| 188 | //config: Enable if/then/elif/else/fi in hush. |
| 189 | //config: |
| 190 | //config:config HUSH_LOOPS |
| 191 | //config: bool "Support for, while and until loops" |
| 192 | //config: default y |
| 193 | //config: depends on HUSH |
| 194 | //config: help |
| 195 | //config: Enable for, while and until loops in hush. |
| 196 | //config: |
| 197 | //config:config HUSH_CASE |
| 198 | //config: bool "Support case ... esac statement" |
| 199 | //config: default y |
| 200 | //config: depends on HUSH |
| 201 | //config: help |
| 202 | //config: Enable case ... esac statement in hush. +400 bytes. |
| 203 | //config: |
| 204 | //config:config HUSH_FUNCTIONS |
| 205 | //config: bool "Support funcname() { commands; } syntax" |
| 206 | //config: default y |
| 207 | //config: depends on HUSH |
| 208 | //config: help |
| 209 | //config: Enable support for shell functions in hush. +800 bytes. |
| 210 | //config: |
| 211 | //config:config HUSH_LOCAL |
| 212 | //config: bool "Support local builtin" |
| 213 | //config: default y |
| 214 | //config: depends on HUSH_FUNCTIONS |
| 215 | //config: help |
| 216 | //config: Enable support for local variables in functions. |
| 217 | //config: |
| 218 | //config:config HUSH_RANDOM_SUPPORT |
| 219 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 220 | //config: default y |
| 221 | //config: depends on HUSH |
| 222 | //config: help |
| 223 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 224 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
| 225 | //config: |
| 226 | //config:config HUSH_EXPORT_N |
| 227 | //config: bool "Support 'export -n' option" |
| 228 | //config: default y |
| 229 | //config: depends on HUSH |
| 230 | //config: help |
| 231 | //config: export -n unexports variables. It is a bash extension. |
| 232 | //config: |
| 233 | //config:config HUSH_MODE_X |
| 234 | //config: bool "Support 'hush -x' option and 'set -x' command" |
| 235 | //config: default y |
| 236 | //config: depends on HUSH |
| 237 | //config: help |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 238 | //config: This instructs hush to print commands before execution. |
| 239 | //config: Adds ~300 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 240 | //config: |
Denys Vlasenko | 6adf2aa | 2010-07-16 19:26:38 +0200 | [diff] [blame] | 241 | //config:config MSH |
| 242 | //config: bool "msh (deprecated: aliased to hush)" |
| 243 | //config: default n |
| 244 | //config: select HUSH |
| 245 | //config: help |
| 246 | //config: msh is deprecated and will be removed, please migrate to hush. |
| 247 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 248 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 249 | //applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP)) |
| 250 | //applet:IF_MSH(APPLET(msh, BB_DIR_BIN, BB_SUID_DROP)) |
| 251 | //applet:IF_FEATURE_SH_IS_HUSH(APPLET_ODDNAME(sh, hush, BB_DIR_BIN, BB_SUID_DROP, sh)) |
| 252 | //applet:IF_FEATURE_BASH_IS_HUSH(APPLET_ODDNAME(bash, hush, BB_DIR_BIN, BB_SUID_DROP, bash)) |
| 253 | |
| 254 | //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o |
| 255 | //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o |
| 256 | |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 257 | /* -i (interactive) and -s (read stdin) are also accepted, |
| 258 | * but currently do nothing, therefore aren't shown in help. |
| 259 | * NOMMU-specific options are not meant to be used by users, |
| 260 | * therefore we don't show them either. |
| 261 | */ |
| 262 | //usage:#define hush_trivial_usage |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 263 | //usage: "[-nxl] [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS]]" |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 264 | //usage:#define hush_full_usage "\n\n" |
| 265 | //usage: "Unix shell interpreter" |
| 266 | |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 267 | //usage:#define msh_trivial_usage hush_trivial_usage |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 268 | //usage:#define msh_full_usage hush_full_usage |
| 269 | |
| 270 | //usage:#if ENABLE_FEATURE_SH_IS_HUSH |
| 271 | //usage:# define sh_trivial_usage hush_trivial_usage |
| 272 | //usage:# define sh_full_usage hush_full_usage |
| 273 | //usage:#endif |
| 274 | //usage:#if ENABLE_FEATURE_BASH_IS_HUSH |
| 275 | //usage:# define bash_trivial_usage hush_trivial_usage |
| 276 | //usage:# define bash_full_usage hush_full_usage |
| 277 | //usage:#endif |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 278 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 279 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 280 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 281 | #define LEAK_HUNTING 0 |
| 282 | #define BUILD_AS_NOMMU 0 |
| 283 | /* Enable/disable sanity checks. Ok to enable in production, |
| 284 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 285 | * Keeping 1 for now even in released versions. |
| 286 | */ |
| 287 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 288 | /* Slightly bigger (+200 bytes), but faster hush. |
| 289 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 290 | * which allows us to do fewer waitpid's. |
| 291 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 292 | * and therefore waitpid will return the same result as last time) |
| 293 | */ |
| 294 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 295 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 296 | * So far ${var%...} ops are always enabled: |
| 297 | */ |
| 298 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 299 | |
| 300 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 301 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 302 | # undef BB_MMU |
| 303 | # undef USE_FOR_NOMMU |
| 304 | # undef USE_FOR_MMU |
| 305 | # define BB_MMU 0 |
| 306 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 307 | # define USE_FOR_MMU(...) |
| 308 | #endif |
| 309 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 310 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 311 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 312 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 313 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 314 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 315 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 316 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 317 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 318 | # define IF_FEATURE_SH_STANDALONE(...) |
| 319 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 320 | #endif |
| 321 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 322 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 323 | # undef ENABLE_FEATURE_EDITING |
| 324 | # define ENABLE_FEATURE_EDITING 0 |
| 325 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 326 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denys Vlasenko | 8cab667 | 2012-04-20 14:48:00 +0200 | [diff] [blame] | 327 | # undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 328 | # define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 329 | #endif |
| 330 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 331 | /* Do we support ANY keywords? */ |
| 332 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 333 | # define HAS_KEYWORDS 1 |
| 334 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 335 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 336 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 337 | # define HAS_KEYWORDS 0 |
| 338 | # define IF_HAS_KEYWORDS(...) |
| 339 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 340 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 341 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 342 | /* If you comment out one of these below, it will be #defined later |
| 343 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 344 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 345 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 346 | #define debug_printf_parse(...) do {} while (0) |
| 347 | #define debug_print_tree(a, b) do {} while (0) |
| 348 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 349 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 350 | #define debug_printf_jobs(...) do {} while (0) |
| 351 | #define debug_printf_expand(...) do {} while (0) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 352 | #define debug_printf_varexp(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 353 | #define debug_printf_glob(...) do {} while (0) |
| 354 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 355 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 356 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 357 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 358 | #define ERR_PTR ((void*)(long)1) |
| 359 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 360 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 361 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 362 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 363 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 364 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 365 | #if ENABLE_HUSH_BASH_COMPAT |
| 366 | /* Support / and // replace ops */ |
| 367 | /* Note that // is stored as \ in "encoded" string representation */ |
| 368 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 369 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 370 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 371 | #else |
| 372 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 373 | # define VAR_SUBST_OPS "%#:-=+?" |
| 374 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 375 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 376 | |
| 377 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 378 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 379 | struct variable; |
| 380 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 381 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 382 | |
| 383 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 384 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 385 | */ |
| 386 | #if !BB_MMU |
| 387 | typedef struct nommu_save_t { |
| 388 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 389 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 390 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 391 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 392 | } nommu_save_t; |
| 393 | #endif |
| 394 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 395 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 396 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 397 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 398 | RES_IF , |
| 399 | RES_THEN , |
| 400 | RES_ELIF , |
| 401 | RES_ELSE , |
| 402 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 403 | #endif |
| 404 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 405 | RES_FOR , |
| 406 | RES_WHILE , |
| 407 | RES_UNTIL , |
| 408 | RES_DO , |
| 409 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 410 | #endif |
| 411 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 412 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 413 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 414 | #if ENABLE_HUSH_CASE |
| 415 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 416 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 417 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 418 | RES_MATCH , /* "word)" */ |
| 419 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 420 | RES_ESAC , |
| 421 | #endif |
| 422 | RES_XXXX , |
| 423 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 424 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 425 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 426 | typedef struct o_string { |
| 427 | char *data; |
| 428 | int length; /* position where data is appended */ |
| 429 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 430 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 431 | /* At least some part of the string was inside '' or "", |
| 432 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 433 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 434 | smallint has_empty_slot; |
| 435 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 436 | } o_string; |
| 437 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 438 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 439 | EXP_FLAG_GLOB = 0x2, |
| 440 | /* Protect newly added chars against globbing |
| 441 | * by prepending \ to *, ?, [, \ */ |
| 442 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 443 | }; |
| 444 | enum { |
| 445 | MAYBE_ASSIGNMENT = 0, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 446 | DEFINITELY_ASSIGNMENT = 1, |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 447 | NOT_ASSIGNMENT = 2, |
Maninder Singh | 97c6491 | 2015-05-25 13:46:36 +0200 | [diff] [blame] | 448 | /* Not an assignment, but next word may be: "if v=xyz cmd;" */ |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 449 | WORD_IS_KEYWORD = 3, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 450 | }; |
| 451 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 452 | #define NULL_O_STRING { NULL } |
| 453 | |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 454 | #ifndef debug_printf_parse |
| 455 | static const char *const assignment_flag[] = { |
| 456 | "MAYBE_ASSIGNMENT", |
| 457 | "DEFINITELY_ASSIGNMENT", |
| 458 | "NOT_ASSIGNMENT", |
| 459 | "WORD_IS_KEYWORD", |
| 460 | }; |
| 461 | #endif |
| 462 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 463 | typedef struct in_str { |
| 464 | const char *p; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 465 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 466 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 467 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 468 | int peek_buf[2]; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 469 | int last_char; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 470 | FILE *file; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 471 | int (*get) (struct in_str *) FAST_FUNC; |
| 472 | int (*peek) (struct in_str *) FAST_FUNC; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 473 | } in_str; |
| 474 | #define i_getch(input) ((input)->get(input)) |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 475 | #define i_peek(input) ((input)->peek(input)) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 476 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 477 | /* The descrip member of this structure is only used to make |
| 478 | * debugging output pretty */ |
| 479 | static const struct { |
| 480 | int mode; |
| 481 | signed char default_fd; |
| 482 | char descrip[3]; |
| 483 | } redir_table[] = { |
| 484 | { O_RDONLY, 0, "<" }, |
| 485 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 486 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 487 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 488 | { O_RDONLY, 0, "<<" }, |
| 489 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 490 | /* { O_RDONLY, 77, "<<" }, */ |
| 491 | }; |
| 492 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 493 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 494 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 495 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 496 | int rd_fd; /* fd to redirect */ |
| 497 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 498 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 499 | smallint rd_type; /* (enum redir_type) */ |
| 500 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 501 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 502 | * bit 0: do we need to trim leading tabs? |
| 503 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 504 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 505 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 506 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 507 | REDIRECT_INPUT = 0, |
| 508 | REDIRECT_OVERWRITE = 1, |
| 509 | REDIRECT_APPEND = 2, |
| 510 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 511 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 512 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 513 | |
| 514 | REDIRFD_CLOSE = -3, |
| 515 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 516 | REDIRFD_TO_FILE = -1, |
| 517 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 518 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 519 | HEREDOC_SKIPTABS = 1, |
| 520 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 521 | } redir_type; |
| 522 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 523 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 524 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 525 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 526 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 527 | smallint cmd_type; /* CMD_xxx */ |
| 528 | #define CMD_NORMAL 0 |
| 529 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 530 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | d383b49 | 2010-09-06 10:22:13 +0200 | [diff] [blame] | 531 | /* used for "[[ EXPR ]]" */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 532 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 533 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 534 | #if ENABLE_HUSH_FUNCTIONS |
| 535 | # define CMD_FUNCDEF 3 |
| 536 | #endif |
| 537 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 538 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 539 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 540 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 541 | #if !BB_MMU |
| 542 | char *group_as_string; |
| 543 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 544 | #if ENABLE_HUSH_FUNCTIONS |
| 545 | struct function *child_func; |
| 546 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 547 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 548 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 549 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 550 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 551 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 552 | * we put those fields back into cmd->xxx |
| 553 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 554 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 555 | * Without this trick, loop would execute a;b;b;b;... |
| 556 | * instead of correct sequence a;b;a;b;... |
| 557 | * When command is freed, it severs the link |
| 558 | * (sets ->child_func->parent_cmd to NULL). |
| 559 | */ |
| 560 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 561 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 562 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 563 | * and on execution these are substituted with their values. |
| 564 | * Substitution can make _several_ words out of one argv[n]! |
| 565 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 566 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 567 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 568 | struct redir_struct *redirects; /* I/O redirections */ |
| 569 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 570 | /* Is there anything in this command at all? */ |
| 571 | #define IS_NULL_CMD(cmd) \ |
| 572 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 573 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 574 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 575 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 576 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 577 | int alive_cmds; /* number of commands running (not exited) */ |
| 578 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 579 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 580 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 581 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 582 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 583 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 584 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 585 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 586 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 587 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 588 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 589 | typedef enum pipe_style { |
| 590 | PIPE_SEQ = 1, |
| 591 | PIPE_AND = 2, |
| 592 | PIPE_OR = 3, |
| 593 | PIPE_BG = 4, |
| 594 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 595 | /* Is there anything in this pipe at all? */ |
| 596 | #define IS_NULL_PIPE(pi) \ |
| 597 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 598 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 599 | /* This holds pointers to the various results of parsing */ |
| 600 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 601 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 602 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 603 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 604 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 605 | /* last command in pipe (being constructed right now) */ |
| 606 | struct command *command; |
| 607 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 608 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 609 | #if !BB_MMU |
| 610 | o_string as_string; |
| 611 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 612 | #if HAS_KEYWORDS |
| 613 | smallint ctx_res_w; |
| 614 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 615 | #if ENABLE_HUSH_CASE |
| 616 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 617 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 618 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 619 | int old_flag; |
| 620 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 621 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 622 | * when we see "if" or "then", we malloc and copy current context, |
| 623 | * and make ->stack point to it. then we parse pipeN. |
| 624 | * when closing "then" / fi" / whatever is found, |
| 625 | * we move list_head into ->stack->command->group, |
| 626 | * copy ->stack into current context, and delete ->stack. |
| 627 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 628 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 629 | struct parse_context *stack; |
| 630 | #endif |
| 631 | }; |
| 632 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 633 | /* On program start, environ points to initial environment. |
| 634 | * putenv adds new pointers into it, unsetenv removes them. |
| 635 | * Neither of these (de)allocates the strings. |
| 636 | * setenv allocates new strings in malloc space and does putenv, |
| 637 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 638 | #define setenv(...) setenv_is_leaky_dont_use() |
| 639 | struct variable { |
| 640 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 641 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 642 | #if ENABLE_HUSH_LOCAL |
| 643 | unsigned func_nest_level; |
| 644 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 645 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 646 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 647 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 648 | }; |
| 649 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 650 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 651 | BC_BREAK = 1, |
| 652 | BC_CONTINUE = 2, |
| 653 | }; |
| 654 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 655 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 656 | struct function { |
| 657 | struct function *next; |
| 658 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 659 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 660 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 661 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 662 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 663 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 664 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 665 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 666 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 667 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 668 | /* set -/+o OPT support. (TODO: make it optional) |
| 669 | * bash supports the following opts: |
| 670 | * allexport off |
| 671 | * braceexpand on |
| 672 | * emacs on |
| 673 | * errexit off |
| 674 | * errtrace off |
| 675 | * functrace off |
| 676 | * hashall on |
| 677 | * histexpand off |
| 678 | * history on |
| 679 | * ignoreeof off |
| 680 | * interactive-comments on |
| 681 | * keyword off |
| 682 | * monitor on |
| 683 | * noclobber off |
| 684 | * noexec off |
| 685 | * noglob off |
| 686 | * nolog off |
| 687 | * notify off |
| 688 | * nounset off |
| 689 | * onecmd off |
| 690 | * physical off |
| 691 | * pipefail off |
| 692 | * posix off |
| 693 | * privileged off |
| 694 | * verbose off |
| 695 | * vi off |
| 696 | * xtrace off |
| 697 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 698 | static const char o_opt_strings[] ALIGN1 = |
| 699 | "pipefail\0" |
| 700 | "noexec\0" |
| 701 | #if ENABLE_HUSH_MODE_X |
| 702 | "xtrace\0" |
| 703 | #endif |
| 704 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 705 | enum { |
| 706 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 707 | OPT_O_NOEXEC, |
| 708 | #if ENABLE_HUSH_MODE_X |
| 709 | OPT_O_XTRACE, |
| 710 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 711 | NUM_OPT_O |
| 712 | }; |
| 713 | |
| 714 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 715 | struct FILE_list { |
| 716 | struct FILE_list *next; |
| 717 | FILE *fp; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 718 | int fd; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 719 | }; |
| 720 | |
| 721 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 722 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 723 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 724 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 725 | /* interactive_fd != 0 means we are an interactive shell. |
| 726 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 727 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 728 | * job control still works, but terminal signals |
| 729 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 730 | * process groups can only be created with "cmd &". |
| 731 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 732 | * to give tty to the foreground process group, |
| 733 | * and will take it back when the group is stopped (^Z) |
| 734 | * or killed (^C). |
| 735 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 736 | #if ENABLE_HUSH_INTERACTIVE |
| 737 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 738 | * _AND_ if we decided to act interactively */ |
| 739 | int interactive_fd; |
| 740 | const char *PS1; |
| 741 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 742 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 743 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 744 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 745 | #endif |
| 746 | #if ENABLE_FEATURE_EDITING |
| 747 | line_input_t *line_input_state; |
| 748 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 749 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 750 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 751 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 752 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 753 | random_t random_gen; |
| 754 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 755 | #if ENABLE_HUSH_JOB |
| 756 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 757 | int last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 758 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 759 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 760 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 761 | #else |
| 762 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 763 | #endif |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 764 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 765 | #if ENABLE_HUSH_MODE_X |
| 766 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 767 | #else |
| 768 | # define G_x_mode 0 |
| 769 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 770 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 771 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 772 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 773 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 774 | #if ENABLE_HUSH_FUNCTIONS |
| 775 | /* 0: outside of a function (or sourced file) |
| 776 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 777 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 778 | */ |
| 779 | smallint flag_return_in_progress; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 780 | # define G_flag_return_in_progress (G.flag_return_in_progress) |
| 781 | #else |
| 782 | # define G_flag_return_in_progress 0 |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 783 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 784 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 785 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 786 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 787 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 788 | smalluint global_args_malloced; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 789 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 790 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 791 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 792 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 793 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 794 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 795 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 796 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 797 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 798 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 799 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 800 | const char *cwd; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 801 | struct variable *top_var; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 802 | char **expanded_assignments; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 803 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 804 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 805 | # if ENABLE_HUSH_LOCAL |
| 806 | struct variable **shadowed_vars_pp; |
| 807 | unsigned func_nest_level; |
| 808 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 809 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 810 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 811 | #if ENABLE_HUSH_FAST |
| 812 | unsigned count_SIGCHLD; |
| 813 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 814 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 815 | #endif |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 816 | struct FILE_list *FILE_list; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 817 | /* Which signals have non-DFL handler (even with no traps set)? |
| 818 | * Set at the start to: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 819 | * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS) |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 820 | * SPECIAL_INTERACTIVE_SIGS are cleared after fork. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 821 | * The rest is cleared right before execv syscalls. |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 822 | * Other than these two times, never modified. |
| 823 | */ |
| 824 | unsigned special_sig_mask; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 825 | #if ENABLE_HUSH_JOB |
| 826 | unsigned fatal_sig_mask; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 827 | # define G_fatal_sig_mask G.fatal_sig_mask |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 828 | #else |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 829 | # define G_fatal_sig_mask 0 |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 830 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 831 | char **traps; /* char *traps[NSIG] */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 832 | sigset_t pending_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 833 | #if HUSH_DEBUG |
| 834 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 835 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 836 | #endif |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 837 | struct sigaction sa; |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 838 | #if ENABLE_FEATURE_EDITING |
| 839 | char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN]; |
| 840 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 841 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 842 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 843 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 844 | * (too many defines). Also, I actually prefer to see when a variable |
| 845 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 846 | #define INIT_G() do { \ |
| 847 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 848 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ |
| 849 | sigfillset(&G.sa.sa_mask); \ |
| 850 | G.sa.sa_flags = SA_RESTART; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 851 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 852 | |
| 853 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 854 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 855 | static int builtin_cd(char **argv) FAST_FUNC; |
| 856 | static int builtin_echo(char **argv) FAST_FUNC; |
| 857 | static int builtin_eval(char **argv) FAST_FUNC; |
| 858 | static int builtin_exec(char **argv) FAST_FUNC; |
| 859 | static int builtin_exit(char **argv) FAST_FUNC; |
| 860 | static int builtin_export(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 861 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 862 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 863 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 864 | #endif |
| 865 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 866 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 867 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 868 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 869 | static int builtin_history(char **argv) FAST_FUNC; |
| 870 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 871 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 872 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 873 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 874 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 875 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 876 | #endif |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 877 | #if ENABLE_PRINTF |
| 878 | static int builtin_printf(char **argv) FAST_FUNC; |
| 879 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 880 | static int builtin_pwd(char **argv) FAST_FUNC; |
| 881 | static int builtin_read(char **argv) FAST_FUNC; |
| 882 | static int builtin_set(char **argv) FAST_FUNC; |
| 883 | static int builtin_shift(char **argv) FAST_FUNC; |
| 884 | static int builtin_source(char **argv) FAST_FUNC; |
| 885 | static int builtin_test(char **argv) FAST_FUNC; |
| 886 | static int builtin_trap(char **argv) FAST_FUNC; |
| 887 | static int builtin_type(char **argv) FAST_FUNC; |
| 888 | static int builtin_true(char **argv) FAST_FUNC; |
| 889 | static int builtin_umask(char **argv) FAST_FUNC; |
| 890 | static int builtin_unset(char **argv) FAST_FUNC; |
| 891 | static int builtin_wait(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 892 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 893 | static int builtin_break(char **argv) FAST_FUNC; |
| 894 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 895 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 896 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 897 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 898 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 899 | |
| 900 | /* Table of built-in functions. They can be forked or not, depending on |
| 901 | * context: within pipes, they fork. As simple commands, they do not. |
| 902 | * When used in non-forking context, they can change global variables |
| 903 | * in the parent shell process. If forked, of course they cannot. |
| 904 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 905 | * still be set at the end. */ |
| 906 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 907 | const char *b_cmd; |
| 908 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 909 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 910 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 911 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 912 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 913 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 914 | #endif |
| 915 | }; |
| 916 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 917 | static const struct built_in_command bltins1[] = { |
| 918 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 919 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 920 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 921 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 922 | #endif |
| 923 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 924 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 925 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 926 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 927 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 928 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 929 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 930 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 931 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 932 | BLTIN("exit" , builtin_exit , "Exit"), |
| 933 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 934 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 935 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 936 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 937 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 938 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 939 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 940 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 941 | BLTIN("history" , builtin_history , "Show command history"), |
| 942 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 943 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 944 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 945 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 946 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 947 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 948 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 949 | #if HUSH_DEBUG |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 950 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 951 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 952 | BLTIN("read" , builtin_read , "Input into variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 953 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 954 | BLTIN("return" , builtin_return , "Return from a function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 955 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 956 | BLTIN("set" , builtin_set , "Set/unset positional parameters"), |
| 957 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 958 | #if ENABLE_HUSH_BASH_COMPAT |
| 959 | BLTIN("source" , builtin_source , "Run commands in a file"), |
| 960 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 961 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 2bba591 | 2014-03-14 12:43:57 +0100 | [diff] [blame] | 962 | BLTIN("true" , builtin_true , NULL), |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 963 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 964 | BLTIN("ulimit" , shell_builtin_ulimit , "Control resource limits"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 965 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 966 | BLTIN("unset" , builtin_unset , "Unset variables"), |
| 967 | BLTIN("wait" , builtin_wait , "Wait for process"), |
| 968 | }; |
| 969 | /* For now, echo and test are unconditionally enabled. |
| 970 | * Maybe make it configurable? */ |
| 971 | static const struct built_in_command bltins2[] = { |
| 972 | BLTIN("[" , builtin_test , NULL), |
| 973 | BLTIN("echo" , builtin_echo , NULL), |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 974 | #if ENABLE_PRINTF |
| 975 | BLTIN("printf" , builtin_printf , NULL), |
| 976 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 977 | BLTIN("pwd" , builtin_pwd , NULL), |
| 978 | BLTIN("test" , builtin_test , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 979 | }; |
| 980 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 981 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 982 | /* Debug printouts. |
| 983 | */ |
| 984 | #if HUSH_DEBUG |
| 985 | /* prevent disasters with G.debug_indent < 0 */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 986 | # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "") |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 987 | # define debug_enter() (G.debug_indent++) |
| 988 | # define debug_leave() (G.debug_indent--) |
| 989 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 990 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 991 | # define debug_enter() ((void)0) |
| 992 | # define debug_leave() ((void)0) |
| 993 | #endif |
| 994 | |
| 995 | #ifndef debug_printf |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 996 | # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 997 | #endif |
| 998 | |
| 999 | #ifndef debug_printf_parse |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1000 | # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1001 | #endif |
| 1002 | |
| 1003 | #ifndef debug_printf_exec |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1004 | #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1005 | #endif |
| 1006 | |
| 1007 | #ifndef debug_printf_env |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1008 | # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1009 | #endif |
| 1010 | |
| 1011 | #ifndef debug_printf_jobs |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1012 | # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1013 | # define DEBUG_JOBS 1 |
| 1014 | #else |
| 1015 | # define DEBUG_JOBS 0 |
| 1016 | #endif |
| 1017 | |
| 1018 | #ifndef debug_printf_expand |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1019 | # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1020 | # define DEBUG_EXPAND 1 |
| 1021 | #else |
| 1022 | # define DEBUG_EXPAND 0 |
| 1023 | #endif |
| 1024 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1025 | #ifndef debug_printf_varexp |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1026 | # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1027 | #endif |
| 1028 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1029 | #ifndef debug_printf_glob |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1030 | # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1031 | # define DEBUG_GLOB 1 |
| 1032 | #else |
| 1033 | # define DEBUG_GLOB 0 |
| 1034 | #endif |
| 1035 | |
| 1036 | #ifndef debug_printf_list |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1037 | # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1038 | #endif |
| 1039 | |
| 1040 | #ifndef debug_printf_subst |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1041 | # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1042 | #endif |
| 1043 | |
| 1044 | #ifndef debug_printf_clean |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1045 | # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1046 | # define DEBUG_CLEAN 1 |
| 1047 | #else |
| 1048 | # define DEBUG_CLEAN 0 |
| 1049 | #endif |
| 1050 | |
| 1051 | #if DEBUG_EXPAND |
| 1052 | static void debug_print_strings(const char *prefix, char **vv) |
| 1053 | { |
| 1054 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1055 | fdprintf(2, "%s:\n", prefix); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1056 | while (*vv) |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1057 | fdprintf(2, " '%s'\n", *vv++); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1058 | } |
| 1059 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1060 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1061 | #endif |
| 1062 | |
| 1063 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1064 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1065 | */ |
| 1066 | #if LEAK_HUNTING |
| 1067 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1068 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1069 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1070 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1071 | return ptr; |
| 1072 | } |
| 1073 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1074 | { |
| 1075 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1076 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1077 | return ptr; |
| 1078 | } |
| 1079 | static char *xxstrdup(int lineno, const char *str) |
| 1080 | { |
| 1081 | char *ptr = xstrdup(str); |
| 1082 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1083 | return ptr; |
| 1084 | } |
| 1085 | static void xxfree(void *ptr) |
| 1086 | { |
| 1087 | fdprintf(2, "free %p\n", ptr); |
| 1088 | free(ptr); |
| 1089 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1090 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1091 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1092 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1093 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1094 | #endif |
| 1095 | |
| 1096 | |
| 1097 | /* Syntax and runtime errors. They always abort scripts. |
| 1098 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1099 | * and return to the prompt. |
| 1100 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1101 | */ |
| 1102 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1103 | # define die_if_script(lineno, ...) die_if_script(__VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1104 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1105 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1106 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1107 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1108 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1109 | #endif |
| 1110 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1111 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1112 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1113 | va_list p; |
| 1114 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1115 | #if HUSH_DEBUG >= 2 |
| 1116 | bb_error_msg("hush.c:%u", lineno); |
| 1117 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1118 | va_start(p, fmt); |
| 1119 | bb_verror_msg(fmt, p, NULL); |
| 1120 | va_end(p); |
| 1121 | if (!G_interactive_fd) |
| 1122 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1123 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1124 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1125 | static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1126 | { |
| 1127 | if (msg) |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1128 | bb_error_msg("syntax error: %s", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1129 | else |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1130 | bb_error_msg("syntax error"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1133 | static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1134 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1135 | bb_error_msg("syntax error at '%s'", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1138 | 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] | 1139 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1140 | bb_error_msg("syntax error: unterminated %s", s); |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1141 | } |
| 1142 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1143 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1144 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1145 | char msg[2] = { ch, '\0' }; |
| 1146 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1149 | static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1150 | { |
| 1151 | char msg[2]; |
| 1152 | msg[0] = ch; |
| 1153 | msg[1] = '\0'; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1154 | bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1157 | #if HUSH_DEBUG < 2 |
| 1158 | # undef die_if_script |
| 1159 | # undef syntax_error |
| 1160 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1161 | # undef syntax_error_unterm_ch |
| 1162 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1163 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1164 | #else |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1165 | # define die_if_script(...) die_if_script(__LINE__, __VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1166 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1167 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1168 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1169 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1170 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1171 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1172 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1173 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1174 | #if ENABLE_HUSH_INTERACTIVE |
| 1175 | static void cmdedit_update_prompt(void); |
| 1176 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1177 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1178 | #endif |
| 1179 | |
| 1180 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1181 | /* Utility functions |
| 1182 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1183 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1184 | static char *unbackslash(char *src) |
| 1185 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1186 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1187 | while (1) { |
| 1188 | if (*src == '\\') |
| 1189 | src++; |
| 1190 | if ((*dst++ = *src++) == '\0') |
| 1191 | break; |
| 1192 | } |
| 1193 | return dst; |
| 1194 | } |
| 1195 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1196 | 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] | 1197 | { |
| 1198 | int i; |
| 1199 | unsigned count1; |
| 1200 | unsigned count2; |
| 1201 | char **v; |
| 1202 | |
| 1203 | v = strings; |
| 1204 | count1 = 0; |
| 1205 | if (v) { |
| 1206 | while (*v) { |
| 1207 | count1++; |
| 1208 | v++; |
| 1209 | } |
| 1210 | } |
| 1211 | count2 = 0; |
| 1212 | v = add; |
| 1213 | while (*v) { |
| 1214 | count2++; |
| 1215 | v++; |
| 1216 | } |
| 1217 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1218 | v[count1 + count2] = NULL; |
| 1219 | i = count2; |
| 1220 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1221 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1222 | return v; |
| 1223 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1224 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1225 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1226 | { |
| 1227 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1228 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1229 | return ptr; |
| 1230 | } |
| 1231 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1232 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1233 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1234 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1235 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1236 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1237 | { |
| 1238 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1239 | v[0] = add; |
| 1240 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1241 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1242 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1243 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1244 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1245 | { |
| 1246 | char **ptr = add_string_to_strings(strings, add); |
| 1247 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1248 | return ptr; |
| 1249 | } |
| 1250 | #define add_string_to_strings(strings, add) \ |
| 1251 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1252 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1253 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1254 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1255 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1256 | char **v; |
| 1257 | |
| 1258 | if (!strings) |
| 1259 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1260 | v = strings; |
| 1261 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1262 | free(*v); |
| 1263 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1264 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1265 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1268 | |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1269 | static int xdup_and_close(int fd, int F_DUPFD_maybe_CLOEXEC) |
| 1270 | { |
| 1271 | /* We avoid taking stdio fds. Mimicking ash: use fds above 9 */ |
| 1272 | int newfd = fcntl(fd, F_DUPFD_maybe_CLOEXEC, 10); |
| 1273 | if (newfd < 0) { |
| 1274 | /* fd was not open? */ |
| 1275 | if (errno == EBADF) |
| 1276 | return fd; |
| 1277 | xfunc_die(); |
| 1278 | } |
| 1279 | close(fd); |
| 1280 | return newfd; |
| 1281 | } |
| 1282 | |
| 1283 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1284 | /* Manipulating the list of open FILEs */ |
| 1285 | static FILE *remember_FILE(FILE *fp) |
| 1286 | { |
| 1287 | if (fp) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1288 | struct FILE_list *n = xmalloc(sizeof(*n)); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1289 | n->next = G.FILE_list; |
| 1290 | G.FILE_list = n; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1291 | n->fp = fp; |
| 1292 | n->fd = fileno(fp); |
| 1293 | close_on_exec_on(n->fd); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1294 | } |
| 1295 | return fp; |
| 1296 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1297 | static void fclose_and_forget(FILE *fp) |
| 1298 | { |
| 1299 | struct FILE_list **pp = &G.FILE_list; |
| 1300 | while (*pp) { |
| 1301 | struct FILE_list *cur = *pp; |
| 1302 | if (cur->fp == fp) { |
| 1303 | *pp = cur->next; |
| 1304 | free(cur); |
| 1305 | break; |
| 1306 | } |
| 1307 | pp = &cur->next; |
| 1308 | } |
| 1309 | fclose(fp); |
| 1310 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1311 | static int save_FILEs_on_redirect(int fd) |
| 1312 | { |
| 1313 | struct FILE_list *fl = G.FILE_list; |
| 1314 | while (fl) { |
| 1315 | if (fd == fl->fd) { |
| 1316 | /* We use it only on script files, they are all CLOEXEC */ |
| 1317 | fl->fd = xdup_and_close(fd, F_DUPFD_CLOEXEC); |
| 1318 | return 1; |
| 1319 | } |
| 1320 | fl = fl->next; |
| 1321 | } |
| 1322 | return 0; |
| 1323 | } |
| 1324 | static void restore_redirected_FILEs(void) |
| 1325 | { |
| 1326 | struct FILE_list *fl = G.FILE_list; |
| 1327 | while (fl) { |
| 1328 | int should_be = fileno(fl->fp); |
| 1329 | if (fl->fd != should_be) { |
| 1330 | xmove_fd(fl->fd, should_be); |
| 1331 | fl->fd = should_be; |
| 1332 | } |
| 1333 | fl = fl->next; |
| 1334 | } |
| 1335 | } |
| 1336 | #if ENABLE_FEATURE_SH_STANDALONE |
| 1337 | static void close_all_FILE_list(void) |
| 1338 | { |
| 1339 | struct FILE_list *fl = G.FILE_list; |
| 1340 | while (fl) { |
| 1341 | /* fclose would also free FILE object. |
| 1342 | * It is disastrous if we share memory with a vforked parent. |
| 1343 | * I'm not sure we never come here after vfork. |
| 1344 | * Therefore just close fd, nothing more. |
| 1345 | */ |
| 1346 | /*fclose(fl->fp); - unsafe */ |
| 1347 | close(fl->fd); |
| 1348 | fl = fl->next; |
| 1349 | } |
| 1350 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1351 | #endif |
| 1352 | |
| 1353 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1354 | /* Helpers for setting new $n and restoring them back |
| 1355 | */ |
| 1356 | typedef struct save_arg_t { |
| 1357 | char *sv_argv0; |
| 1358 | char **sv_g_argv; |
| 1359 | int sv_g_argc; |
| 1360 | smallint sv_g_malloced; |
| 1361 | } save_arg_t; |
| 1362 | |
| 1363 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1364 | { |
| 1365 | int n; |
| 1366 | |
| 1367 | sv->sv_argv0 = argv[0]; |
| 1368 | sv->sv_g_argv = G.global_argv; |
| 1369 | sv->sv_g_argc = G.global_argc; |
| 1370 | sv->sv_g_malloced = G.global_args_malloced; |
| 1371 | |
| 1372 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1373 | G.global_argv = argv; |
| 1374 | G.global_args_malloced = 0; |
| 1375 | |
| 1376 | n = 1; |
| 1377 | while (*++argv) |
| 1378 | n++; |
| 1379 | G.global_argc = n; |
| 1380 | } |
| 1381 | |
| 1382 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1383 | { |
| 1384 | char **pp; |
| 1385 | |
| 1386 | if (G.global_args_malloced) { |
| 1387 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 1388 | pp = G.global_argv; |
| 1389 | while (*++pp) /* note: does not free $0 */ |
| 1390 | free(*pp); |
| 1391 | free(G.global_argv); |
| 1392 | } |
| 1393 | argv[0] = sv->sv_argv0; |
| 1394 | G.global_argv = sv->sv_g_argv; |
| 1395 | G.global_argc = sv->sv_g_argc; |
| 1396 | G.global_args_malloced = sv->sv_g_malloced; |
| 1397 | } |
| 1398 | |
| 1399 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1400 | /* Basic theory of signal handling in shell |
| 1401 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1402 | * This does not describe what hush does, rather, it is current understanding |
| 1403 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1404 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1405 | * |
| 1406 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1407 | * is finished or backgrounded. It is the same in interactive and |
| 1408 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1409 | * 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] | 1410 | * ^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] | 1411 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1412 | * pipe. |
| 1413 | * |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1414 | * Wait builtin is interruptible by signals for which user trap is set |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1415 | * or by SIGINT in interactive shell. |
| 1416 | * |
| 1417 | * Trap handlers will execute even within trap handlers. (right?) |
| 1418 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1419 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1420 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1421 | * |
| 1422 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1423 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1424 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1425 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1426 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1427 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1428 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1429 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1430 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1431 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1432 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1433 | * |
| 1434 | * SIGQUIT: ignore |
| 1435 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1436 | * SIGHUP (interactive): |
| 1437 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1438 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1439 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1440 | * that all pipe members are stopped. Try this in bash: |
| 1441 | * while :; do :; done - ^Z does not background it |
| 1442 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1443 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1444 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1445 | * to interactive shell while shell is waiting for a pipe, |
| 1446 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1447 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1448 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1449 | * Example 2: this does not wait and does not execute ls: |
| 1450 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1451 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1452 | * "sleep 5; ls -l" + press ^C |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1453 | * Example 4: this does not wait and does not execute ls: |
| 1454 | * "sleep 5 & wait; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1455 | * |
| 1456 | * (What happens to signals which are IGN on shell start?) |
| 1457 | * (What happens with signal mask on shell start?) |
| 1458 | * |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1459 | * Old implementation |
| 1460 | * ================== |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1461 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1462 | * We block all signals which we don't want to take action immediately, |
| 1463 | * i.e. we block all signals which need to have special handling as described |
| 1464 | * above, and all signals which have traps set. |
| 1465 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1466 | * and act on them. |
| 1467 | * |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1468 | * unsigned special_sig_mask: a mask of such "special" signals |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1469 | * sigset_t blocked_set: current blocked signal set |
| 1470 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1471 | * "trap - SIGxxx": |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1472 | * 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] | 1473 | * "trap 'cmd' SIGxxx": |
| 1474 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1475 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1476 | * unblock signals with special interactive handling |
| 1477 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1478 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1479 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1480 | * 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] | 1481 | * 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] | 1482 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1483 | * 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] | 1484 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1485 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1486 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1487 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1488 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1489 | * 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] | 1490 | * 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] | 1491 | * |
| 1492 | * Problem: the above approach makes it unwieldy to catch signals while |
Denys Vlasenko | e95738f | 2013-07-08 03:13:08 +0200 | [diff] [blame] | 1493 | * we are in read builtin, or while we read commands from stdin: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1494 | * masked signals are not visible! |
| 1495 | * |
| 1496 | * New implementation |
| 1497 | * ================== |
| 1498 | * We record each signal we are interested in by installing signal handler |
| 1499 | * for them - a bit like emulating kernel pending signal mask in userspace. |
| 1500 | * We are interested in: signals which need to have special handling |
| 1501 | * as described above, and all signals which have traps set. |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1502 | * Signals are recorded in pending_set. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1503 | * After each pipe execution, we extract any pending signals |
| 1504 | * and act on them. |
| 1505 | * |
| 1506 | * unsigned special_sig_mask: a mask of shell-special signals. |
| 1507 | * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp. |
| 1508 | * char *traps[sig] if trap for sig is set (even if it's ''). |
| 1509 | * sigset_t pending_set: set of sigs we received. |
| 1510 | * |
| 1511 | * "trap - SIGxxx": |
| 1512 | * if sig is in special_sig_mask, set handler back to: |
| 1513 | * record_pending_signo, or to IGN if it's a tty stop signal |
| 1514 | * if sig is in fatal_sig_mask, set handler back to sigexit. |
| 1515 | * else: set handler back to SIG_DFL |
| 1516 | * "trap 'cmd' SIGxxx": |
| 1517 | * set handler to record_pending_signo. |
| 1518 | * "trap '' SIGxxx": |
| 1519 | * set handler to SIG_IGN. |
| 1520 | * after [v]fork, if we plan to be a shell: |
| 1521 | * set signals with special interactive handling to SIG_DFL |
| 1522 | * (because child shell is not interactive), |
| 1523 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
| 1524 | * after [v]fork, if we plan to exec: |
| 1525 | * POSIX says fork clears pending signal mask in child - no need to clear it. |
| 1526 | * |
| 1527 | * To make wait builtin interruptible, we handle SIGCHLD as special signal, |
| 1528 | * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it. |
| 1529 | * |
| 1530 | * Note (compat): |
| 1531 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1532 | * are set to the default actions". bash interprets it so that traps which |
| 1533 | * 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] | 1534 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1535 | enum { |
| 1536 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1537 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1538 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1539 | | (1 << SIGHUP) |
| 1540 | , |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1541 | SPECIAL_JOBSTOP_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1542 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1543 | | (1 << SIGTTIN) |
| 1544 | | (1 << SIGTTOU) |
| 1545 | | (1 << SIGTSTP) |
| 1546 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1547 | , |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1548 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1549 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1550 | static void record_pending_signo(int sig) |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 1551 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1552 | sigaddset(&G.pending_set, sig); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1553 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1554 | if (sig == SIGCHLD) { |
| 1555 | G.count_SIGCHLD++; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1556 | //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] | 1557 | } |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1558 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1559 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1560 | |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1561 | static sighandler_t install_sighandler(int sig, sighandler_t handler) |
| 1562 | { |
| 1563 | struct sigaction old_sa; |
| 1564 | |
| 1565 | /* We could use signal() to install handlers... almost: |
| 1566 | * except that we need to mask ALL signals while handlers run. |
| 1567 | * I saw signal nesting in strace, race window isn't small. |
| 1568 | * SA_RESTART is also needed, but in Linux, signal() |
| 1569 | * sets SA_RESTART too. |
| 1570 | */ |
| 1571 | /* memset(&G.sa, 0, sizeof(G.sa)); - already done */ |
| 1572 | /* sigfillset(&G.sa.sa_mask); - already done */ |
| 1573 | /* G.sa.sa_flags = SA_RESTART; - already done */ |
| 1574 | G.sa.sa_handler = handler; |
| 1575 | sigaction(sig, &G.sa, &old_sa); |
| 1576 | return old_sa.sa_handler; |
| 1577 | } |
| 1578 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1579 | static void hush_exit(int exitcode) NORETURN; |
| 1580 | static void fflush_and__exit(void) NORETURN; |
| 1581 | static void restore_ttypgrp_and__exit(void) NORETURN; |
| 1582 | |
| 1583 | static void restore_ttypgrp_and__exit(void) |
| 1584 | { |
| 1585 | /* xfunc has failed! die die die */ |
| 1586 | /* no EXIT traps, this is an escape hatch! */ |
| 1587 | G.exiting = 1; |
| 1588 | hush_exit(xfunc_error_retval); |
| 1589 | } |
| 1590 | |
| 1591 | /* Needed only on some libc: |
| 1592 | * It was observed that on exit(), fgetc'ed buffered data |
| 1593 | * gets "unwound" via lseek(fd, -NUM, SEEK_CUR). |
| 1594 | * With the net effect that even after fork(), not vfork(), |
| 1595 | * exit() in NOEXECed applet in "sh SCRIPT": |
| 1596 | * noexec_applet_here |
| 1597 | * echo END_OF_SCRIPT |
| 1598 | * lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT". |
| 1599 | * This makes "echo END_OF_SCRIPT" executed twice. |
| 1600 | * Similar problems can be seen with die_if_script() -> xfunc_die() |
| 1601 | * and in `cmd` handling. |
| 1602 | * If set as die_func(), this makes xfunc_die() exit via _exit(), not exit(): |
| 1603 | */ |
| 1604 | static void fflush_and__exit(void) |
| 1605 | { |
| 1606 | fflush_all(); |
| 1607 | _exit(xfunc_error_retval); |
| 1608 | } |
| 1609 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1610 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1611 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1612 | /* 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] | 1613 | # define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1614 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1615 | # 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] | 1616 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1617 | /* Restores tty foreground process group, and exits. |
| 1618 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1619 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1620 | * or called directly with -EXITCODE. |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1621 | * We also call it if xfunc is exiting. |
| 1622 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1623 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1624 | static void sigexit(int sig) |
| 1625 | { |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1626 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1627 | * tty pgrp then, only top-level shell process does that */ |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1628 | if (G_saved_tty_pgrp && getpid() == G.root_pid) { |
| 1629 | /* Disable all signals: job control, SIGPIPE, etc. |
| 1630 | * Mostly paranoid measure, to prevent infinite SIGTTOU. |
| 1631 | */ |
| 1632 | sigprocmask_allsigs(SIG_BLOCK); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1633 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1634 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1635 | |
| 1636 | /* Not a signal, just exit */ |
| 1637 | if (sig <= 0) |
| 1638 | _exit(- sig); |
| 1639 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1640 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1641 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1642 | #else |
| 1643 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1644 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1645 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1646 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1647 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1648 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1649 | static sighandler_t pick_sighandler(unsigned sig) |
| 1650 | { |
| 1651 | sighandler_t handler = SIG_DFL; |
| 1652 | if (sig < sizeof(unsigned)*8) { |
| 1653 | unsigned sigmask = (1 << sig); |
| 1654 | |
| 1655 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1656 | /* is sig fatal? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1657 | if (G_fatal_sig_mask & sigmask) |
| 1658 | handler = sigexit; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1659 | else |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1660 | #endif |
| 1661 | /* sig has special handling? */ |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1662 | if (G.special_sig_mask & sigmask) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1663 | handler = record_pending_signo; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1664 | /* TTIN/TTOU/TSTP can't be set to record_pending_signo |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1665 | * in order to ignore them: they will be raised |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 1666 | * in an endless loop when we try to do some |
| 1667 | * terminal ioctls! We do have to _ignore_ these. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1668 | */ |
| 1669 | if (SPECIAL_JOBSTOP_SIGS & sigmask) |
| 1670 | handler = SIG_IGN; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1671 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1672 | } |
| 1673 | return handler; |
| 1674 | } |
| 1675 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1676 | /* Restores tty foreground process group, and exits. */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1677 | static void hush_exit(int exitcode) |
| 1678 | { |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 1679 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 1680 | save_history(G.line_input_state); |
| 1681 | #endif |
| 1682 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 1683 | fflush_all(); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1684 | 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] | 1685 | char *argv[3]; |
| 1686 | /* argv[0] is unused */ |
| 1687 | argv[1] = G.traps[0]; |
| 1688 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1689 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1690 | /* Note: G.traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 1691 | * "trap" will still show it, if executed |
| 1692 | * in the handler */ |
| 1693 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1694 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1695 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1696 | #if ENABLE_FEATURE_CLEAN_UP |
| 1697 | { |
| 1698 | struct variable *cur_var; |
| 1699 | if (G.cwd != bb_msg_unknown) |
| 1700 | free((char*)G.cwd); |
| 1701 | cur_var = G.top_var; |
| 1702 | while (cur_var) { |
| 1703 | struct variable *tmp = cur_var; |
| 1704 | if (!cur_var->max_len) |
| 1705 | free(cur_var->varstr); |
| 1706 | cur_var = cur_var->next; |
| 1707 | free(tmp); |
| 1708 | } |
| 1709 | } |
| 1710 | #endif |
| 1711 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1712 | fflush_all(); |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1713 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1714 | sigexit(- (exitcode & 0xff)); |
| 1715 | #else |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1716 | _exit(exitcode); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1717 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 1720 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1721 | //TODO: return a mask of ALL handled sigs? |
| 1722 | static int check_and_run_traps(void) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1723 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1724 | int last_sig = 0; |
| 1725 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1726 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1727 | int sig; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 1728 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1729 | if (sigisemptyset(&G.pending_set)) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1730 | break; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1731 | sig = 0; |
| 1732 | do { |
| 1733 | sig++; |
| 1734 | if (sigismember(&G.pending_set, sig)) { |
| 1735 | sigdelset(&G.pending_set, sig); |
| 1736 | goto got_sig; |
| 1737 | } |
| 1738 | } while (sig < NSIG); |
| 1739 | break; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1740 | got_sig: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1741 | if (G.traps && G.traps[sig]) { |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1742 | debug_printf_exec("%s: sig:%d handler:'%s'\n", __func__, sig, G.traps[sig]); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1743 | if (G.traps[sig][0]) { |
| 1744 | /* We have user-defined handler */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1745 | smalluint save_rcode; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1746 | char *argv[3]; |
| 1747 | /* argv[0] is unused */ |
| 1748 | argv[1] = G.traps[sig]; |
| 1749 | argv[2] = NULL; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1750 | save_rcode = G.last_exitcode; |
| 1751 | builtin_eval(argv); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1752 | G.last_exitcode = save_rcode; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1753 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1754 | } /* else: "" trap, ignoring signal */ |
| 1755 | continue; |
| 1756 | } |
| 1757 | /* not a trap: special action */ |
| 1758 | switch (sig) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1759 | case SIGINT: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1760 | debug_printf_exec("%s: sig:%d default SIGINT handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1761 | /* Builtin was ^C'ed, make it look prettier: */ |
| 1762 | bb_putchar('\n'); |
| 1763 | G.flag_SIGINT = 1; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1764 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1765 | break; |
| 1766 | #if ENABLE_HUSH_JOB |
| 1767 | case SIGHUP: { |
| 1768 | struct pipe *job; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1769 | debug_printf_exec("%s: sig:%d default SIGHUP handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1770 | /* bash is observed to signal whole process groups, |
| 1771 | * not individual processes */ |
| 1772 | for (job = G.job_list; job; job = job->next) { |
| 1773 | if (job->pgrp <= 0) |
| 1774 | continue; |
| 1775 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 1776 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 1777 | kill(- job->pgrp, SIGCONT); |
| 1778 | } |
| 1779 | sigexit(SIGHUP); |
| 1780 | } |
| 1781 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1782 | #if ENABLE_HUSH_FAST |
| 1783 | case SIGCHLD: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1784 | debug_printf_exec("%s: sig:%d default SIGCHLD handler\n", __func__, sig); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1785 | G.count_SIGCHLD++; |
| 1786 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1787 | /* Note: |
| 1788 | * We dont do 'last_sig = sig' here -> NOT returning this sig. |
| 1789 | * This simplifies wait builtin a bit. |
| 1790 | */ |
| 1791 | break; |
| 1792 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1793 | default: /* ignored: */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1794 | 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] | 1795 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1796 | /* Note: |
| 1797 | * We dont do 'last_sig = sig' here -> NOT returning this sig. |
| 1798 | * Example: wait is not interrupted by TERM |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1799 | * in interactive shell, because TERM is ignored. |
| 1800 | */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1801 | break; |
| 1802 | } |
| 1803 | } |
| 1804 | return last_sig; |
| 1805 | } |
| 1806 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1807 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1808 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1809 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1810 | if (force || G.cwd == NULL) { |
| 1811 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1812 | * we must not try to free(bb_msg_unknown) */ |
| 1813 | if (G.cwd == bb_msg_unknown) |
| 1814 | G.cwd = NULL; |
| 1815 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1816 | if (!G.cwd) |
| 1817 | G.cwd = bb_msg_unknown; |
| 1818 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1819 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1822 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1823 | /* |
| 1824 | * Shell and environment variable support |
| 1825 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1826 | 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] | 1827 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1828 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1829 | struct variable *cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1830 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1831 | pp = &G.top_var; |
| 1832 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1833 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1834 | return pp; |
| 1835 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1836 | } |
| 1837 | return NULL; |
| 1838 | } |
| 1839 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1840 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1841 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1842 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1843 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1844 | |
| 1845 | if (G.expanded_assignments) { |
| 1846 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1847 | while (*cpp) { |
| 1848 | char *cp = *cpp; |
| 1849 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 1850 | return cp + len + 1; |
| 1851 | cpp++; |
| 1852 | } |
| 1853 | } |
| 1854 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1855 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1856 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1857 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1858 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 1859 | if (strcmp(name, "PPID") == 0) |
| 1860 | return utoa(G.root_ppid); |
| 1861 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1862 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1863 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1864 | return utoa(next_random(&G.random_gen)); |
| 1865 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1866 | return NULL; |
| 1867 | } |
| 1868 | |
| 1869 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1870 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1871 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1872 | * 0: do not change export flag |
| 1873 | * (if creating new variable, flag will be 0) |
| 1874 | * 1: set export flag and putenv the variable |
| 1875 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1876 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1877 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1878 | #if !BB_MMU && ENABLE_HUSH_LOCAL |
| 1879 | /* all params are used */ |
| 1880 | #elif BB_MMU && ENABLE_HUSH_LOCAL |
| 1881 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1882 | set_local_var(str, flg_export, local_lvl) |
| 1883 | #elif BB_MMU && !ENABLE_HUSH_LOCAL |
| 1884 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1885 | set_local_var(str, flg_export) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1886 | #elif !BB_MMU && !ENABLE_HUSH_LOCAL |
| 1887 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1888 | set_local_var(str, flg_export, flg_read_only) |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1889 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1890 | static int set_local_var(char *str, int flg_export, int local_lvl, int flg_read_only) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1891 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1892 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1893 | struct variable *cur; |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 1894 | char *free_me = NULL; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1895 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1896 | int name_len; |
| 1897 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1898 | eq_sign = strchr(str, '='); |
| 1899 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1900 | free(str); |
| 1901 | return -1; |
| 1902 | } |
| 1903 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1904 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1905 | var_pp = &G.top_var; |
| 1906 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1907 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1908 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1909 | continue; |
| 1910 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 1911 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1912 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1913 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1914 | #if !BB_MMU |
| 1915 | if (!flg_read_only) |
| 1916 | #endif |
| 1917 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1918 | free(str); |
| 1919 | return -1; |
| 1920 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1921 | if (flg_export == -1) { // "&& cur->flg_export" ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1922 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1923 | *eq_sign = '\0'; |
| 1924 | unsetenv(str); |
| 1925 | *eq_sign = '='; |
| 1926 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1927 | #if ENABLE_HUSH_LOCAL |
| 1928 | if (cur->func_nest_level < local_lvl) { |
| 1929 | /* New variable is declared as local, |
| 1930 | * and existing one is global, or local |
| 1931 | * from enclosing function. |
| 1932 | * Remove and save old one: */ |
| 1933 | *var_pp = cur->next; |
| 1934 | cur->next = *G.shadowed_vars_pp; |
| 1935 | *G.shadowed_vars_pp = cur; |
| 1936 | /* bash 3.2.33(1) and exported vars: |
| 1937 | * # export z=z |
| 1938 | * # f() { local z=a; env | grep ^z; } |
| 1939 | * # f |
| 1940 | * z=a |
| 1941 | * # env | grep ^z |
| 1942 | * z=z |
| 1943 | */ |
| 1944 | if (cur->flg_export) |
| 1945 | flg_export = 1; |
| 1946 | break; |
| 1947 | } |
| 1948 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1949 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1950 | free_and_exp: |
| 1951 | free(str); |
| 1952 | goto exp; |
| 1953 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1954 | if (cur->max_len != 0) { |
| 1955 | if (cur->max_len >= strlen(str)) { |
| 1956 | /* This one is from startup env, reuse space */ |
| 1957 | strcpy(cur->varstr, str); |
| 1958 | goto free_and_exp; |
| 1959 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 1960 | /* Can't reuse */ |
| 1961 | cur->max_len = 0; |
| 1962 | goto set_str_and_exp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1963 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 1964 | /* max_len == 0 signifies "malloced" var, which we can |
| 1965 | * (and have to) free. But we can't free(cur->varstr) here: |
| 1966 | * if cur->flg_export is 1, it is in the environment. |
| 1967 | * We should either unsetenv+free, or wait until putenv, |
| 1968 | * then putenv(new)+free(old). |
| 1969 | */ |
| 1970 | free_me = cur->varstr; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1971 | goto set_str_and_exp; |
| 1972 | } |
| 1973 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1974 | /* Not found - create new variable struct */ |
| 1975 | cur = xzalloc(sizeof(*cur)); |
| 1976 | #if ENABLE_HUSH_LOCAL |
| 1977 | cur->func_nest_level = local_lvl; |
| 1978 | #endif |
| 1979 | cur->next = *var_pp; |
| 1980 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1981 | |
| 1982 | set_str_and_exp: |
| 1983 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1984 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1985 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1986 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1987 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1988 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1989 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1990 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1991 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1992 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1993 | if (flg_export == -1) { |
| 1994 | cur->flg_export = 0; |
| 1995 | /* unsetenv was already done */ |
| 1996 | } else { |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 1997 | int i; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1998 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 1999 | i = putenv(cur->varstr); |
| 2000 | /* only now we can free old exported malloced string */ |
| 2001 | free(free_me); |
| 2002 | return i; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2003 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2004 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2005 | free(free_me); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2006 | return 0; |
| 2007 | } |
| 2008 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2009 | /* Used at startup and after each cd */ |
| 2010 | static void set_pwd_var(int exp) |
| 2011 | { |
| 2012 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), |
| 2013 | /*exp:*/ exp, /*lvl:*/ 0, /*ro:*/ 0); |
| 2014 | } |
| 2015 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2016 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2017 | { |
| 2018 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2019 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2020 | |
| 2021 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2022 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2023 | var_pp = &G.top_var; |
| 2024 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2025 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 2026 | if (cur->flg_read_only) { |
| 2027 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2028 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2029 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2030 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2031 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 2032 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2033 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 2034 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2035 | if (!cur->max_len) |
| 2036 | free(cur->varstr); |
| 2037 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2038 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2039 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2040 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2041 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2042 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2045 | static int unset_local_var(const char *name) |
| 2046 | { |
| 2047 | return unset_local_var_len(name, strlen(name)); |
| 2048 | } |
| 2049 | |
| 2050 | static void unset_vars(char **strings) |
| 2051 | { |
| 2052 | char **v; |
| 2053 | |
| 2054 | if (!strings) |
| 2055 | return; |
| 2056 | v = strings; |
| 2057 | while (*v) { |
| 2058 | const char *eq = strchrnul(*v, '='); |
| 2059 | unset_local_var_len(*v, (int)(eq - *v)); |
| 2060 | v++; |
| 2061 | } |
| 2062 | free(strings); |
| 2063 | } |
| 2064 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2065 | 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] | 2066 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2067 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2068 | set_local_var(var, /*flags:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2069 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2070 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2071 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2072 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2073 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 2074 | */ |
| 2075 | static void add_vars(struct variable *var) |
| 2076 | { |
| 2077 | struct variable *next; |
| 2078 | |
| 2079 | while (var) { |
| 2080 | next = var->next; |
| 2081 | var->next = G.top_var; |
| 2082 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2083 | if (var->flg_export) { |
| 2084 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2085 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2086 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2087 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2088 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2089 | var = next; |
| 2090 | } |
| 2091 | } |
| 2092 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2093 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2094 | { |
| 2095 | char **s; |
| 2096 | struct variable *old = NULL; |
| 2097 | |
| 2098 | if (!strings) |
| 2099 | return old; |
| 2100 | s = strings; |
| 2101 | while (*s) { |
| 2102 | struct variable *var_p; |
| 2103 | struct variable **var_pp; |
| 2104 | char *eq; |
| 2105 | |
| 2106 | eq = strchr(*s, '='); |
| 2107 | if (eq) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2108 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2109 | if (var_pp) { |
| 2110 | /* Remove variable from global linked list */ |
| 2111 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2112 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2113 | *var_pp = var_p->next; |
| 2114 | /* Add it to returned list */ |
| 2115 | var_p->next = old; |
| 2116 | old = var_p; |
| 2117 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2118 | set_local_var(*s, /*exp:*/ 1, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2119 | } |
| 2120 | s++; |
| 2121 | } |
| 2122 | return old; |
| 2123 | } |
| 2124 | |
| 2125 | |
| 2126 | /* |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2127 | * Unicode helper |
| 2128 | */ |
| 2129 | static void reinit_unicode_for_hush(void) |
| 2130 | { |
| 2131 | /* Unicode support should be activated even if LANG is set |
| 2132 | * _during_ shell execution, not only if it was set when |
| 2133 | * shell was started. Therefore, re-check LANG every time: |
| 2134 | */ |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2135 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2136 | || ENABLE_UNICODE_USING_LOCALE |
| 2137 | ) { |
| 2138 | const char *s = get_local_var_value("LC_ALL"); |
| 2139 | if (!s) s = get_local_var_value("LC_CTYPE"); |
| 2140 | if (!s) s = get_local_var_value("LANG"); |
| 2141 | reinit_unicode(s); |
| 2142 | } |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2143 | } |
| 2144 | |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2145 | /* |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2146 | * in_str support (strings, and "strings" read from files). |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2147 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2148 | |
| 2149 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2150 | /* To test correct lineedit/interactive behavior, type from command line: |
| 2151 | * echo $P\ |
| 2152 | * \ |
| 2153 | * AT\ |
| 2154 | * H\ |
| 2155 | * \ |
| 2156 | * It excercises a lot of corner cases. |
| 2157 | */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2158 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2159 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2160 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2161 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2162 | if (G.PS1 == NULL) |
| 2163 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2164 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 2165 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2166 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 2167 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2168 | if (G.PS2 == NULL) |
| 2169 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2170 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2171 | static const char *setup_prompt_string(int promptmode) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2172 | { |
| 2173 | const char *prompt_str; |
| 2174 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2175 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 2176 | /* Set up the prompt */ |
| 2177 | if (promptmode == 0) { /* PS1 */ |
| 2178 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2179 | /* bash uses $PWD value, even if it is set by user. |
| 2180 | * It uses current dir only if PWD is unset. |
| 2181 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2182 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2183 | prompt_str = G.PS1; |
| 2184 | } else |
| 2185 | prompt_str = G.PS2; |
| 2186 | } else |
| 2187 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2188 | debug_printf("prompt_str '%s'\n", prompt_str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2189 | return prompt_str; |
| 2190 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2191 | static int get_user_input(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2192 | { |
| 2193 | int r; |
| 2194 | const char *prompt_str; |
| 2195 | |
| 2196 | prompt_str = setup_prompt_string(i->promptmode); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2197 | # if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2198 | do { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2199 | reinit_unicode_for_hush(); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2200 | G.flag_SIGINT = 0; |
| 2201 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 2202 | * only after <Enter>. (^C will work) */ |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2203 | r = read_line_input(G.line_input_state, prompt_str, |
| 2204 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1, |
| 2205 | /*timeout*/ -1 |
| 2206 | ); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2207 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2208 | check_and_run_traps(); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2209 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2210 | if (r < 0) { |
| 2211 | /* EOF/error detected */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2212 | i->p = NULL; |
| 2213 | i->peek_buf[0] = r = EOF; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2214 | return r; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2215 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2216 | i->p = G.user_input_buf; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2217 | return (unsigned char)*i->p++; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2218 | # else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2219 | do { |
| 2220 | G.flag_SIGINT = 0; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2221 | if (i->last_char == '\0' || i->last_char == '\n') { |
| 2222 | /* Why check_and_run_traps here? Try this interactively: |
| 2223 | * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) & |
| 2224 | * $ <[enter], repeatedly...> |
| 2225 | * Without check_and_run_traps, handler never runs. |
| 2226 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2227 | check_and_run_traps(); |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2228 | fputs(prompt_str, stdout); |
| 2229 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2230 | fflush_all(); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2231 | r = fgetc(i->file); |
| 2232 | } while (G.flag_SIGINT || r == '\0'); |
| 2233 | return r; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2234 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2235 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2236 | /* This is the magic location that prints prompts |
| 2237 | * and gets data back from the user */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2238 | static int fgetc_interactive(struct in_str *i) |
| 2239 | { |
| 2240 | int ch; |
| 2241 | /* If it's interactive stdin, get new line. */ |
| 2242 | if (G_interactive_fd && i->file == stdin) { |
| 2243 | /* Returns first char (or EOF), the rest is in i->p[] */ |
| 2244 | ch = get_user_input(i); |
| 2245 | i->promptmode = 1; /* PS2 */ |
| 2246 | } else { |
| 2247 | /* Not stdin: script file, sourced file, etc */ |
| 2248 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2249 | } |
| 2250 | return ch; |
| 2251 | } |
| 2252 | #else |
| 2253 | static inline int fgetc_interactive(struct in_str *i) |
| 2254 | { |
| 2255 | int ch; |
| 2256 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2257 | return ch; |
| 2258 | } |
| 2259 | #endif /* INTERACTIVE */ |
| 2260 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2261 | static int FAST_FUNC file_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2262 | { |
| 2263 | int ch; |
| 2264 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2265 | #if ENABLE_FEATURE_EDITING |
| 2266 | /* This can be stdin, check line editing char[] buffer */ |
| 2267 | if (i->p && *i->p != '\0') { |
| 2268 | ch = (unsigned char)*i->p++; |
| 2269 | goto out; |
| 2270 | } |
| 2271 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2272 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2273 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2274 | if (ch != 0) { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2275 | int ch2 = i->peek_buf[1]; |
| 2276 | i->peek_buf[0] = ch2; |
| 2277 | if (ch2 == 0) /* very likely, avoid redundant write */ |
| 2278 | goto out; |
| 2279 | i->peek_buf[1] = 0; |
| 2280 | goto out; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2281 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2282 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2283 | ch = fgetc_interactive(i); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2284 | out: |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2285 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 2286 | i->last_char = ch; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2287 | return ch; |
| 2288 | } |
| 2289 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2290 | static int FAST_FUNC file_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2291 | { |
| 2292 | int ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2293 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2294 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2295 | /* This can be stdin, check line editing char[] buffer */ |
| 2296 | if (i->p && *i->p != '\0') |
| 2297 | return (unsigned char)*i->p; |
| 2298 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2299 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2300 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2301 | if (ch != 0) |
| 2302 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2303 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2304 | /* Need to get a new char */ |
| 2305 | ch = fgetc_interactive(i); |
| 2306 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
| 2307 | |
| 2308 | /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */ |
| 2309 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
| 2310 | if (i->p) { |
| 2311 | i->p -= 1; |
| 2312 | return ch; |
| 2313 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2314 | #endif |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2315 | i->peek_buf[0] = ch; |
| 2316 | /*i->peek_buf[1] = 0; - already is */ |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2317 | return ch; |
| 2318 | } |
| 2319 | |
| 2320 | static int FAST_FUNC static_get(struct in_str *i) |
| 2321 | { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2322 | int ch = (unsigned char)*i->p; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2323 | if (ch != '\0') { |
| 2324 | i->p++; |
| 2325 | i->last_char = ch; |
| 2326 | return ch; |
| 2327 | } |
| 2328 | return EOF; |
| 2329 | } |
| 2330 | |
| 2331 | static int FAST_FUNC static_peek(struct in_str *i) |
| 2332 | { |
| 2333 | /* Doesn't report EOF on NUL. None of the callers care. */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2334 | return (unsigned char)*i->p; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2335 | } |
| 2336 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2337 | /* Only ever called if i_peek() was called, and did not return EOF. |
| 2338 | * IOW: we know the previous peek saw an ordinary char, not EOF, not NUL, |
| 2339 | * not end-of-line. Therefore we never need to read a new editing line here. |
| 2340 | */ |
| 2341 | static int i_peek2(struct in_str *i) |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2342 | { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2343 | int ch; |
| 2344 | |
| 2345 | /* There are two cases when i->p[] buffer exists. |
| 2346 | * (1) it's a string in_str. |
Denys Vlasenko | 08755f9 | 2016-09-30 02:02:25 +0200 | [diff] [blame] | 2347 | * (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] | 2348 | * In both cases, we know that i->p[0] exists and not NUL, and |
| 2349 | * the peek2 result is in i->p[1]. |
| 2350 | */ |
| 2351 | if (i->p) |
| 2352 | return (unsigned char)i->p[1]; |
| 2353 | |
| 2354 | /* Now we know it is a file-based in_str. */ |
| 2355 | |
| 2356 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2357 | /* Is there 2nd char? */ |
| 2358 | ch = i->peek_buf[1]; |
| 2359 | if (ch == 0) { |
| 2360 | /* We did not read it yet, get it now */ |
| 2361 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2362 | i->peek_buf[1] = ch; |
| 2363 | } |
| 2364 | |
| 2365 | debug_printf("file_peek2: got '%c' %d\n", ch, ch); |
| 2366 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2367 | } |
| 2368 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2369 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 2370 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2371 | memset(i, 0, sizeof(*i)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2372 | i->get = file_get; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2373 | i->peek = file_peek; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2374 | /* i->promptmode = 0; - PS1 (memset did it) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2375 | i->file = f; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2376 | /* i->p = NULL; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 2380 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2381 | memset(i, 0, sizeof(*i)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2382 | i->get = static_get; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2383 | i->peek = static_peek; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2384 | /* i->promptmode = 0; - PS1 (memset did it) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2385 | i->p = s; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2389 | /* |
| 2390 | * o_string support |
| 2391 | */ |
| 2392 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2393 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2394 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2395 | { |
| 2396 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2397 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2398 | if (o->data) |
| 2399 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2402 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2403 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2404 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2405 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2408 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 2409 | { |
| 2410 | free(o->data); |
| 2411 | } |
| 2412 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2413 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2414 | { |
| 2415 | if (o->length + len > o->maxlen) { |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2416 | o->maxlen += (2 * len) | (B_CHUNK-1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2417 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2418 | } |
| 2419 | } |
| 2420 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2421 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2422 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2423 | 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] | 2424 | if (o->length < o->maxlen) { |
| 2425 | /* likely. avoid o_grow_by() call */ |
| 2426 | add: |
| 2427 | o->data[o->length] = ch; |
| 2428 | o->length++; |
| 2429 | o->data[o->length] = '\0'; |
| 2430 | return; |
| 2431 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2432 | o_grow_by(o, 1); |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2433 | goto add; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 2436 | #if 0 |
| 2437 | /* Valid only if we know o_string is not empty */ |
| 2438 | static void o_delchr(o_string *o) |
| 2439 | { |
| 2440 | o->length--; |
| 2441 | o->data[o->length] = '\0'; |
| 2442 | } |
| 2443 | #endif |
| 2444 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2445 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2446 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2447 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2448 | memcpy(&o->data[o->length], str, len); |
| 2449 | o->length += len; |
| 2450 | o->data[o->length] = '\0'; |
| 2451 | } |
| 2452 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2453 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2454 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2455 | o_addblock(o, str, strlen(str)); |
| 2456 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2457 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2458 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2459 | static void nommu_addchr(o_string *o, int ch) |
| 2460 | { |
| 2461 | if (o) |
| 2462 | o_addchr(o, ch); |
| 2463 | } |
| 2464 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2465 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2466 | #endif |
| 2467 | |
| 2468 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2469 | { |
| 2470 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2473 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2474 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2475 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2476 | * Apparently, on unquoted $v bash still does globbing |
| 2477 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2478 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2479 | * quoting mechanisms on $v expansion side: one protects |
| 2480 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2481 | * We have only second one. |
| 2482 | */ |
| 2483 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2484 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2485 | # define MAYBE_BRACES "{}" |
| 2486 | #else |
| 2487 | # define MAYBE_BRACES "" |
| 2488 | #endif |
| 2489 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2490 | /* My analysis of quoting semantics tells me that state information |
| 2491 | * is associated with a destination, not a source. |
| 2492 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2493 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2494 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2495 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2496 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2497 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2498 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2499 | o_grow_by(o, sz); |
| 2500 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2501 | o->data[o->length] = '\\'; |
| 2502 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2503 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2504 | o->data[o->length] = ch; |
| 2505 | o->length++; |
| 2506 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2509 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2510 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2511 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2512 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
| 2513 | && strchr("*?[\\" MAYBE_BRACES, ch) |
| 2514 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2515 | sz++; |
| 2516 | o->data[o->length] = '\\'; |
| 2517 | o->length++; |
| 2518 | } |
| 2519 | o_grow_by(o, sz); |
| 2520 | o->data[o->length] = ch; |
| 2521 | o->length++; |
| 2522 | o->data[o->length] = '\0'; |
| 2523 | } |
| 2524 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2525 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2526 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2527 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2528 | char ch; |
| 2529 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2530 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2531 | if (ordinary_cnt > len) /* paranoia */ |
| 2532 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2533 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2534 | if (ordinary_cnt == len) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2535 | return; /* NUL is already added by o_addblock */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2536 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2537 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2538 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2539 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2540 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2541 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2542 | sz++; |
| 2543 | o->data[o->length] = '\\'; |
| 2544 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2545 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2546 | o_grow_by(o, sz); |
| 2547 | o->data[o->length] = ch; |
| 2548 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2549 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2550 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2553 | static void o_addQblock(o_string *o, const char *str, int len) |
| 2554 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2555 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2556 | o_addblock(o, str, len); |
| 2557 | return; |
| 2558 | } |
| 2559 | o_addqblock(o, str, len); |
| 2560 | } |
| 2561 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2562 | static void o_addQstr(o_string *o, const char *str) |
| 2563 | { |
| 2564 | o_addQblock(o, str, strlen(str)); |
| 2565 | } |
| 2566 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2567 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2568 | * 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] | 2569 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2570 | * list[i] contains an INDEX (int!) into this string data. |
| 2571 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2572 | * but list[i]'s need not be modified. |
| 2573 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2574 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2575 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2576 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2577 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2578 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2579 | { |
| 2580 | char **list = (char**)o->data; |
| 2581 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2582 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2583 | |
| 2584 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2585 | 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] | 2586 | prefix, list, n, string_start, o->length, o->maxlen, |
| 2587 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 2588 | o->has_quoted_part, |
| 2589 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2590 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2591 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2592 | fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i], |
| 2593 | o->data + (int)(uintptr_t)list[i] + string_start, |
| 2594 | o->data + (int)(uintptr_t)list[i] + string_start); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2595 | i++; |
| 2596 | } |
| 2597 | if (n) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2598 | 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] | 2599 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2600 | 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] | 2601 | } |
| 2602 | } |
| 2603 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2604 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2605 | #endif |
| 2606 | |
| 2607 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 2608 | * in list[n] so that it points past last stored byte so far. |
| 2609 | * It returns n+1. */ |
| 2610 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2611 | { |
| 2612 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2613 | int string_start; |
| 2614 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2615 | |
| 2616 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2617 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2618 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2619 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2620 | 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] | 2621 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 2622 | o->maxlen += 0x10 * sizeof(list[0]); |
| 2623 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 2624 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2625 | memmove(list + n + 0x10, list + n, string_len); |
| 2626 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2627 | } else { |
| 2628 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 2629 | n, string_len, string_start); |
| 2630 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2631 | } else { |
| 2632 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2633 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 2634 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2635 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 2636 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2637 | o->has_empty_slot = 0; |
| 2638 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2639 | o->has_quoted_part = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2640 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2641 | return n + 1; |
| 2642 | } |
| 2643 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2644 | /* "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] | 2645 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2646 | { |
| 2647 | char **list = (char**)o->data; |
| 2648 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2649 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2650 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2653 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2654 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 2655 | * first, it processes even {a} (no commas), second, |
| 2656 | * 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] | 2657 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2658 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2659 | |
| 2660 | /* Helper */ |
| 2661 | static int glob_needed(const char *s) |
| 2662 | { |
| 2663 | while (*s) { |
| 2664 | if (*s == '\\') { |
| 2665 | if (!s[1]) |
| 2666 | return 0; |
| 2667 | s += 2; |
| 2668 | continue; |
| 2669 | } |
| 2670 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 2671 | return 1; |
| 2672 | s++; |
| 2673 | } |
| 2674 | return 0; |
| 2675 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2676 | /* Return pointer to next closing brace or to comma */ |
| 2677 | static const char *next_brace_sub(const char *cp) |
| 2678 | { |
| 2679 | unsigned depth = 0; |
| 2680 | cp++; |
| 2681 | while (*cp != '\0') { |
| 2682 | if (*cp == '\\') { |
| 2683 | if (*++cp == '\0') |
| 2684 | break; |
| 2685 | cp++; |
| 2686 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 2687 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2688 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2689 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2690 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2691 | depth++; |
| 2692 | } |
| 2693 | |
| 2694 | return *cp != '\0' ? cp : NULL; |
| 2695 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2696 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 2697 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2698 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2699 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2700 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2701 | const char *next; |
| 2702 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2703 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2704 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2705 | |
| 2706 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 2707 | |
| 2708 | begin = pattern; |
| 2709 | while (1) { |
| 2710 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2711 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2712 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2713 | /* Find the first sub-pattern and at the same time |
| 2714 | * find the rest after the closing brace */ |
| 2715 | next = next_brace_sub(begin); |
| 2716 | if (next == NULL) { |
| 2717 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2718 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2719 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2720 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2721 | /* "{abc}" with no commas - illegal |
| 2722 | * brace expr, disregard and skip it */ |
| 2723 | begin = next + 1; |
| 2724 | continue; |
| 2725 | } |
| 2726 | break; |
| 2727 | } |
| 2728 | if (*begin == '\\' && begin[1] != '\0') |
| 2729 | begin++; |
| 2730 | begin++; |
| 2731 | } |
| 2732 | debug_printf_glob("begin:%s\n", begin); |
| 2733 | debug_printf_glob("next:%s\n", next); |
| 2734 | |
| 2735 | /* Now find the end of the whole brace expression */ |
| 2736 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2737 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2738 | rest = next_brace_sub(rest); |
| 2739 | if (rest == NULL) { |
| 2740 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2741 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2742 | } |
| 2743 | debug_printf_glob("rest:%s\n", rest); |
| 2744 | } |
| 2745 | rest_len = strlen(++rest) + 1; |
| 2746 | |
| 2747 | /* We are sure the brace expression is well-formed */ |
| 2748 | |
| 2749 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2750 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2751 | |
| 2752 | /* We have a brace expression. BEGIN points to the opening {, |
| 2753 | * NEXT points past the terminator of the first element, and REST |
| 2754 | * points past the final }. We will accumulate result names from |
| 2755 | * recursive runs for each brace alternative in the buffer using |
| 2756 | * GLOB_APPEND. */ |
| 2757 | |
| 2758 | p = begin + 1; |
| 2759 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2760 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2761 | memcpy( |
| 2762 | mempcpy( |
| 2763 | mempcpy(new_pattern_buf, |
| 2764 | /* We know the prefix for all sub-patterns */ |
| 2765 | pattern, begin - pattern), |
| 2766 | p, next - p), |
| 2767 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2768 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2769 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 2770 | * That's why we re-copy prefix every time (1st memcpy above). |
| 2771 | */ |
| 2772 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2773 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2774 | /* We saw the last entry */ |
| 2775 | break; |
| 2776 | } |
| 2777 | p = next + 1; |
| 2778 | next = next_brace_sub(next); |
| 2779 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2780 | free(new_pattern_buf); |
| 2781 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2782 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2783 | simple_glob: |
| 2784 | { |
| 2785 | int gr; |
| 2786 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2787 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2788 | memset(&globdata, 0, sizeof(globdata)); |
| 2789 | gr = glob(pattern, 0, NULL, &globdata); |
| 2790 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 2791 | if (gr != 0) { |
| 2792 | if (gr == GLOB_NOMATCH) { |
| 2793 | globfree(&globdata); |
| 2794 | /* NB: garbles parameter */ |
| 2795 | unbackslash(pattern); |
| 2796 | o_addstr_with_NUL(o, pattern); |
| 2797 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2798 | return o_save_ptr_helper(o, n); |
| 2799 | } |
| 2800 | if (gr == GLOB_NOSPACE) |
| 2801 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
| 2802 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2803 | * but we didn't specify it. Paranoia again. */ |
| 2804 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 2805 | } |
| 2806 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2807 | char **argv = globdata.gl_pathv; |
| 2808 | while (1) { |
| 2809 | o_addstr_with_NUL(o, *argv); |
| 2810 | n = o_save_ptr_helper(o, n); |
| 2811 | argv++; |
| 2812 | if (!*argv) |
| 2813 | break; |
| 2814 | } |
| 2815 | } |
| 2816 | globfree(&globdata); |
| 2817 | } |
| 2818 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2819 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2820 | /* Performs globbing on last list[], |
| 2821 | * saving each result as a new list[]. |
| 2822 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2823 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2824 | { |
| 2825 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2826 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2827 | 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] | 2828 | if (!o->data) |
| 2829 | return o_save_ptr_helper(o, n); |
| 2830 | pattern = o->data + o_get_last_ptr(o, n); |
| 2831 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 2832 | if (!glob_needed(pattern)) { |
| 2833 | /* unbackslash last string in o in place, fix length */ |
| 2834 | o->length = unbackslash(pattern) - o->data; |
| 2835 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2836 | return o_save_ptr_helper(o, n); |
| 2837 | } |
| 2838 | |
| 2839 | copy = xstrdup(pattern); |
| 2840 | /* "forget" pattern in o */ |
| 2841 | o->length = pattern - o->data; |
| 2842 | n = glob_brace(copy, o, n); |
| 2843 | free(copy); |
| 2844 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2845 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2846 | return n; |
| 2847 | } |
| 2848 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2849 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2850 | |
| 2851 | /* Helper */ |
| 2852 | static int glob_needed(const char *s) |
| 2853 | { |
| 2854 | while (*s) { |
| 2855 | if (*s == '\\') { |
| 2856 | if (!s[1]) |
| 2857 | return 0; |
| 2858 | s += 2; |
| 2859 | continue; |
| 2860 | } |
| 2861 | if (*s == '*' || *s == '[' || *s == '?') |
| 2862 | return 1; |
| 2863 | s++; |
| 2864 | } |
| 2865 | return 0; |
| 2866 | } |
| 2867 | /* Performs globbing on last list[], |
| 2868 | * saving each result as a new list[]. |
| 2869 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2870 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2871 | { |
| 2872 | glob_t globdata; |
| 2873 | int gr; |
| 2874 | char *pattern; |
| 2875 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2876 | 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] | 2877 | if (!o->data) |
| 2878 | return o_save_ptr_helper(o, n); |
| 2879 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2880 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2881 | if (!glob_needed(pattern)) { |
| 2882 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2883 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2884 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2885 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2886 | return o_save_ptr_helper(o, n); |
| 2887 | } |
| 2888 | |
| 2889 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2890 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 2891 | * If we glob "*.\*" and don't find anything, we need |
| 2892 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 2893 | * will return "*.\*"! |
| 2894 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2895 | gr = glob(pattern, 0, NULL, &globdata); |
| 2896 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2897 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2898 | if (gr == GLOB_NOMATCH) { |
| 2899 | globfree(&globdata); |
| 2900 | goto literal; |
| 2901 | } |
| 2902 | if (gr == GLOB_NOSPACE) |
| 2903 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2904 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2905 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2906 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2907 | } |
| 2908 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2909 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2910 | /* "forget" pattern in o */ |
| 2911 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2912 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2913 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2914 | n = o_save_ptr_helper(o, n); |
| 2915 | argv++; |
| 2916 | if (!*argv) |
| 2917 | break; |
| 2918 | } |
| 2919 | } |
| 2920 | globfree(&globdata); |
| 2921 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2922 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2923 | return n; |
| 2924 | } |
| 2925 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2926 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2927 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2928 | /* 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] | 2929 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2930 | static int o_save_ptr(o_string *o, int n) |
| 2931 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2932 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2933 | /* If o->has_empty_slot, list[n] was already globbed |
| 2934 | * (if it was requested back then when it was filled) |
| 2935 | * so don't do that again! */ |
| 2936 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2937 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2938 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2939 | return o_save_ptr_helper(o, n); |
| 2940 | } |
| 2941 | |
| 2942 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2943 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2944 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2945 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2946 | int string_start; |
| 2947 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2948 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 2949 | if (DEBUG_EXPAND) |
| 2950 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2951 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2952 | list = (char**)o->data; |
| 2953 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2954 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2955 | while (n) { |
| 2956 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2957 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2958 | } |
| 2959 | return list; |
| 2960 | } |
| 2961 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2962 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2963 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2964 | /* Returns pi->next - next pipe in the list */ |
| 2965 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2966 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2967 | struct pipe *next; |
| 2968 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2969 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2970 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2971 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2972 | struct command *command; |
| 2973 | struct redir_struct *r, *rnext; |
| 2974 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2975 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2976 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2977 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2978 | if (DEBUG_CLEAN) { |
| 2979 | int a; |
| 2980 | char **p; |
| 2981 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 2982 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 2983 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2984 | } |
| 2985 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2986 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2987 | } |
| 2988 | /* not "else if": on syntax error, we may have both! */ |
| 2989 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2990 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 2991 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2992 | free_pipe_list(command->group); |
| 2993 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2994 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2995 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 2996 | /* else is crucial here. |
| 2997 | * If group != NULL, child_func is meaningless */ |
| 2998 | #if ENABLE_HUSH_FUNCTIONS |
| 2999 | else if (command->child_func) { |
| 3000 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 3001 | command->child_func->parent_cmd = NULL; |
| 3002 | } |
| 3003 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3004 | #if !BB_MMU |
| 3005 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3006 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3007 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3008 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3009 | debug_printf_clean(" redirect %d%s", |
| 3010 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3011 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3012 | if (r->rd_filename) { |
| 3013 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3014 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3015 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3016 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3017 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3018 | rnext = r->next; |
| 3019 | free(r); |
| 3020 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3021 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3022 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3023 | 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] | 3024 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3025 | #if ENABLE_HUSH_JOB |
| 3026 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3027 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3028 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3029 | |
| 3030 | next = pi->next; |
| 3031 | free(pi); |
| 3032 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3033 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3034 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3035 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3036 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3037 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3038 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3039 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3040 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3041 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3042 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3043 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3047 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3048 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3049 | #ifndef debug_print_tree |
| 3050 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3051 | { |
| 3052 | static const char *const PIPE[] = { |
| 3053 | [PIPE_SEQ] = "SEQ", |
| 3054 | [PIPE_AND] = "AND", |
| 3055 | [PIPE_OR ] = "OR" , |
| 3056 | [PIPE_BG ] = "BG" , |
| 3057 | }; |
| 3058 | static const char *RES[] = { |
| 3059 | [RES_NONE ] = "NONE" , |
| 3060 | # if ENABLE_HUSH_IF |
| 3061 | [RES_IF ] = "IF" , |
| 3062 | [RES_THEN ] = "THEN" , |
| 3063 | [RES_ELIF ] = "ELIF" , |
| 3064 | [RES_ELSE ] = "ELSE" , |
| 3065 | [RES_FI ] = "FI" , |
| 3066 | # endif |
| 3067 | # if ENABLE_HUSH_LOOPS |
| 3068 | [RES_FOR ] = "FOR" , |
| 3069 | [RES_WHILE] = "WHILE", |
| 3070 | [RES_UNTIL] = "UNTIL", |
| 3071 | [RES_DO ] = "DO" , |
| 3072 | [RES_DONE ] = "DONE" , |
| 3073 | # endif |
| 3074 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 3075 | [RES_IN ] = "IN" , |
| 3076 | # endif |
| 3077 | # if ENABLE_HUSH_CASE |
| 3078 | [RES_CASE ] = "CASE" , |
| 3079 | [RES_CASE_IN ] = "CASE_IN" , |
| 3080 | [RES_MATCH] = "MATCH", |
| 3081 | [RES_CASE_BODY] = "CASE_BODY", |
| 3082 | [RES_ESAC ] = "ESAC" , |
| 3083 | # endif |
| 3084 | [RES_XXXX ] = "XXXX" , |
| 3085 | [RES_SNTX ] = "SNTX" , |
| 3086 | }; |
| 3087 | static const char *const CMDTYPE[] = { |
| 3088 | "{}", |
| 3089 | "()", |
| 3090 | "[noglob]", |
| 3091 | # if ENABLE_HUSH_FUNCTIONS |
| 3092 | "func()", |
| 3093 | # endif |
| 3094 | }; |
| 3095 | |
| 3096 | int pin, prn; |
| 3097 | |
| 3098 | pin = 0; |
| 3099 | while (pi) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3100 | 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] | 3101 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
| 3102 | prn = 0; |
| 3103 | while (prn < pi->num_cmds) { |
| 3104 | struct command *command = &pi->cmds[prn]; |
| 3105 | char **argv = command->argv; |
| 3106 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3107 | fdprintf(2, "%*s cmd %d assignment_cnt:%d", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3108 | lvl*2, "", prn, |
| 3109 | command->assignment_cnt); |
| 3110 | if (command->group) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3111 | fdprintf(2, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3112 | CMDTYPE[command->cmd_type], |
| 3113 | argv |
| 3114 | # if !BB_MMU |
| 3115 | , " group_as_string:", command->group_as_string |
| 3116 | # else |
| 3117 | , "", "" |
| 3118 | # endif |
| 3119 | ); |
| 3120 | debug_print_tree(command->group, lvl+1); |
| 3121 | prn++; |
| 3122 | continue; |
| 3123 | } |
| 3124 | if (argv) while (*argv) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3125 | fdprintf(2, " '%s'", *argv); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3126 | argv++; |
| 3127 | } |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3128 | fdprintf(2, "\n"); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3129 | prn++; |
| 3130 | } |
| 3131 | pi = pi->next; |
| 3132 | pin++; |
| 3133 | } |
| 3134 | } |
| 3135 | #endif /* debug_print_tree */ |
| 3136 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3137 | static struct pipe *new_pipe(void) |
| 3138 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3139 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3140 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3141 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3142 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3143 | return pi; |
| 3144 | } |
| 3145 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3146 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 3147 | * if ctx->command is NULL. |
| 3148 | * No errors possible here. |
| 3149 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3150 | static int done_command(struct parse_context *ctx) |
| 3151 | { |
| 3152 | /* The command is really already in the pipe structure, so |
| 3153 | * advance the pipe counter and make a new, null command. */ |
| 3154 | struct pipe *pi = ctx->pipe; |
| 3155 | struct command *command = ctx->command; |
| 3156 | |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3157 | #if 0 /* Instead we emit error message at run time */ |
| 3158 | if (ctx->pending_redirect) { |
| 3159 | /* For example, "cmd >" (no filename to redirect to) */ |
| 3160 | die_if_script("syntax error: %s", "invalid redirect"); |
| 3161 | ctx->pending_redirect = NULL; |
| 3162 | } |
| 3163 | #endif |
| 3164 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3165 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3166 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3167 | 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] | 3168 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3169 | } |
| 3170 | pi->num_cmds++; |
| 3171 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3172 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3173 | } else { |
| 3174 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3175 | } |
| 3176 | |
| 3177 | /* Only real trickiness here is that the uncommitted |
| 3178 | * command structure is not counted in pi->num_cmds. */ |
| 3179 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3180 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 3181 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3182 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3183 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3184 | } |
| 3185 | |
| 3186 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3187 | { |
| 3188 | int not_null; |
| 3189 | |
| 3190 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3191 | /* Close previous command */ |
| 3192 | not_null = done_command(ctx); |
| 3193 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3194 | #if HAS_KEYWORDS |
| 3195 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 3196 | ctx->ctx_inverted = 0; |
| 3197 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 3198 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3199 | |
| 3200 | /* Without this check, even just <enter> on command line generates |
| 3201 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3202 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3203 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3204 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3205 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3206 | #endif |
| 3207 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3208 | || ctx->ctx_res_w == RES_DONE |
| 3209 | || ctx->ctx_res_w == RES_FOR |
| 3210 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3211 | #endif |
| 3212 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3213 | || ctx->ctx_res_w == RES_ESAC |
| 3214 | #endif |
| 3215 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3216 | struct pipe *new_p; |
| 3217 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3218 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3219 | not_null, ctx->ctx_res_w); |
| 3220 | new_p = new_pipe(); |
| 3221 | ctx->pipe->next = new_p; |
| 3222 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3223 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3224 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3225 | * This is used to control execution. |
| 3226 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3227 | * cases where variable or value happens to match a keyword): |
| 3228 | */ |
| 3229 | #if ENABLE_HUSH_LOOPS |
| 3230 | if (ctx->ctx_res_w == RES_FOR |
| 3231 | || ctx->ctx_res_w == RES_IN) |
| 3232 | ctx->ctx_res_w = RES_NONE; |
| 3233 | #endif |
| 3234 | #if ENABLE_HUSH_CASE |
| 3235 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3236 | ctx->ctx_res_w = RES_CASE_BODY; |
| 3237 | if (ctx->ctx_res_w == RES_CASE) |
| 3238 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3239 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3240 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3241 | /* Create the memory for command, roughly: |
| 3242 | * ctx->pipe->cmds = new struct command; |
| 3243 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3244 | */ |
| 3245 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3246 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3247 | } |
| 3248 | debug_printf_parse("done_pipe return\n"); |
| 3249 | } |
| 3250 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3251 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3252 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3253 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3254 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3255 | /* Create the memory for command, roughly: |
| 3256 | * ctx->pipe->cmds = new struct command; |
| 3257 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3258 | */ |
| 3259 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3262 | /* If a reserved word is found and processed, parse context is modified |
| 3263 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3264 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3265 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3266 | struct reserved_combo { |
| 3267 | char literal[6]; |
| 3268 | unsigned char res; |
| 3269 | unsigned char assignment_flag; |
| 3270 | int flag; |
| 3271 | }; |
| 3272 | enum { |
| 3273 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3274 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3275 | FLAG_IF = (1 << RES_IF ), |
| 3276 | FLAG_THEN = (1 << RES_THEN ), |
| 3277 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3278 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3279 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3280 | # endif |
| 3281 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3282 | FLAG_FOR = (1 << RES_FOR ), |
| 3283 | FLAG_WHILE = (1 << RES_WHILE), |
| 3284 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3285 | FLAG_DO = (1 << RES_DO ), |
| 3286 | FLAG_DONE = (1 << RES_DONE ), |
| 3287 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3288 | # endif |
| 3289 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3290 | FLAG_MATCH = (1 << RES_MATCH), |
| 3291 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3292 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3293 | FLAG_START = (1 << RES_XXXX ), |
| 3294 | }; |
| 3295 | |
| 3296 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3297 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3298 | /* Mostly a list of accepted follow-up reserved words. |
| 3299 | * FLAG_END means we are done with the sequence, and are ready |
| 3300 | * to turn the compound list into a command. |
| 3301 | * FLAG_START means the word must start a new compound list. |
| 3302 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3303 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3304 | # if ENABLE_HUSH_IF |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3305 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3306 | { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START }, |
| 3307 | { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3308 | { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN }, |
| 3309 | { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI }, |
| 3310 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3311 | # endif |
| 3312 | # if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3313 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3314 | { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3315 | { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3316 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3317 | { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE }, |
| 3318 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3319 | # endif |
| 3320 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3321 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3322 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3323 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3324 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3325 | const struct reserved_combo *r; |
| 3326 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 3327 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3328 | if (strcmp(word->data, r->literal) == 0) |
| 3329 | return r; |
| 3330 | } |
| 3331 | return NULL; |
| 3332 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3333 | /* Return 0: not a keyword, 1: keyword |
| 3334 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3335 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 3336 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3337 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3338 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3339 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3340 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3341 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3342 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3343 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3344 | if (word->has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3345 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3346 | r = match_reserved_word(word); |
| 3347 | if (!r) |
| 3348 | return 0; |
| 3349 | |
| 3350 | 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] | 3351 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3352 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 3353 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3354 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3355 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3356 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3357 | if (r->flag == 0) { /* '!' */ |
| 3358 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3359 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3360 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3361 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3362 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3363 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3364 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3365 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3366 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3367 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3368 | old = xmalloc(sizeof(*old)); |
| 3369 | debug_printf_parse("push stack %p\n", old); |
| 3370 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3371 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3372 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3373 | } 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] | 3374 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3375 | ctx->ctx_res_w = RES_SNTX; |
| 3376 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3377 | } else { |
| 3378 | /* "{...} fi" is ok. "{...} if" is not |
| 3379 | * Example: |
| 3380 | * if { echo foo; } then { echo bar; } fi */ |
| 3381 | if (ctx->command->group) |
| 3382 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3383 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3384 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3385 | ctx->ctx_res_w = r->res; |
| 3386 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3387 | word->o_assignment = r->assignment_flag; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3388 | 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] | 3389 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3390 | if (ctx->old_flag & FLAG_END) { |
| 3391 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3392 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3393 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3394 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3395 | old = ctx->stack; |
| 3396 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3397 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3398 | # if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 3399 | /* At this point, the compound command's string is in |
| 3400 | * ctx->as_string... except for the leading keyword! |
| 3401 | * Consider this example: "echo a | if true; then echo a; fi" |
| 3402 | * ctx->as_string will contain "true; then echo a; fi", |
| 3403 | * with "if " remaining in old->as_string! |
| 3404 | */ |
| 3405 | { |
| 3406 | char *str; |
| 3407 | int len = old->as_string.length; |
| 3408 | /* Concatenate halves */ |
| 3409 | o_addstr(&old->as_string, ctx->as_string.data); |
| 3410 | o_free_unsafe(&ctx->as_string); |
| 3411 | /* Find where leading keyword starts in first half */ |
| 3412 | str = old->as_string.data + len; |
| 3413 | if (str > old->as_string.data) |
| 3414 | str--; /* skip whitespace after keyword */ |
| 3415 | while (str > old->as_string.data && isalpha(str[-1])) |
| 3416 | str--; |
| 3417 | /* Ugh, we're done with this horrid hack */ |
| 3418 | old->command->group_as_string = xstrdup(str); |
| 3419 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 3420 | old->command->group_as_string); |
| 3421 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3422 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3423 | *ctx = *old; /* physical copy */ |
| 3424 | free(old); |
| 3425 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3426 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3427 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3428 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3429 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3430 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3431 | * Normal return is 0. Syntax errors return 1. |
| 3432 | * Note: on return, word is reset, but not o_free'd! |
| 3433 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3434 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3435 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3436 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3437 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3438 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3439 | if (word->length == 0 && !word->has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3440 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3441 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3442 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3443 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3444 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3445 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3446 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3447 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 3448 | * "2.7 Redirection |
| 3449 | * ...the word that follows the redirection operator |
| 3450 | * shall be subjected to tilde expansion, parameter expansion, |
| 3451 | * command substitution, arithmetic expansion, and quote |
| 3452 | * removal. Pathname expansion shall not be performed |
| 3453 | * on the word by a non-interactive shell; an interactive |
| 3454 | * shell may perform it, but shall do so only when |
| 3455 | * the expansion would result in one word." |
| 3456 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3457 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3458 | /* Cater for >\file case: |
| 3459 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 3460 | * Same with heredocs: |
| 3461 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 3462 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3463 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 3464 | unbackslash(ctx->pending_redirect->rd_filename); |
| 3465 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3466 | if (word->has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3467 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 3468 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3469 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3470 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3471 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3472 | } else { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3473 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3474 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3475 | if (ctx->ctx_dsemicolon |
| 3476 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 3477 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3478 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3479 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3480 | ctx->ctx_dsemicolon = 0; |
| 3481 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3482 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3483 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3484 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3485 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3486 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3487 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3488 | # if ENABLE_HUSH_CASE |
| 3489 | && ctx->ctx_res_w != RES_CASE |
| 3490 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3491 | ) { |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3492 | int reserved = reserved_word(word, ctx); |
| 3493 | debug_printf_parse("checking for reserved-ness: %d\n", reserved); |
| 3494 | if (reserved) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3495 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3496 | debug_printf_parse("done_word return %d\n", |
| 3497 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3498 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3499 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3500 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3501 | if (strcmp(word->data, "[[") == 0) { |
| 3502 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 3503 | } |
| 3504 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3505 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3506 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3507 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3508 | if (command->group) { |
| 3509 | /* "{ echo foo; } echo bar" - bad */ |
| 3510 | syntax_error_at(word->data); |
| 3511 | debug_printf_parse("done_word return 1: syntax error, " |
| 3512 | "groups and arglists don't mix\n"); |
| 3513 | return 1; |
| 3514 | } |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3515 | |
| 3516 | /* If this word wasn't an assignment, next ones definitely |
| 3517 | * can't be assignments. Even if they look like ones. */ |
| 3518 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 3519 | && word->o_assignment != WORD_IS_KEYWORD |
| 3520 | ) { |
| 3521 | word->o_assignment = NOT_ASSIGNMENT; |
| 3522 | } else { |
| 3523 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) { |
| 3524 | command->assignment_cnt++; |
| 3525 | debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt); |
| 3526 | } |
| 3527 | debug_printf_parse("word->o_assignment was:'%s'\n", assignment_flag[word->o_assignment]); |
| 3528 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 3529 | } |
| 3530 | debug_printf_parse("word->o_assignment='%s'\n", assignment_flag[word->o_assignment]); |
| 3531 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3532 | if (word->has_quoted_part |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3533 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 3534 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3535 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3536 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3537 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3538 | char *p = word->data; |
| 3539 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 3540 | && (p[1] & 0x7f) == '@' |
| 3541 | && p[2] == SPECIAL_VAR_SYMBOL |
| 3542 | ) { |
| 3543 | p += 3; |
| 3544 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3545 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3546 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3547 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3548 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3549 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3550 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3551 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3552 | if (word->has_quoted_part |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3553 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 3554 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3555 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3556 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3557 | return 1; |
| 3558 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3559 | /* Force FOR to have just one word (variable name) */ |
| 3560 | /* NB: basically, this makes hush see "for v in ..." |
| 3561 | * syntax as if it is "for v; in ...". FOR and IN become |
| 3562 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3563 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3564 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3565 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3566 | #if ENABLE_HUSH_CASE |
| 3567 | /* Force CASE to have just one word */ |
| 3568 | if (ctx->ctx_res_w == RES_CASE) { |
| 3569 | done_pipe(ctx, PIPE_SEQ); |
| 3570 | } |
| 3571 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3572 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3573 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3574 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3575 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3576 | return 0; |
| 3577 | } |
| 3578 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3579 | |
| 3580 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 3581 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3582 | * Return: |
| 3583 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 3584 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 3585 | * REDIRFD_TO_FILE if no & was seen, |
| 3586 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3587 | */ |
| 3588 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3589 | #define parse_redir_right_fd(as_string, input) \ |
| 3590 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3591 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3592 | 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] | 3593 | { |
| 3594 | int ch, d, ok; |
| 3595 | |
| 3596 | ch = i_peek(input); |
| 3597 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3598 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3599 | |
| 3600 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3601 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3602 | ch = i_peek(input); |
| 3603 | if (ch == '-') { |
| 3604 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3605 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3606 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3607 | } |
| 3608 | d = 0; |
| 3609 | ok = 0; |
| 3610 | while (ch != EOF && isdigit(ch)) { |
| 3611 | d = d*10 + (ch-'0'); |
| 3612 | ok = 1; |
| 3613 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3614 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3615 | ch = i_peek(input); |
| 3616 | } |
| 3617 | if (ok) return d; |
| 3618 | |
| 3619 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 3620 | |
| 3621 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3622 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3625 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3626 | */ |
| 3627 | static int parse_redirect(struct parse_context *ctx, |
| 3628 | int fd, |
| 3629 | redir_type style, |
| 3630 | struct in_str *input) |
| 3631 | { |
| 3632 | struct command *command = ctx->command; |
| 3633 | struct redir_struct *redir; |
| 3634 | struct redir_struct **redirp; |
| 3635 | int dup_num; |
| 3636 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3637 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3638 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3639 | /* Check for a '>&1' type redirect */ |
| 3640 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 3641 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 3642 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3643 | } else { |
| 3644 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3645 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3646 | if (dup_num) { /* <<-... */ |
| 3647 | ch = i_getch(input); |
| 3648 | nommu_addchr(&ctx->as_string, ch); |
| 3649 | ch = i_peek(input); |
| 3650 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3651 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3652 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3653 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3654 | int ch = i_peek(input); |
| 3655 | if (ch == '|') { |
| 3656 | /* >|FILE redirect ("clobbering" >). |
| 3657 | * Since we do not support "set -o noclobber" yet, |
| 3658 | * >| and > are the same for now. Just eat |. |
| 3659 | */ |
| 3660 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3661 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3662 | } |
| 3663 | } |
| 3664 | |
| 3665 | /* Create a new redir_struct and append it to the linked list */ |
| 3666 | redirp = &command->redirects; |
| 3667 | while ((redir = *redirp) != NULL) { |
| 3668 | redirp = &(redir->next); |
| 3669 | } |
| 3670 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 3671 | /* redir->next = NULL; */ |
| 3672 | /* redir->rd_filename = NULL; */ |
| 3673 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3674 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3675 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3676 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 3677 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3678 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3679 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3680 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3681 | /* Erik had a check here that the file descriptor in question |
| 3682 | * is legit; I postpone that to "run time" |
| 3683 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3684 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 3685 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3686 | } else { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3687 | #if 0 /* Instead we emit error message at run time */ |
| 3688 | if (ctx->pending_redirect) { |
| 3689 | /* For example, "cmd > <file" */ |
| 3690 | die_if_script("syntax error: %s", "invalid redirect"); |
| 3691 | } |
| 3692 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3693 | /* Set ctx->pending_redirect, so we know what to do at the |
| 3694 | * end of the next parsed word. */ |
| 3695 | ctx->pending_redirect = redir; |
| 3696 | } |
| 3697 | return 0; |
| 3698 | } |
| 3699 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3700 | /* If a redirect is immediately preceded by a number, that number is |
| 3701 | * supposed to tell which file descriptor to redirect. This routine |
| 3702 | * looks for such preceding numbers. In an ideal world this routine |
| 3703 | * needs to handle all the following classes of redirects... |
| 3704 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3705 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3706 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3707 | * 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] | 3708 | * |
| 3709 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 3710 | * "2.7 Redirection |
| 3711 | * ... If n is quoted, the number shall not be recognized as part of |
| 3712 | * the redirection expression. For example: |
| 3713 | * echo \2>a |
| 3714 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3715 | * 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] | 3716 | * |
| 3717 | * A -1 return means no valid number was found, |
| 3718 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3719 | */ |
| 3720 | static int redirect_opt_num(o_string *o) |
| 3721 | { |
| 3722 | int num; |
| 3723 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3724 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3725 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3726 | num = bb_strtou(o->data, NULL, 10); |
| 3727 | if (errno || num < 0) |
| 3728 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3729 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3730 | return num; |
| 3731 | } |
| 3732 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3733 | #if BB_MMU |
| 3734 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 3735 | fetch_till_str(input, word, skip_tabs) |
| 3736 | #endif |
| 3737 | static char *fetch_till_str(o_string *as_string, |
| 3738 | struct in_str *input, |
| 3739 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3740 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3741 | { |
| 3742 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3743 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3744 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3745 | int ch; |
| 3746 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3747 | goto jump_in; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 3748 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3749 | while (1) { |
| 3750 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3751 | if (ch != EOF) |
| 3752 | nommu_addchr(as_string, ch); |
| 3753 | if ((ch == '\n' || ch == EOF) |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3754 | && ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') |
| 3755 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3756 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 3757 | heredoc.data[past_EOL] = '\0'; |
| 3758 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 3759 | return heredoc.data; |
| 3760 | } |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3761 | while (ch == '\n') { |
| 3762 | o_addchr(&heredoc, ch); |
| 3763 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3764 | jump_in: |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3765 | past_EOL = heredoc.length; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3766 | do { |
| 3767 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3768 | if (ch != EOF) |
| 3769 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3770 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3771 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3772 | } |
| 3773 | if (ch == EOF) { |
| 3774 | o_free_unsafe(&heredoc); |
| 3775 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3776 | } |
| 3777 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3778 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 3779 | if (prev == '\\' && ch == '\\') |
| 3780 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
| 3781 | prev = 0; /* not \ */ |
| 3782 | else |
| 3783 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3784 | } |
| 3785 | } |
| 3786 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3787 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 3788 | * and load them all. There should be exactly heredoc_cnt of them. |
| 3789 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3790 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 3791 | { |
| 3792 | struct pipe *pi = ctx->list_head; |
| 3793 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3794 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3795 | int i; |
| 3796 | struct command *cmd = pi->cmds; |
| 3797 | |
| 3798 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 3799 | pi->num_cmds, |
| 3800 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 3801 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3802 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3803 | |
| 3804 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 3805 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3806 | while (redir) { |
| 3807 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3808 | char *p; |
| 3809 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3810 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3811 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3812 | p = fetch_till_str(&ctx->as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3813 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3814 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3815 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3816 | return 1; |
| 3817 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3818 | free(redir->rd_filename); |
| 3819 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3820 | heredoc_cnt--; |
| 3821 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3822 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3823 | } |
| 3824 | cmd++; |
| 3825 | } |
| 3826 | pi = pi->next; |
| 3827 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3828 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3829 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3830 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3831 | bb_error_msg_and_die("heredoc BUG 2"); |
| 3832 | #endif |
| 3833 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3834 | } |
| 3835 | |
| 3836 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3837 | static int run_list(struct pipe *pi); |
| 3838 | #if BB_MMU |
| 3839 | #define parse_stream(pstring, input, end_trigger) \ |
| 3840 | parse_stream(input, end_trigger) |
| 3841 | #endif |
| 3842 | static struct pipe *parse_stream(char **pstring, |
| 3843 | struct in_str *input, |
| 3844 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3845 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3846 | |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 3847 | #if !ENABLE_HUSH_FUNCTIONS |
| 3848 | #define parse_group(dest, ctx, input, ch) \ |
| 3849 | parse_group(ctx, input, ch) |
| 3850 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3851 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3852 | struct in_str *input, int ch) |
| 3853 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3854 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 3855 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3856 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3857 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3858 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3859 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3860 | |
| 3861 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3862 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3863 | if (ch == '(' && !dest->has_quoted_part) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3864 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3865 | if (done_word(dest, ctx)) |
| 3866 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3867 | if (!command->argv) |
| 3868 | goto skip; /* (... */ |
| 3869 | if (command->argv[1]) { /* word word ... (... */ |
| 3870 | syntax_error_unexpected_ch('('); |
| 3871 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3872 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3873 | /* it is "word(..." or "word (..." */ |
| 3874 | do |
| 3875 | ch = i_getch(input); |
| 3876 | while (ch == ' ' || ch == '\t'); |
| 3877 | if (ch != ')') { |
| 3878 | syntax_error_unexpected_ch(ch); |
| 3879 | return 1; |
| 3880 | } |
| 3881 | nommu_addchr(&ctx->as_string, ch); |
| 3882 | do |
| 3883 | ch = i_getch(input); |
| 3884 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 3885 | if (ch != '{') { |
| 3886 | syntax_error_unexpected_ch(ch); |
| 3887 | return 1; |
| 3888 | } |
| 3889 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3890 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3891 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3892 | } |
| 3893 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3894 | |
| 3895 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3896 | if (command->argv /* word [word]{... */ |
| 3897 | || dest->length /* word{... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3898 | || dest->has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3899 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3900 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3901 | debug_printf_parse("parse_group return 1: " |
| 3902 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3903 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3904 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3905 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3906 | |
| 3907 | #if ENABLE_HUSH_FUNCTIONS |
| 3908 | skip: |
| 3909 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3910 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3911 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3912 | endch = ')'; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3913 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3914 | } else { |
| 3915 | /* bash does not allow "{echo...", requires whitespace */ |
| 3916 | ch = i_getch(input); |
| 3917 | if (ch != ' ' && ch != '\t' && ch != '\n') { |
| 3918 | syntax_error_unexpected_ch(ch); |
| 3919 | return 1; |
| 3920 | } |
| 3921 | nommu_addchr(&ctx->as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3922 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3923 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3924 | { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3925 | #if BB_MMU |
| 3926 | # define as_string NULL |
| 3927 | #else |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3928 | char *as_string = NULL; |
| 3929 | #endif |
| 3930 | pipe_list = parse_stream(&as_string, input, endch); |
| 3931 | #if !BB_MMU |
| 3932 | if (as_string) |
| 3933 | o_addstr(&ctx->as_string, as_string); |
| 3934 | #endif |
| 3935 | /* empty ()/{} or parse error? */ |
| 3936 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3937 | /* parse_stream already emitted error msg */ |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3938 | if (!BB_MMU) |
| 3939 | free(as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3940 | debug_printf_parse("parse_group return 1: " |
| 3941 | "parse_stream returned %p\n", pipe_list); |
| 3942 | return 1; |
| 3943 | } |
| 3944 | command->group = pipe_list; |
| 3945 | #if !BB_MMU |
| 3946 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 3947 | command->group_as_string = as_string; |
| 3948 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 3949 | command->group_as_string); |
| 3950 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3951 | #undef as_string |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3952 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3953 | debug_printf_parse("parse_group return 0\n"); |
| 3954 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3955 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 3958 | static int i_getch_and_eat_bkslash_nl(struct in_str *input) |
| 3959 | { |
| 3960 | for (;;) { |
| 3961 | int ch, ch2; |
| 3962 | |
| 3963 | ch = i_getch(input); |
| 3964 | if (ch != '\\') |
| 3965 | return ch; |
| 3966 | ch2 = i_peek(input); |
| 3967 | if (ch2 != '\n') |
| 3968 | return ch; |
| 3969 | /* backslash+newline, skip it */ |
| 3970 | i_getch(input); |
| 3971 | } |
| 3972 | } |
| 3973 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 3974 | static int i_peek_and_eat_bkslash_nl(struct in_str *input) |
| 3975 | { |
| 3976 | for (;;) { |
| 3977 | int ch, ch2; |
| 3978 | |
| 3979 | ch = i_peek(input); |
| 3980 | if (ch != '\\') |
| 3981 | return ch; |
| 3982 | ch2 = i_peek2(input); |
| 3983 | if (ch2 != '\n') |
| 3984 | return ch; |
| 3985 | /* backslash+newline, skip it */ |
| 3986 | i_getch(input); |
| 3987 | i_getch(input); |
| 3988 | } |
| 3989 | } |
| 3990 | |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3991 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3992 | /* Subroutines for copying $(...) and `...` things */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3993 | 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] | 3994 | /* '...' */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3995 | 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] | 3996 | { |
| 3997 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3998 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3999 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4000 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4001 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4002 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4003 | if (ch == '\'') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4004 | return 1; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4005 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4006 | } |
| 4007 | } |
| 4008 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4009 | 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] | 4010 | { |
| 4011 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4012 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4013 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4014 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4015 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4016 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4017 | if (ch == '"') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4018 | return 1; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4019 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4020 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4021 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4022 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4023 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4024 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4025 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 1)) |
| 4026 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4027 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4028 | continue; |
| 4029 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4030 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4031 | } |
| 4032 | } |
| 4033 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 4034 | * \` quoting. |
| 4035 | * "Within the backquoted style of command substitution, backslash |
| 4036 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 4037 | * The search for the matching backquote shall be satisfied by the first |
| 4038 | * backquote found without a preceding backslash; during this search, |
| 4039 | * if a non-escaped backquote is encountered within a shell comment, |
| 4040 | * a here-document, an embedded command substitution of the $(command) |
| 4041 | * form, or a quoted string, undefined results occur. A single-quoted |
| 4042 | * or double-quoted string that begins, but does not end, within the |
| 4043 | * "`...`" sequence produces undefined results." |
| 4044 | * Example Output |
| 4045 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 4046 | */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4047 | 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] | 4048 | { |
| 4049 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4050 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4051 | if (ch == '`') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4052 | return 1; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4053 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4054 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 4055 | ch = i_getch(input); |
| 4056 | if (ch != '`' |
| 4057 | && ch != '$' |
| 4058 | && ch != '\\' |
| 4059 | && (!in_dquote || ch != '"') |
| 4060 | ) { |
| 4061 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4062 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4063 | } |
| 4064 | if (ch == EOF) { |
| 4065 | syntax_error_unterm_ch('`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4066 | return 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4067 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4068 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4069 | } |
| 4070 | } |
| 4071 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 4072 | * quoting and nested ()s. |
| 4073 | * "With the $(command) style of command substitution, all characters |
| 4074 | * following the open parenthesis to the matching closing parenthesis |
| 4075 | * constitute the command. Any valid shell script can be used for command, |
| 4076 | * except a script consisting solely of redirections which produces |
| 4077 | * unspecified results." |
| 4078 | * Example Output |
| 4079 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 4080 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 4081 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4082 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4083 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4084 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4085 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 4086 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4087 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4088 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4089 | 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] | 4090 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4091 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4092 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4093 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4094 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4095 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4096 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 4097 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4098 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4099 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4100 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4101 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4102 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4103 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4104 | if (ch == end_ch IF_HUSH_BASH_COMPAT( || ch == end_char2)) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4105 | if (!dbl) |
| 4106 | break; |
| 4107 | /* we look for closing )) of $((EXPR)) */ |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4108 | if (i_peek_and_eat_bkslash_nl(input) == end_ch) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4109 | i_getch(input); /* eat second ')' */ |
| 4110 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4111 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4112 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4113 | o_addchr(dest, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4114 | if (ch == '(' || ch == '{') { |
| 4115 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4116 | if (!add_till_closing_bracket(dest, input, ch)) |
| 4117 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4118 | o_addchr(dest, ch); |
| 4119 | continue; |
| 4120 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4121 | if (ch == '\'') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4122 | if (!add_till_single_quote(dest, input)) |
| 4123 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4124 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4125 | continue; |
| 4126 | } |
| 4127 | if (ch == '"') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4128 | if (!add_till_double_quote(dest, input)) |
| 4129 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4130 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4131 | continue; |
| 4132 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4133 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4134 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 0)) |
| 4135 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4136 | o_addchr(dest, ch); |
| 4137 | continue; |
| 4138 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4139 | if (ch == '\\') { |
| 4140 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4141 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4142 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4143 | syntax_error_unterm_ch(')'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4144 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4145 | } |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4146 | #if 0 |
| 4147 | if (ch == '\n') { |
| 4148 | /* "backslash+newline", ignore both */ |
| 4149 | o_delchr(dest); /* undo insertion of '\' */ |
| 4150 | continue; |
| 4151 | } |
| 4152 | #endif |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4153 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4154 | continue; |
| 4155 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4156 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4157 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4158 | } |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4159 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4160 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4161 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4162 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4163 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 4164 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4165 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4166 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4167 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4168 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4169 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4170 | { |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4171 | 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] | 4172 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4173 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4174 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4175 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4176 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 4177 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4178 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4179 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4180 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4181 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4182 | quote_mask = 0; |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4183 | ch = i_peek_and_eat_bkslash_nl(input); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4184 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4185 | /* End of variable name reached */ |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4186 | break; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4187 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4188 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4189 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4190 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4191 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4192 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4193 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4194 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4195 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4196 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4197 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4198 | o_addchr(dest, ch | quote_mask); |
| 4199 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4200 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4201 | case '$': /* pid */ |
| 4202 | case '!': /* last bg pid */ |
| 4203 | case '?': /* last exit code */ |
| 4204 | case '#': /* number of args */ |
| 4205 | case '*': /* args */ |
| 4206 | case '@': /* args */ |
| 4207 | goto make_one_char_var; |
| 4208 | case '{': { |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 4209 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4210 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4211 | ch = i_getch(input); /* eat '{' */ |
| 4212 | nommu_addchr(as_string, ch); |
| 4213 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 4214 | ch = i_getch_and_eat_bkslash_nl(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4215 | /* It should be ${?}, or ${#var}, |
| 4216 | * or even ${?+subst} - operator acting on a special variable, |
| 4217 | * or the beginning of variable name. |
| 4218 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4219 | if (ch == EOF |
| 4220 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 4221 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4222 | bad_dollar_syntax: |
| 4223 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4224 | debug_printf_parse("parse_dollar return 0: unterminated ${name}\n"); |
| 4225 | return 0; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4226 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4227 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4228 | ch |= quote_mask; |
| 4229 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4230 | /* 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] | 4231 | * However, this regresses some of our testsuite cases |
| 4232 | * which check invalid constructs like ${%}. |
| 4233 | * Oh well... let's check that the var name part is fine... */ |
| 4234 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4235 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4236 | unsigned pos; |
| 4237 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4238 | o_addchr(dest, ch); |
| 4239 | debug_printf_parse(": '%c'\n", ch); |
| 4240 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4241 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4242 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4243 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4244 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4245 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4246 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4247 | unsigned end_ch; |
| 4248 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4249 | /* handle parameter expansions |
| 4250 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 4251 | */ |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4252 | if (!strchr(VAR_SUBST_OPS, ch)) /* ${var<bad_char>... */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4253 | goto bad_dollar_syntax; |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4254 | |
| 4255 | /* Eat everything until closing '}' (or ':') */ |
| 4256 | end_ch = '}'; |
| 4257 | if (ENABLE_HUSH_BASH_COMPAT |
| 4258 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4259 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4260 | ) { |
| 4261 | /* It's ${var:N[:M]} thing */ |
| 4262 | end_ch = '}' * 0x100 + ':'; |
| 4263 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4264 | if (ENABLE_HUSH_BASH_COMPAT |
| 4265 | && ch == '/' |
| 4266 | ) { |
| 4267 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 4268 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 4269 | i_getch(input); |
| 4270 | nommu_addchr(as_string, '/'); |
| 4271 | ch = '\\'; |
| 4272 | } |
| 4273 | end_ch = '}' * 0x100 + '/'; |
| 4274 | } |
| 4275 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4276 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4277 | if (!BB_MMU) |
| 4278 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4279 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4280 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4281 | if (last_ch == 0) /* error? */ |
| 4282 | return 0; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4283 | #else |
| 4284 | #error Simple code to only allow ${var} is not implemented |
| 4285 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4286 | if (as_string) { |
| 4287 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4288 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4289 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4290 | |
| 4291 | if (ENABLE_HUSH_BASH_COMPAT && (end_ch & 0xff00)) { |
| 4292 | /* close the first block: */ |
| 4293 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4294 | /* while parsing N from ${var:N[:M]} |
| 4295 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4296 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4297 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4298 | end_ch = '}'; |
| 4299 | goto again; |
| 4300 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4301 | /* got '}' */ |
| 4302 | if (end_ch == '}' * 0x100 + ':') { |
| 4303 | /* it's ${var:N} - emulate :999999999 */ |
| 4304 | o_addstr(dest, "999999999"); |
| 4305 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4306 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4307 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4308 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4309 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4310 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4311 | break; |
| 4312 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4313 | #if ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4314 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4315 | unsigned pos; |
| 4316 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4317 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4318 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4319 | # if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4320 | if (i_peek_and_eat_bkslash_nl(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4321 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4322 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4323 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4324 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4325 | if (!BB_MMU) |
| 4326 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4327 | if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG)) |
| 4328 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4329 | if (as_string) { |
| 4330 | o_addstr(as_string, dest->data + pos); |
| 4331 | o_addchr(as_string, ')'); |
| 4332 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4333 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4334 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4335 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4336 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4337 | # endif |
| 4338 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4339 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4340 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4341 | if (!BB_MMU) |
| 4342 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4343 | if (!add_till_closing_bracket(dest, input, ')')) |
| 4344 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4345 | if (as_string) { |
| 4346 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4347 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4348 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4349 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4350 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4351 | break; |
| 4352 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4353 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4354 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4355 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4356 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4357 | ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4358 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 4359 | ch = '_'; |
| 4360 | goto make_var; |
| 4361 | } |
| 4362 | /* else: it's $_ */ |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 4363 | /* TODO: $_ and $-: */ |
| 4364 | /* $_ Shell or shell script name; or last argument of last command |
| 4365 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 4366 | * 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] | 4367 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 4368 | default: |
| 4369 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4370 | } |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4371 | debug_printf_parse("parse_dollar return 1 (ok)\n"); |
| 4372 | return 1; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4373 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4376 | #if BB_MMU |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4377 | # if ENABLE_HUSH_BASH_COMPAT |
| 4378 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4379 | encode_string(dest, input, dquote_end, process_bkslash) |
| 4380 | # else |
| 4381 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4382 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4383 | encode_string(dest, input, dquote_end) |
| 4384 | # endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4385 | #define as_string NULL |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4386 | |
| 4387 | #else /* !MMU */ |
| 4388 | |
| 4389 | # if ENABLE_HUSH_BASH_COMPAT |
| 4390 | /* all parameters are needed, no macro tricks */ |
| 4391 | # else |
| 4392 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4393 | encode_string(as_string, dest, input, dquote_end) |
| 4394 | # endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4395 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4396 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4397 | o_string *dest, |
| 4398 | struct in_str *input, |
Denys Vlasenko | 14e289b | 2010-09-10 10:15:18 +0200 | [diff] [blame] | 4399 | int dquote_end, |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4400 | int process_bkslash) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4401 | { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4402 | #if !ENABLE_HUSH_BASH_COMPAT |
| 4403 | const int process_bkslash = 1; |
| 4404 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4405 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4406 | int next; |
| 4407 | |
| 4408 | again: |
| 4409 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4410 | if (ch != EOF) |
| 4411 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4412 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4413 | debug_printf_parse("encode_string return 1 (ok)\n"); |
| 4414 | return 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4415 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4416 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4417 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4418 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4419 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4420 | } |
| 4421 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4422 | if (ch != '\n') { |
| 4423 | next = i_peek(input); |
| 4424 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4425 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4426 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4427 | if (process_bkslash && ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4428 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4429 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4430 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4431 | } |
| 4432 | /* bash: |
| 4433 | * "The backslash retains its special meaning [in "..."] |
| 4434 | * only when followed by one of the following characters: |
| 4435 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4436 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4437 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 4438 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4439 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4440 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 4441 | ch = i_getch(input); /* eat next */ |
| 4442 | if (ch == '\n') |
| 4443 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4444 | } /* else: ch remains == '\\', and we double it below: */ |
| 4445 | 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] | 4446 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4447 | goto again; |
| 4448 | } |
| 4449 | if (ch == '$') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4450 | if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) { |
| 4451 | debug_printf_parse("encode_string return 0: " |
| 4452 | "parse_dollar returned 0 (error)\n"); |
| 4453 | return 0; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4454 | } |
| 4455 | goto again; |
| 4456 | } |
| 4457 | #if ENABLE_HUSH_TICK |
| 4458 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4459 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4460 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4461 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4462 | if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"')) |
| 4463 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4464 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4465 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4466 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4467 | } |
| 4468 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4469 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4470 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4471 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4472 | } |
| 4473 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4474 | /* |
| 4475 | * Scan input until EOF or end_trigger char. |
| 4476 | * Return a list of pipes to execute, or NULL on EOF |
| 4477 | * or if end_trigger character is met. |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4478 | * On syntax error, exit if shell is not interactive, |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4479 | * reset parsing machinery and start parsing anew, |
| 4480 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4481 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4482 | static struct pipe *parse_stream(char **pstring, |
| 4483 | struct in_str *input, |
| 4484 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4485 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4486 | struct parse_context ctx; |
| 4487 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4488 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4489 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4490 | /* 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] | 4491 | * found. When recursing, quote state is passed in via dest->o_expflags. |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4492 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4493 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 4494 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4495 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4496 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4497 | /* If very first arg is "" or '', dest.data may end up NULL. |
| 4498 | * Preventing this: */ |
| 4499 | o_addchr(&dest, '\0'); |
| 4500 | dest.length = 0; |
| 4501 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4502 | /* We used to separate words on $IFS here. This was wrong. |
| 4503 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4504 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4505 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4506 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4507 | if (MAYBE_ASSIGNMENT != 0) |
| 4508 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4509 | initialize_context(&ctx); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4510 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4511 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4512 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4513 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4514 | int ch; |
| 4515 | int next; |
| 4516 | int redir_fd; |
| 4517 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4518 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4519 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4520 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4521 | ch, ch, !!(dest.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4522 | if (ch == EOF) { |
| 4523 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4524 | |
| 4525 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4526 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4527 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4528 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4529 | /* end_trigger == '}' case errors out earlier, |
| 4530 | * checking only ')' */ |
| 4531 | if (end_trigger == ')') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4532 | syntax_error_unterm_ch('('); |
| 4533 | goto parse_error; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4534 | } |
| 4535 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4536 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4537 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4538 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4539 | o_free(&dest); |
| 4540 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4541 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4542 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4543 | /* (this makes bare "&" cmd a no-op. |
| 4544 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4545 | if (pi->num_cmds == 0 |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 4546 | IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE) |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4547 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4548 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4549 | pi = NULL; |
| 4550 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4551 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 4552 | debug_printf_parse("as_string1 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4553 | if (pstring) |
| 4554 | *pstring = ctx.as_string.data; |
| 4555 | else |
| 4556 | o_free_unsafe(&ctx.as_string); |
| 4557 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4558 | debug_leave(); |
| 4559 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4560 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4561 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4562 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4563 | |
| 4564 | next = '\0'; |
| 4565 | if (ch != '\n') |
| 4566 | next = i_peek(input); |
| 4567 | |
| 4568 | is_special = "{}<>;&|()#'" /* special outside of "str" */ |
| 4569 | "\\$\"" IF_HUSH_TICK("`"); /* always special */ |
| 4570 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 4571 | if (ctx.command->argv /* word [word]{... - non-special */ |
| 4572 | || dest.length /* word{... - non-special */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4573 | || dest.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4574 | || (next != ';' /* }; - special */ |
| 4575 | && next != ')' /* }) - special */ |
| 4576 | && next != '&' /* }& and }&& ... - special */ |
| 4577 | && next != '|' /* }|| ... - special */ |
| 4578 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 4579 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4580 | ) { |
| 4581 | /* They are not special, skip "{}" */ |
| 4582 | is_special += 2; |
| 4583 | } |
| 4584 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4585 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4586 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4587 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 4588 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4589 | o_addQchr(&dest, ch); |
| 4590 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 4591 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4592 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4593 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4594 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4595 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4596 | 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] | 4597 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4598 | continue; |
| 4599 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4600 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4601 | if (is_blank) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4602 | if (done_word(&dest, &ctx)) { |
| 4603 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 4604 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4605 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4606 | /* Is this a case when newline is simply ignored? |
| 4607 | * Some examples: |
| 4608 | * "cmd | <newline> cmd ..." |
| 4609 | * "case ... in <newline> word) ..." |
| 4610 | */ |
| 4611 | if (IS_NULL_CMD(ctx.command) |
| 4612 | && dest.length == 0 && !dest.has_quoted_part |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4613 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4614 | /* This newline can be ignored. But... |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4615 | * Without check #1, interactive shell |
| 4616 | * ignores even bare <newline>, |
| 4617 | * and shows the continuation prompt: |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4618 | * ps1_prompt$ <enter> |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4619 | * ps2> _ <=== wrong, should be ps1 |
| 4620 | * Without check #2, "cmd & <newline>" |
| 4621 | * is similarly mistreated. |
| 4622 | * (BTW, this makes "cmd & cmd" |
| 4623 | * and "cmd && cmd" non-orthogonal. |
| 4624 | * Really, ask yourself, why |
| 4625 | * "cmd && <newline>" doesn't start |
| 4626 | * cmd but waits for more input? |
| 4627 | * No reason...) |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4628 | */ |
| 4629 | struct pipe *pi = ctx.list_head; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4630 | if (pi->num_cmds != 0 /* check #1 */ |
| 4631 | && pi->followup != PIPE_BG /* check #2 */ |
| 4632 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4633 | continue; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4634 | } |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4635 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4636 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4637 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4638 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 4639 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4640 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4641 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4642 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4643 | heredoc_cnt = 0; |
| 4644 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4645 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4646 | 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] | 4647 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4648 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4649 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4650 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4651 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 4652 | |
| 4653 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 4654 | * } is an ordinary char in this case, even inside { cmd; } |
| 4655 | * Pathological example: { ""}; } should exec "}" cmd |
| 4656 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4657 | if (ch == '}') { |
| 4658 | if (!IS_NULL_CMD(ctx.command) /* cmd } */ |
| 4659 | || dest.length != 0 /* word} */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4660 | || dest.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4661 | ) { |
| 4662 | goto ordinary_char; |
| 4663 | } |
| 4664 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
| 4665 | goto skip_end_trigger; |
| 4666 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 4667 | } |
| 4668 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4669 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4670 | && (ch != ';' || heredoc_cnt == 0) |
| 4671 | #if ENABLE_HUSH_CASE |
| 4672 | && (ch != ')' |
| 4673 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4674 | || (!dest.has_quoted_part && strcmp(dest.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4675 | ) |
| 4676 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4677 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4678 | if (heredoc_cnt) { |
| 4679 | /* This is technically valid: |
| 4680 | * { cat <<HERE; }; echo Ok |
| 4681 | * heredoc |
| 4682 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4683 | * HERE |
| 4684 | * but we don't support this. |
| 4685 | * We require heredoc to be in enclosing {}/(), |
| 4686 | * if any. |
| 4687 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4688 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4689 | goto parse_error; |
| 4690 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4691 | if (done_word(&dest, &ctx)) { |
| 4692 | goto parse_error; |
| 4693 | } |
| 4694 | done_pipe(&ctx, PIPE_SEQ); |
| 4695 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4696 | 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] | 4697 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4698 | if (!HAS_KEYWORDS |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 4699 | 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] | 4700 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4701 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4702 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 4703 | debug_printf_parse("as_string2 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4704 | if (pstring) |
| 4705 | *pstring = ctx.as_string.data; |
| 4706 | else |
| 4707 | o_free_unsafe(&ctx.as_string); |
| 4708 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4709 | debug_leave(); |
| 4710 | debug_printf_parse("parse_stream return %p: " |
| 4711 | "end_trigger char found\n", |
| 4712 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4713 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4714 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4715 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4716 | skip_end_trigger: |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4717 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4718 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4719 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4720 | /* Catch <, > before deciding whether this word is |
| 4721 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 4722 | switch (ch) { |
| 4723 | case '>': |
| 4724 | redir_fd = redirect_opt_num(&dest); |
| 4725 | if (done_word(&dest, &ctx)) { |
| 4726 | goto parse_error; |
| 4727 | } |
| 4728 | redir_style = REDIRECT_OVERWRITE; |
| 4729 | if (next == '>') { |
| 4730 | redir_style = REDIRECT_APPEND; |
| 4731 | ch = i_getch(input); |
| 4732 | nommu_addchr(&ctx.as_string, ch); |
| 4733 | } |
| 4734 | #if 0 |
| 4735 | else if (next == '(') { |
| 4736 | syntax_error(">(process) not supported"); |
| 4737 | goto parse_error; |
| 4738 | } |
| 4739 | #endif |
| 4740 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 4741 | goto parse_error; |
| 4742 | continue; /* back to top of while (1) */ |
| 4743 | case '<': |
| 4744 | redir_fd = redirect_opt_num(&dest); |
| 4745 | if (done_word(&dest, &ctx)) { |
| 4746 | goto parse_error; |
| 4747 | } |
| 4748 | redir_style = REDIRECT_INPUT; |
| 4749 | if (next == '<') { |
| 4750 | redir_style = REDIRECT_HEREDOC; |
| 4751 | heredoc_cnt++; |
| 4752 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 4753 | ch = i_getch(input); |
| 4754 | nommu_addchr(&ctx.as_string, ch); |
| 4755 | } else if (next == '>') { |
| 4756 | redir_style = REDIRECT_IO; |
| 4757 | ch = i_getch(input); |
| 4758 | nommu_addchr(&ctx.as_string, ch); |
| 4759 | } |
| 4760 | #if 0 |
| 4761 | else if (next == '(') { |
| 4762 | syntax_error("<(process) not supported"); |
| 4763 | goto parse_error; |
| 4764 | } |
| 4765 | #endif |
| 4766 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 4767 | goto parse_error; |
| 4768 | continue; /* back to top of while (1) */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4769 | case '#': |
| 4770 | if (dest.length == 0 && !dest.has_quoted_part) { |
| 4771 | /* skip "#comment" */ |
| 4772 | while (1) { |
| 4773 | ch = i_peek(input); |
| 4774 | if (ch == EOF || ch == '\n') |
| 4775 | break; |
| 4776 | i_getch(input); |
| 4777 | /* note: we do not add it to &ctx.as_string */ |
| 4778 | } |
| 4779 | nommu_addchr(&ctx.as_string, '\n'); |
| 4780 | continue; /* back to top of while (1) */ |
| 4781 | } |
| 4782 | break; |
| 4783 | case '\\': |
| 4784 | if (next == '\n') { |
| 4785 | /* It's "\<newline>" */ |
| 4786 | #if !BB_MMU |
| 4787 | /* Remove trailing '\' from ctx.as_string */ |
| 4788 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
| 4789 | #endif |
| 4790 | ch = i_getch(input); /* eat it */ |
| 4791 | continue; /* back to top of while (1) */ |
| 4792 | } |
| 4793 | break; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4794 | } |
| 4795 | |
| 4796 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 4797 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 4798 | && !ctx.pending_redirect |
| 4799 | ) { |
| 4800 | /* ch is a special char and thus this word |
| 4801 | * cannot be an assignment */ |
| 4802 | dest.o_assignment = NOT_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4803 | 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] | 4804 | } |
| 4805 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 4806 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 4807 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4808 | switch (ch) { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4809 | case '#': /* non-comment #: "echo a#b" etc */ |
| 4810 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4811 | break; |
| 4812 | case '\\': |
| 4813 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4814 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4815 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4816 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4817 | ch = i_getch(input); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4818 | /* note: ch != '\n' (that case does not reach this place) */ |
| 4819 | o_addchr(&dest, '\\'); |
| 4820 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
| 4821 | o_addchr(&dest, ch); |
| 4822 | nommu_addchr(&ctx.as_string, ch); |
| 4823 | /* Example: echo Hello \2>file |
| 4824 | * we need to know that word 2 is quoted */ |
| 4825 | dest.has_quoted_part = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4826 | break; |
| 4827 | case '$': |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4828 | if (!parse_dollar(&ctx.as_string, &dest, input, /*quote_mask:*/ 0)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4829 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4830 | "parse_dollar returned 0 (error)\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4831 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4832 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4833 | break; |
| 4834 | case '\'': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4835 | dest.has_quoted_part = 1; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4836 | if (next == '\'' && !ctx.pending_redirect) { |
| 4837 | insert_empty_quoted_str_marker: |
| 4838 | nommu_addchr(&ctx.as_string, next); |
| 4839 | i_getch(input); /* eat second ' */ |
| 4840 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4841 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4842 | } else { |
| 4843 | while (1) { |
| 4844 | ch = i_getch(input); |
| 4845 | if (ch == EOF) { |
| 4846 | syntax_error_unterm_ch('\''); |
| 4847 | goto parse_error; |
| 4848 | } |
| 4849 | nommu_addchr(&ctx.as_string, ch); |
| 4850 | if (ch == '\'') |
| 4851 | break; |
| 4852 | o_addqchr(&dest, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4853 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4854 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4855 | break; |
| 4856 | case '"': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4857 | dest.has_quoted_part = 1; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4858 | if (next == '"' && !ctx.pending_redirect) |
| 4859 | goto insert_empty_quoted_str_marker; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4860 | if (dest.o_assignment == NOT_ASSIGNMENT) |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4861 | dest.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4862 | if (!encode_string(&ctx.as_string, &dest, input, '"', /*process_bkslash:*/ 1)) |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4863 | goto parse_error; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4864 | dest.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4865 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4866 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4867 | case '`': { |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 4868 | USE_FOR_NOMMU(unsigned pos;) |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4869 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4870 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4871 | o_addchr(&dest, '`'); |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 4872 | USE_FOR_NOMMU(pos = dest.length;) |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4873 | if (!add_till_backquote(&dest, input, /*in_dquote:*/ 0)) |
| 4874 | goto parse_error; |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4875 | # if !BB_MMU |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4876 | o_addstr(&ctx.as_string, dest.data + pos); |
| 4877 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4878 | # endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4879 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4880 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4881 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4882 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4883 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4884 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4885 | #if ENABLE_HUSH_CASE |
| 4886 | case_semi: |
| 4887 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4888 | if (done_word(&dest, &ctx)) { |
| 4889 | goto parse_error; |
| 4890 | } |
| 4891 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4892 | #if ENABLE_HUSH_CASE |
| 4893 | /* Eat multiple semicolons, detect |
| 4894 | * whether it means something special */ |
| 4895 | while (1) { |
| 4896 | ch = i_peek(input); |
| 4897 | if (ch != ';') |
| 4898 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4899 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4900 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4901 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4902 | ctx.ctx_dsemicolon = 1; |
| 4903 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4904 | break; |
| 4905 | } |
| 4906 | } |
| 4907 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4908 | new_cmd: |
| 4909 | /* We just finished a cmd. New one may start |
| 4910 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4911 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4912 | 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] | 4913 | break; |
| 4914 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4915 | if (done_word(&dest, &ctx)) { |
| 4916 | goto parse_error; |
| 4917 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4918 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4919 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4920 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4921 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4922 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4923 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4924 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4925 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4926 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4927 | if (done_word(&dest, &ctx)) { |
| 4928 | goto parse_error; |
| 4929 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4930 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4931 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4932 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4933 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4934 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4935 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4936 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4937 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4938 | } else { |
| 4939 | /* we could pick up a file descriptor choice here |
| 4940 | * with redirect_opt_num(), but bash doesn't do it. |
| 4941 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4942 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4943 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4944 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4945 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4946 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4947 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4948 | if (ctx.ctx_res_w == RES_MATCH |
| 4949 | && ctx.command->argv == NULL /* not (word|(... */ |
| 4950 | && dest.length == 0 /* not word(... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4951 | && dest.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4952 | ) { |
| 4953 | continue; |
| 4954 | } |
| 4955 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4956 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4957 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 4958 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4959 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4960 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4961 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4962 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4963 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4964 | goto case_semi; |
| 4965 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4966 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4967 | /* proper use of this character is caught by end_trigger: |
| 4968 | * if we see {, we call parse_group(..., end_trigger='}') |
| 4969 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4970 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4971 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4972 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4973 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4974 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4975 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4976 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4977 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4978 | parse_error: |
| 4979 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4980 | struct parse_context *pctx; |
| 4981 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4982 | |
| 4983 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 4984 | * Sample for finding leaks on syntax error recovery path. |
| 4985 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4986 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 4987 | * Samples to catch leaks at execution: |
| 4988 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 4989 | * 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] | 4990 | */ |
| 4991 | pctx = &ctx; |
| 4992 | do { |
| 4993 | /* Update pipe/command counts, |
| 4994 | * otherwise freeing may miss some */ |
| 4995 | done_pipe(pctx, PIPE_SEQ); |
| 4996 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 4997 | pctx->list_head, pctx); |
| 4998 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4999 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5000 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5001 | #if !BB_MMU |
| 5002 | o_free_unsafe(&pctx->as_string); |
| 5003 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5004 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5005 | if (pctx != &ctx) { |
| 5006 | free(pctx); |
| 5007 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5008 | IF_HAS_KEYWORDS(pctx = p2;) |
| 5009 | } while (HAS_KEYWORDS && pctx); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5010 | |
Denys Vlasenko | a439fa9 | 2011-03-30 19:11:46 +0200 | [diff] [blame] | 5011 | o_free(&dest); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5012 | G.last_exitcode = 1; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5013 | #if !BB_MMU |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5014 | if (pstring) |
| 5015 | *pstring = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5016 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5017 | debug_leave(); |
| 5018 | return ERR_PTR; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5019 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5020 | } |
| 5021 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5022 | |
| 5023 | /*** Execution routines ***/ |
| 5024 | |
| 5025 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5026 | #if !ENABLE_HUSH_BASH_COMPAT |
| 5027 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 5028 | #define expand_string_to_string(str, do_unbackslash) \ |
| 5029 | expand_string_to_string(str) |
| 5030 | #endif |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5031 | static char *expand_string_to_string(const char *str, int do_unbackslash); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5032 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5033 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5034 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5035 | |
| 5036 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 5037 | * all variable references within and returns a pointer to |
| 5038 | * a list of expanded strings, possibly with larger number |
| 5039 | * of strings. (Think VAR="a b"; echo $VAR). |
| 5040 | * This new list is allocated as a single malloc block. |
| 5041 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5042 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5043 | * Caller can deallocate entire list by single free(list). */ |
| 5044 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5045 | /* A horde of its helpers come first: */ |
| 5046 | |
| 5047 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 5048 | { |
| 5049 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5050 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5051 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5052 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 5053 | if (c == '{' || c == '}') { |
| 5054 | /* { -> \{, } -> \} */ |
| 5055 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5056 | /* And now we want to add { or } and continue: |
| 5057 | * o_addchr(o, c); |
| 5058 | * continue; |
| 5059 | * luckily, just falling throught achieves this. |
| 5060 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5061 | } |
| 5062 | #endif |
| 5063 | o_addchr(o, c); |
| 5064 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5065 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 5066 | o_addchr(o, '\\'); |
| 5067 | if (len) { |
| 5068 | len--; |
| 5069 | o_addchr(o, '\\'); |
| 5070 | o_addchr(o, *str++); |
| 5071 | } |
| 5072 | } |
| 5073 | } |
| 5074 | } |
| 5075 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5076 | /* Store given string, finalizing the word and starting new one whenever |
| 5077 | * we encounter IFS char(s). This is used for expanding variable values. |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5078 | * End-of-string does NOT finalize word: think about 'echo -$VAR-'. |
| 5079 | * Return in *ended_with_ifs: |
| 5080 | * 1 - ended with IFS char, else 0 (this includes case of empty str). |
| 5081 | */ |
| 5082 | 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] | 5083 | { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5084 | int last_is_ifs = 0; |
| 5085 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5086 | while (1) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5087 | int word_len; |
| 5088 | |
| 5089 | if (!*str) /* EOL - do not finalize word */ |
| 5090 | break; |
| 5091 | word_len = strcspn(str, G.ifs); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5092 | if (word_len) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5093 | /* We have WORD_LEN leading non-IFS chars */ |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5094 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5095 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5096 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5097 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 5098 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 5099 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5100 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5101 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5102 | /*/ Why can't we do it easier? */ |
| 5103 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 5104 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 5105 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5106 | last_is_ifs = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5107 | str += word_len; |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5108 | if (!*str) /* EOL - do not finalize word */ |
| 5109 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5110 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5111 | |
| 5112 | /* We know str here points to at least one IFS char */ |
| 5113 | last_is_ifs = 1; |
| 5114 | str += strspn(str, G.ifs); /* skip IFS chars */ |
| 5115 | if (!*str) /* EOL - do not finalize word */ |
| 5116 | break; |
| 5117 | |
| 5118 | /* Start new word... but not always! */ |
| 5119 | /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5120 | if (output->has_quoted_part |
| 5121 | /* Case "v=' a'; echo $v": |
| 5122 | * here nothing precedes the space in $v expansion, |
| 5123 | * therefore we should not finish the word |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5124 | * (IOW: if there *is* word to finalize, only then do it): |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5125 | */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5126 | || (n > 0 && output->data[output->length - 1]) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5127 | ) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5128 | o_addchr(output, '\0'); |
| 5129 | debug_print_list("expand_on_ifs", output, n); |
| 5130 | n = o_save_ptr(output, n); |
| 5131 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5132 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5133 | |
| 5134 | if (ended_with_ifs) |
| 5135 | *ended_with_ifs = last_is_ifs; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5136 | debug_print_list("expand_on_ifs[1]", output, n); |
| 5137 | return n; |
| 5138 | } |
| 5139 | |
| 5140 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 5141 | * they are in double quotes, with the exception that they are not :). |
| 5142 | * Just the rules are similar: "expand only $var and `cmd`" |
| 5143 | * |
| 5144 | * Returns malloced string. |
| 5145 | * As an optimization, we return NULL if expansion is not needed. |
| 5146 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5147 | #if !ENABLE_HUSH_BASH_COMPAT |
| 5148 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 5149 | #define encode_then_expand_string(str, process_bkslash, do_unbackslash) \ |
| 5150 | encode_then_expand_string(str) |
| 5151 | #endif |
| 5152 | 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] | 5153 | { |
| 5154 | char *exp_str; |
| 5155 | struct in_str input; |
| 5156 | o_string dest = NULL_O_STRING; |
| 5157 | |
| 5158 | if (!strchr(str, '$') |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 5159 | && !strchr(str, '\\') |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5160 | #if ENABLE_HUSH_TICK |
| 5161 | && !strchr(str, '`') |
| 5162 | #endif |
| 5163 | ) { |
| 5164 | return NULL; |
| 5165 | } |
| 5166 | |
| 5167 | /* We need to expand. Example: |
| 5168 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 5169 | */ |
| 5170 | setup_string_in_str(&input, str); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5171 | encode_string(NULL, &dest, &input, EOF, process_bkslash); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5172 | //TODO: error check (encode_string returns 0 on error)? |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5173 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5174 | exp_str = expand_string_to_string(dest.data, /*unbackslash:*/ do_unbackslash); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5175 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 5176 | o_free_unsafe(&dest); |
| 5177 | return exp_str; |
| 5178 | } |
| 5179 | |
| 5180 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5181 | 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] | 5182 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5183 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5184 | arith_t res; |
| 5185 | char *exp_str; |
| 5186 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5187 | math_state.lookupvar = get_local_var_value; |
| 5188 | math_state.setvar = set_local_var_from_halves; |
| 5189 | //math_state.endofname = endofname; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5190 | exp_str = encode_then_expand_string(arg, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5191 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5192 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5193 | if (errmsg_p) |
| 5194 | *errmsg_p = math_state.errmsg; |
| 5195 | if (math_state.errmsg) |
| 5196 | die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5197 | return res; |
| 5198 | } |
| 5199 | #endif |
| 5200 | |
| 5201 | #if ENABLE_HUSH_BASH_COMPAT |
| 5202 | /* ${var/[/]pattern[/repl]} helpers */ |
| 5203 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 5204 | { |
| 5205 | while (1) { |
| 5206 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 5207 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 5208 | if (end) { |
| 5209 | *size = end - val; |
| 5210 | return val; |
| 5211 | } |
| 5212 | if (*val == '\0') |
| 5213 | return NULL; |
| 5214 | /* Optimization: if "*pat" did not match the start of "string", |
| 5215 | * we know that "tring", "ring" etc will not match too: |
| 5216 | */ |
| 5217 | if (pattern[0] == '*') |
| 5218 | return NULL; |
| 5219 | val++; |
| 5220 | } |
| 5221 | } |
| 5222 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 5223 | { |
| 5224 | char *result = NULL; |
| 5225 | unsigned res_len = 0; |
| 5226 | unsigned repl_len = strlen(repl); |
| 5227 | |
| 5228 | while (1) { |
| 5229 | int size; |
| 5230 | char *s = strstr_pattern(val, pattern, &size); |
| 5231 | if (!s) |
| 5232 | break; |
| 5233 | |
| 5234 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
| 5235 | memcpy(result + res_len, val, s - val); |
| 5236 | res_len += s - val; |
| 5237 | strcpy(result + res_len, repl); |
| 5238 | res_len += repl_len; |
| 5239 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 5240 | |
| 5241 | val = s + size; |
| 5242 | if (exp_op == '/') |
| 5243 | break; |
| 5244 | } |
| 5245 | if (val[0] && result) { |
| 5246 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 5247 | strcpy(result + res_len, val); |
| 5248 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 5249 | } |
| 5250 | debug_printf_varexp("result:'%s'\n", result); |
| 5251 | return result; |
| 5252 | } |
| 5253 | #endif |
| 5254 | |
| 5255 | /* Helper: |
| 5256 | * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
| 5257 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5258 | 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] | 5259 | { |
| 5260 | const char *val = NULL; |
| 5261 | char *to_be_freed = NULL; |
| 5262 | char *p = *pp; |
| 5263 | char *var; |
| 5264 | char first_char; |
| 5265 | char exp_op; |
| 5266 | char exp_save = exp_save; /* for compiler */ |
| 5267 | char *exp_saveptr; /* points to expansion operator */ |
| 5268 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5269 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5270 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5271 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5272 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5273 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5274 | arg0 = arg[0]; |
| 5275 | first_char = arg[0] = arg0 & 0x7f; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5276 | exp_op = 0; |
| 5277 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5278 | if (first_char == '#' /* ${#... */ |
| 5279 | && arg[1] && !exp_saveptr /* not ${#} and not ${#<op_char>...} */ |
| 5280 | ) { |
| 5281 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5282 | var++; |
| 5283 | exp_op = 'L'; |
| 5284 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5285 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5286 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
| 5287 | && strchr(NUMERIC_SPECVARS_STR, first_char) /* 1st char is special variable */ |
| 5288 | ) { |
| 5289 | /* ${?:0}, ${#[:]%0} etc */ |
| 5290 | exp_saveptr = var + 1; |
| 5291 | } else { |
| 5292 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 5293 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 5294 | } |
| 5295 | exp_op = exp_save = *exp_saveptr; |
| 5296 | if (exp_op) { |
| 5297 | exp_word = exp_saveptr + 1; |
| 5298 | if (exp_op == ':') { |
| 5299 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5300 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5301 | if (ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5302 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5303 | ) { |
| 5304 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 5305 | exp_op = ':'; |
| 5306 | exp_word--; |
| 5307 | } |
| 5308 | } |
| 5309 | *exp_saveptr = '\0'; |
| 5310 | } /* else: it's not an expansion op, but bare ${var} */ |
| 5311 | } |
| 5312 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5313 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5314 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5315 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5316 | int n = xatoi_positive(var); |
| 5317 | if (n < G.global_argc) |
| 5318 | val = G.global_argv[n]; |
| 5319 | /* else val remains NULL: $N with too big N */ |
| 5320 | } else { |
| 5321 | switch (var[0]) { |
| 5322 | case '$': /* pid */ |
| 5323 | val = utoa(G.root_pid); |
| 5324 | break; |
| 5325 | case '!': /* bg pid */ |
| 5326 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 5327 | break; |
| 5328 | case '?': /* exitcode */ |
| 5329 | val = utoa(G.last_exitcode); |
| 5330 | break; |
| 5331 | case '#': /* argc */ |
| 5332 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 5333 | break; |
| 5334 | default: |
| 5335 | val = get_local_var_value(var); |
| 5336 | } |
| 5337 | } |
| 5338 | |
| 5339 | /* Handle any expansions */ |
| 5340 | if (exp_op == 'L') { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5341 | reinit_unicode_for_hush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5342 | debug_printf_expand("expand: length(%s)=", val); |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5343 | val = utoa(val ? unicode_strlen(val) : 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5344 | debug_printf_expand("%s\n", val); |
| 5345 | } else if (exp_op) { |
| 5346 | if (exp_op == '%' || exp_op == '#') { |
| 5347 | /* Standard-mandated substring removal ops: |
| 5348 | * ${parameter%word} - remove smallest suffix pattern |
| 5349 | * ${parameter%%word} - remove largest suffix pattern |
| 5350 | * ${parameter#word} - remove smallest prefix pattern |
| 5351 | * ${parameter##word} - remove largest prefix pattern |
| 5352 | * |
| 5353 | * Word is expanded to produce a glob pattern. |
| 5354 | * Then var's value is matched to it and matching part removed. |
| 5355 | */ |
| 5356 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5357 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5358 | char *exp_exp_word; |
| 5359 | char *loc; |
| 5360 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 5361 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5362 | exp_word++; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5363 | 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] | 5364 | if (exp_exp_word) |
| 5365 | exp_word = exp_exp_word; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5366 | /* HACK ALERT. We depend here on the fact that |
| 5367 | * G.global_argv and results of utoa and get_local_var_value |
| 5368 | * are actually in writable memory: |
| 5369 | * scan_and_match momentarily stores NULs there. */ |
| 5370 | t = (char*)val; |
| 5371 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5372 | //bb_error_msg("op:%c str:'%s' pat:'%s' res:'%s'", |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5373 | // exp_op, t, exp_word, loc); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5374 | free(exp_exp_word); |
| 5375 | if (loc) { /* match was found */ |
| 5376 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5377 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5378 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5379 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5380 | } |
| 5381 | } |
| 5382 | } |
| 5383 | #if ENABLE_HUSH_BASH_COMPAT |
| 5384 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5385 | /* It's ${var/[/]pattern[/repl]} thing. |
| 5386 | * Note that in encoded form it has TWO parts: |
| 5387 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5388 | * and if // is used, it is encoded as \: |
| 5389 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5390 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5391 | /* Empty variable always gives nothing: */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5392 | // "v=''; echo ${v/*/w}" prints "", not "w" |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5393 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5394 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5395 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5396 | * by the usual expansion rules: |
| 5397 | * >az; >bz; |
| 5398 | * v='a bz'; echo "${v/a*z/a*z}" prints "a*z" |
| 5399 | * v='a bz'; echo "${v/a*z/\z}" prints "\z" |
| 5400 | * v='a bz'; echo ${v/a*z/a*z} prints "az" |
| 5401 | * v='a bz'; echo ${v/a*z/\z} prints "z" |
| 5402 | * (note that a*z _pattern_ is never globbed!) |
| 5403 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5404 | char *pattern, *repl, *t; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5405 | pattern = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5406 | if (!pattern) |
| 5407 | pattern = xstrdup(exp_word); |
| 5408 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 5409 | *p++ = SPECIAL_VAR_SYMBOL; |
| 5410 | exp_word = p; |
| 5411 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5412 | *p = '\0'; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5413 | 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] | 5414 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 5415 | /* HACK ALERT. We depend here on the fact that |
| 5416 | * G.global_argv and results of utoa and get_local_var_value |
| 5417 | * are actually in writable memory: |
| 5418 | * replace_pattern momentarily stores NULs there. */ |
| 5419 | t = (char*)val; |
| 5420 | to_be_freed = replace_pattern(t, |
| 5421 | pattern, |
| 5422 | (repl ? repl : exp_word), |
| 5423 | exp_op); |
| 5424 | if (to_be_freed) /* at least one replace happened */ |
| 5425 | val = to_be_freed; |
| 5426 | free(pattern); |
| 5427 | free(repl); |
| 5428 | } |
| 5429 | } |
| 5430 | #endif |
| 5431 | else if (exp_op == ':') { |
| 5432 | #if ENABLE_HUSH_BASH_COMPAT && ENABLE_SH_MATH_SUPPORT |
| 5433 | /* It's ${var:N[:M]} bashism. |
| 5434 | * Note that in encoded form it has TWO parts: |
| 5435 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 5436 | */ |
| 5437 | arith_t beg, len; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5438 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5439 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5440 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 5441 | if (errmsg) |
| 5442 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5443 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 5444 | *p++ = SPECIAL_VAR_SYMBOL; |
| 5445 | exp_word = p; |
| 5446 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5447 | *p = '\0'; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5448 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 5449 | if (errmsg) |
| 5450 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5451 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5452 | if (len >= 0) { /* bash compat: len < 0 is illegal */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5453 | if (beg < 0) /* bash compat */ |
| 5454 | beg = 0; |
| 5455 | debug_printf_varexp("from val:'%s'\n", val); |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 5456 | if (len == 0 || !val || beg >= strlen(val)) { |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5457 | arith_err: |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 5458 | val = NULL; |
| 5459 | } else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5460 | /* Paranoia. What if user entered 9999999999999 |
| 5461 | * which fits in arith_t but not int? */ |
| 5462 | if (len >= INT_MAX) |
| 5463 | len = INT_MAX; |
| 5464 | val = to_be_freed = xstrndup(val + beg, len); |
| 5465 | } |
| 5466 | debug_printf_varexp("val:'%s'\n", val); |
| 5467 | } else |
| 5468 | #endif |
| 5469 | { |
| 5470 | die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 5471 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5472 | } |
| 5473 | } else { /* one of "-=+?" */ |
| 5474 | /* Standard-mandated substitution ops: |
| 5475 | * ${var?word} - indicate error if unset |
| 5476 | * If var is unset, word (or a message indicating it is unset |
| 5477 | * if word is null) is written to standard error |
| 5478 | * and the shell exits with a non-zero exit status. |
| 5479 | * Otherwise, the value of var is substituted. |
| 5480 | * ${var-word} - use default value |
| 5481 | * If var is unset, word is substituted. |
| 5482 | * ${var=word} - assign and use default value |
| 5483 | * If var is unset, word is assigned to var. |
| 5484 | * In all cases, final value of var is substituted. |
| 5485 | * ${var+word} - use alternative value |
| 5486 | * If var is unset, null is substituted. |
| 5487 | * Otherwise, word is substituted. |
| 5488 | * |
| 5489 | * Word is subjected to tilde expansion, parameter expansion, |
| 5490 | * command substitution, and arithmetic expansion. |
| 5491 | * If word is not needed, it is not expanded. |
| 5492 | * |
| 5493 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 5494 | * but also treat null var as if it is unset. |
| 5495 | */ |
| 5496 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 5497 | if (exp_op == '+') |
| 5498 | use_word = !use_word; |
| 5499 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 5500 | (exp_save == ':') ? "true" : "false", use_word); |
| 5501 | if (use_word) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5502 | 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] | 5503 | if (to_be_freed) |
| 5504 | exp_word = to_be_freed; |
| 5505 | if (exp_op == '?') { |
| 5506 | /* mimic bash message */ |
| 5507 | die_if_script("%s: %s", |
| 5508 | var, |
| 5509 | exp_word[0] ? exp_word : "parameter null or not set" |
| 5510 | ); |
| 5511 | //TODO: how interactive bash aborts expansion mid-command? |
| 5512 | } else { |
| 5513 | val = exp_word; |
| 5514 | } |
| 5515 | |
| 5516 | if (exp_op == '=') { |
| 5517 | /* ${var=[word]} or ${var:=[word]} */ |
| 5518 | if (isdigit(var[0]) || var[0] == '#') { |
| 5519 | /* mimic bash message */ |
| 5520 | die_if_script("$%s: cannot assign in this way", var); |
| 5521 | val = NULL; |
| 5522 | } else { |
| 5523 | char *new_var = xasprintf("%s=%s", var, val); |
| 5524 | set_local_var(new_var, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 5525 | } |
| 5526 | } |
| 5527 | } |
| 5528 | } /* one of "-=+?" */ |
| 5529 | |
| 5530 | *exp_saveptr = exp_save; |
| 5531 | } /* if (exp_op) */ |
| 5532 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5533 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5534 | |
| 5535 | *pp = p; |
| 5536 | *to_be_freed_pp = to_be_freed; |
| 5537 | return val; |
| 5538 | } |
| 5539 | |
| 5540 | /* Expand all variable references in given string, adding words to list[] |
| 5541 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 5542 | * to be filled). This routine is extremely tricky: has to deal with |
| 5543 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 5544 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5545 | 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] | 5546 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5547 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5548 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5549 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5550 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5551 | 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] | 5552 | char *p; |
| 5553 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5554 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 5555 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5556 | debug_print_list("expand_vars_to_list", output, n); |
| 5557 | n = o_save_ptr(output, n); |
| 5558 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 5559 | |
| 5560 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 5561 | char first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5562 | char *to_be_freed = NULL; |
| 5563 | const char *val = NULL; |
| 5564 | #if ENABLE_HUSH_TICK |
| 5565 | o_string subst_result = NULL_O_STRING; |
| 5566 | #endif |
| 5567 | #if ENABLE_SH_MATH_SUPPORT |
| 5568 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 5569 | #endif |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5570 | |
| 5571 | if (ended_in_ifs) { |
| 5572 | o_addchr(output, '\0'); |
| 5573 | n = o_save_ptr(output, n); |
| 5574 | ended_in_ifs = 0; |
| 5575 | } |
| 5576 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5577 | o_addblock(output, arg, p - arg); |
| 5578 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 5579 | arg = ++p; |
| 5580 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5581 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5582 | /* Fetch special var name (if it is indeed one of them) |
| 5583 | * and quote bit, force the bit on if singleword expansion - |
| 5584 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5585 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5586 | |
| 5587 | /* Is this variable quoted and thus expansion can't be null? |
| 5588 | * "$@" is special. Even if quoted, it can still |
| 5589 | * expand to nothing (not even an empty string), |
| 5590 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5591 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5592 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5593 | |
| 5594 | switch (first_ch & 0x7f) { |
| 5595 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 5596 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5597 | case '@': { |
| 5598 | int i; |
| 5599 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5600 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5601 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5602 | 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] | 5603 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5604 | while (G.global_argv[i]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5605 | n = expand_on_ifs(NULL, output, n, G.global_argv[i]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5606 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 5607 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 5608 | /* this argv[] is not empty and not last: |
| 5609 | * put terminating NUL, start new word */ |
| 5610 | o_addchr(output, '\0'); |
| 5611 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 5612 | n = o_save_ptr(output, n); |
| 5613 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 5614 | } |
| 5615 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5616 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5617 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5618 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5619 | if (first_ch == ('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5620 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5621 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5622 | while (1) { |
| 5623 | o_addQstr(output, G.global_argv[i]); |
| 5624 | if (++i >= G.global_argc) |
| 5625 | break; |
| 5626 | o_addchr(output, '\0'); |
| 5627 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 5628 | n = o_save_ptr(output, n); |
| 5629 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5630 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5631 | while (1) { |
| 5632 | o_addQstr(output, G.global_argv[i]); |
| 5633 | if (!G.global_argv[++i]) |
| 5634 | break; |
| 5635 | if (G.ifs[0]) |
| 5636 | o_addchr(output, G.ifs[0]); |
| 5637 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5638 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5639 | } |
| 5640 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5641 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5642 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 5643 | /* "Empty variable", used to make "" etc to not disappear */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5644 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5645 | arg++; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5646 | cant_be_null = 0x80; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5647 | break; |
| 5648 | #if ENABLE_HUSH_TICK |
| 5649 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5650 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5651 | arg++; |
| 5652 | /* Can't just stuff it into output o_string, |
| 5653 | * expanded result may need to be globbed |
| 5654 | * and $IFS-splitted */ |
| 5655 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 5656 | G.last_exitcode = process_command_subs(&subst_result, arg); |
| 5657 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
| 5658 | val = subst_result.data; |
| 5659 | goto store_val; |
| 5660 | #endif |
| 5661 | #if ENABLE_SH_MATH_SUPPORT |
| 5662 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
| 5663 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5664 | |
| 5665 | arg++; /* skip '+' */ |
| 5666 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 5667 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5668 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 5669 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 5670 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5671 | val = arith_buf; |
| 5672 | break; |
| 5673 | } |
| 5674 | #endif |
| 5675 | default: |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5676 | val = expand_one_var(&to_be_freed, arg, &p); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5677 | IF_HUSH_TICK(store_val:) |
| 5678 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5679 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 5680 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5681 | if (val && val[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5682 | n = expand_on_ifs(&ended_in_ifs, output, n, val); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5683 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5684 | } |
| 5685 | } else { /* quoted $VAR, val will be appended below */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5686 | output->has_quoted_part = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5687 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 5688 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5689 | } |
| 5690 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5691 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 5692 | |
| 5693 | if (val && val[0]) { |
| 5694 | o_addQstr(output, val); |
| 5695 | } |
| 5696 | free(to_be_freed); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5697 | |
| 5698 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 5699 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5700 | if (*p != SPECIAL_VAR_SYMBOL) |
| 5701 | *p = SPECIAL_VAR_SYMBOL; |
| 5702 | |
| 5703 | #if ENABLE_HUSH_TICK |
| 5704 | o_free(&subst_result); |
| 5705 | #endif |
| 5706 | arg = ++p; |
| 5707 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 5708 | |
| 5709 | if (arg[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5710 | if (ended_in_ifs) { |
| 5711 | o_addchr(output, '\0'); |
| 5712 | n = o_save_ptr(output, n); |
| 5713 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5714 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 5715 | /* this part is literal, and it was already pre-quoted |
| 5716 | * if needed (much earlier), do not use o_addQstr here! */ |
| 5717 | o_addstr_with_NUL(output, arg); |
| 5718 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 5719 | } 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] | 5720 | && !(cant_be_null & 0x80) /* and all vars were not quoted. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5721 | ) { |
| 5722 | n--; |
| 5723 | /* allow to reuse list[n] later without re-growth */ |
| 5724 | output->has_empty_slot = 1; |
| 5725 | } else { |
| 5726 | o_addchr(output, '\0'); |
| 5727 | } |
| 5728 | |
| 5729 | return n; |
| 5730 | } |
| 5731 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5732 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5733 | { |
| 5734 | int n; |
| 5735 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5736 | o_string output = NULL_O_STRING; |
| 5737 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5738 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5739 | |
| 5740 | n = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5741 | while (*argv) { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5742 | n = expand_vars_to_list(&output, n, *argv); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5743 | argv++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5744 | } |
| 5745 | debug_print_list("expand_variables", &output, n); |
| 5746 | |
| 5747 | /* output.data (malloced in one block) gets returned in "list" */ |
| 5748 | list = o_finalize_list(&output, n); |
| 5749 | debug_print_strings("expand_variables[1]", list); |
| 5750 | return list; |
| 5751 | } |
| 5752 | |
| 5753 | static char **expand_strvec_to_strvec(char **argv) |
| 5754 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5755 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5756 | } |
| 5757 | |
| 5758 | #if ENABLE_HUSH_BASH_COMPAT |
| 5759 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 5760 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5761 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5762 | } |
| 5763 | #endif |
| 5764 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5765 | /* Used for expansion of right hand of assignments, |
| 5766 | * $((...)), heredocs, variable espansion parts. |
| 5767 | * |
| 5768 | * NB: should NOT do globbing! |
| 5769 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 5770 | */ |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5771 | static char *expand_string_to_string(const char *str, int do_unbackslash) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5772 | { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5773 | #if !ENABLE_HUSH_BASH_COMPAT |
| 5774 | const int do_unbackslash = 1; |
| 5775 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5776 | char *argv[2], **list; |
| 5777 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5778 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5779 | /* This is generally an optimization, but it also |
| 5780 | * handles "", which otherwise trips over !list[0] check below. |
| 5781 | * (is this ever happens that we actually get str="" here?) |
| 5782 | */ |
| 5783 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 5784 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5785 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5786 | return xstrdup(str); |
| 5787 | } |
| 5788 | |
| 5789 | argv[0] = (char*)str; |
| 5790 | argv[1] = NULL; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5791 | list = expand_variables(argv, do_unbackslash |
| 5792 | ? EXP_FLAG_ESC_GLOB_CHARS | EXP_FLAG_SINGLEWORD |
| 5793 | : EXP_FLAG_SINGLEWORD |
| 5794 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5795 | if (HUSH_DEBUG) |
| 5796 | if (!list[0] || list[1]) |
| 5797 | bb_error_msg_and_die("BUG in varexp2"); |
| 5798 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 5799 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5800 | if (do_unbackslash) |
| 5801 | unbackslash((char*)list); |
| 5802 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5803 | return (char*)list; |
| 5804 | } |
| 5805 | |
| 5806 | /* Used for "eval" builtin */ |
| 5807 | static char* expand_strvec_to_string(char **argv) |
| 5808 | { |
| 5809 | char **list; |
| 5810 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5811 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5812 | /* Convert all NULs to spaces */ |
| 5813 | if (list[0]) { |
| 5814 | int n = 1; |
| 5815 | while (list[n]) { |
| 5816 | if (HUSH_DEBUG) |
| 5817 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 5818 | bb_error_msg_and_die("BUG in varexp3"); |
| 5819 | /* bash uses ' ' regardless of $IFS contents */ |
| 5820 | list[n][-1] = ' '; |
| 5821 | n++; |
| 5822 | } |
| 5823 | } |
Denys Vlasenko | 78c9c73 | 2016-09-29 01:44:17 +0200 | [diff] [blame] | 5824 | overlapping_strcpy((char*)list, list[0] ? list[0] : ""); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5825 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 5826 | return (char*)list; |
| 5827 | } |
| 5828 | |
| 5829 | static char **expand_assignments(char **argv, int count) |
| 5830 | { |
| 5831 | int i; |
| 5832 | char **p; |
| 5833 | |
| 5834 | G.expanded_assignments = p = NULL; |
| 5835 | /* Expand assignments into one string each */ |
| 5836 | for (i = 0; i < count; i++) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5837 | 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] | 5838 | } |
| 5839 | G.expanded_assignments = NULL; |
| 5840 | return p; |
| 5841 | } |
| 5842 | |
| 5843 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5844 | static void switch_off_special_sigs(unsigned mask) |
| 5845 | { |
| 5846 | unsigned sig = 0; |
| 5847 | while ((mask >>= 1) != 0) { |
| 5848 | sig++; |
| 5849 | if (!(mask & 1)) |
| 5850 | continue; |
| 5851 | if (G.traps) { |
| 5852 | if (G.traps[sig] && !G.traps[sig][0]) |
| 5853 | /* trap is '', has to remain SIG_IGN */ |
| 5854 | continue; |
| 5855 | free(G.traps[sig]); |
| 5856 | G.traps[sig] = NULL; |
| 5857 | } |
| 5858 | /* We are here only if no trap or trap was not '' */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 5859 | install_sighandler(sig, SIG_DFL); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5860 | } |
| 5861 | } |
| 5862 | |
Denys Vlasenko | b347df9 | 2011-08-09 22:49:15 +0200 | [diff] [blame] | 5863 | #if BB_MMU |
| 5864 | /* never called */ |
| 5865 | void re_execute_shell(char ***to_free, const char *s, |
| 5866 | char *g_argv0, char **g_argv, |
| 5867 | char **builtin_argv) NORETURN; |
| 5868 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5869 | static void reset_traps_to_defaults(void) |
| 5870 | { |
| 5871 | /* This function is always called in a child shell |
| 5872 | * after fork (not vfork, NOMMU doesn't use this function). |
| 5873 | */ |
| 5874 | unsigned sig; |
| 5875 | unsigned mask; |
| 5876 | |
| 5877 | /* Child shells are not interactive. |
| 5878 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 5879 | * Testcase: (while :; do :; done) + ^Z should background. |
| 5880 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 5881 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5882 | mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask; |
| 5883 | if (!G.traps && !mask) |
| 5884 | return; /* already no traps and no special sigs */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5885 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5886 | /* Switch off special sigs */ |
| 5887 | switch_off_special_sigs(mask); |
| 5888 | #if ENABLE_HUSH_JOB |
| 5889 | G_fatal_sig_mask = 0; |
| 5890 | #endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 5891 | G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 5892 | /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS |
| 5893 | * remain set in G.special_sig_mask */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5894 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5895 | if (!G.traps) |
| 5896 | return; |
| 5897 | |
| 5898 | /* Reset all sigs to default except ones with empty traps */ |
| 5899 | for (sig = 0; sig < NSIG; sig++) { |
| 5900 | if (!G.traps[sig]) |
| 5901 | continue; /* no trap: nothing to do */ |
| 5902 | if (!G.traps[sig][0]) |
| 5903 | continue; /* empty trap: has to remain SIG_IGN */ |
| 5904 | /* sig has non-empty trap, reset it: */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5905 | free(G.traps[sig]); |
| 5906 | G.traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5907 | /* There is no signal for trap 0 (EXIT) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5908 | if (sig == 0) |
| 5909 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 5910 | install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5911 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5912 | } |
| 5913 | |
| 5914 | #else /* !BB_MMU */ |
| 5915 | |
| 5916 | static void re_execute_shell(char ***to_free, const char *s, |
| 5917 | char *g_argv0, char **g_argv, |
| 5918 | char **builtin_argv) NORETURN; |
| 5919 | static void re_execute_shell(char ***to_free, const char *s, |
| 5920 | char *g_argv0, char **g_argv, |
| 5921 | char **builtin_argv) |
| 5922 | { |
| 5923 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 5924 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 5925 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 5926 | char *heredoc_argv[4]; |
| 5927 | struct variable *cur; |
| 5928 | # if ENABLE_HUSH_FUNCTIONS |
| 5929 | struct function *funcp; |
| 5930 | # endif |
| 5931 | char **argv, **pp; |
| 5932 | unsigned cnt; |
| 5933 | unsigned long long empty_trap_mask; |
| 5934 | |
| 5935 | if (!g_argv0) { /* heredoc */ |
| 5936 | argv = heredoc_argv; |
| 5937 | argv[0] = (char *) G.argv0_for_re_execing; |
| 5938 | argv[1] = (char *) "-<"; |
| 5939 | argv[2] = (char *) s; |
| 5940 | argv[3] = NULL; |
| 5941 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 5942 | goto do_exec; |
| 5943 | } |
| 5944 | |
| 5945 | cnt = 0; |
| 5946 | pp = builtin_argv; |
| 5947 | if (pp) while (*pp++) |
| 5948 | cnt++; |
| 5949 | |
| 5950 | empty_trap_mask = 0; |
| 5951 | if (G.traps) { |
| 5952 | int sig; |
| 5953 | for (sig = 1; sig < NSIG; sig++) { |
| 5954 | if (G.traps[sig] && !G.traps[sig][0]) |
| 5955 | empty_trap_mask |= 1LL << sig; |
| 5956 | } |
| 5957 | } |
| 5958 | |
| 5959 | sprintf(param_buf, NOMMU_HACK_FMT |
| 5960 | , (unsigned) G.root_pid |
| 5961 | , (unsigned) G.root_ppid |
| 5962 | , (unsigned) G.last_bg_pid |
| 5963 | , (unsigned) G.last_exitcode |
| 5964 | , cnt |
| 5965 | , empty_trap_mask |
| 5966 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 5967 | ); |
| 5968 | # undef NOMMU_HACK_FMT |
| 5969 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 5970 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 5971 | */ |
| 5972 | cnt += 6; |
| 5973 | for (cur = G.top_var; cur; cur = cur->next) { |
| 5974 | if (!cur->flg_export || cur->flg_read_only) |
| 5975 | cnt += 2; |
| 5976 | } |
| 5977 | # if ENABLE_HUSH_FUNCTIONS |
| 5978 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 5979 | cnt += 3; |
| 5980 | # endif |
| 5981 | pp = g_argv; |
| 5982 | while (*pp++) |
| 5983 | cnt++; |
| 5984 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 5985 | *pp++ = (char *) G.argv0_for_re_execing; |
| 5986 | *pp++ = param_buf; |
| 5987 | for (cur = G.top_var; cur; cur = cur->next) { |
| 5988 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 5989 | continue; |
| 5990 | if (cur->flg_read_only) { |
| 5991 | *pp++ = (char *) "-R"; |
| 5992 | *pp++ = cur->varstr; |
| 5993 | } else if (!cur->flg_export) { |
| 5994 | *pp++ = (char *) "-V"; |
| 5995 | *pp++ = cur->varstr; |
| 5996 | } |
| 5997 | } |
| 5998 | # if ENABLE_HUSH_FUNCTIONS |
| 5999 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 6000 | *pp++ = (char *) "-F"; |
| 6001 | *pp++ = funcp->name; |
| 6002 | *pp++ = funcp->body_as_string; |
| 6003 | } |
| 6004 | # endif |
| 6005 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 6006 | * |
| 6007 | * However, POSIX says that subshells reset signals with traps |
| 6008 | * to SIG_DFL. |
| 6009 | * I tested bash-3.2 and it not only does that with true subshells |
| 6010 | * of the form ( list ), but with any forked children shells. |
| 6011 | * I set trap "echo W" WINCH; and then tried: |
| 6012 | * |
| 6013 | * { echo 1; sleep 20; echo 2; } & |
| 6014 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 6015 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 6016 | * |
| 6017 | * In all these cases sending SIGWINCH to the child shell |
| 6018 | * did not run the trap. If I add trap "echo V" WINCH; |
| 6019 | * _inside_ group (just before echo 1), it works. |
| 6020 | * |
| 6021 | * 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] | 6022 | */ |
| 6023 | *pp++ = (char *) "-c"; |
| 6024 | *pp++ = (char *) s; |
| 6025 | if (builtin_argv) { |
| 6026 | while (*++builtin_argv) |
| 6027 | *pp++ = *builtin_argv; |
| 6028 | *pp++ = (char *) ""; |
| 6029 | } |
| 6030 | *pp++ = g_argv0; |
| 6031 | while (*g_argv) |
| 6032 | *pp++ = *g_argv++; |
| 6033 | /* *pp = NULL; - is already there */ |
| 6034 | pp = environ; |
| 6035 | |
| 6036 | do_exec: |
| 6037 | 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] | 6038 | /* Don't propagate SIG_IGN to the child */ |
| 6039 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6040 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6041 | execve(bb_busybox_exec_path, argv, pp); |
| 6042 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 6043 | if (argv[0][0] == '/') |
| 6044 | execve(argv[0], argv, pp); |
| 6045 | xfunc_error_retval = 127; |
| 6046 | bb_error_msg_and_die("can't re-execute the shell"); |
| 6047 | } |
| 6048 | #endif /* !BB_MMU */ |
| 6049 | |
| 6050 | |
| 6051 | static int run_and_free_list(struct pipe *pi); |
| 6052 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6053 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6054 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 6055 | * end_trigger controls how often we stop parsing |
| 6056 | * NUL: parse all, execute, return |
| 6057 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 6058 | */ |
| 6059 | 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] | 6060 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6061 | /* Why we need empty flag? |
| 6062 | * An obscure corner case "false; ``; echo $?": |
| 6063 | * empty command in `` should still set $? to 0. |
| 6064 | * But we can't just set $? to 0 at the start, |
| 6065 | * this breaks "false; echo `echo $?`" case. |
| 6066 | */ |
| 6067 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6068 | while (1) { |
| 6069 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6070 | |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 6071 | #if ENABLE_HUSH_INTERACTIVE |
| 6072 | if (end_trigger == ';') |
| 6073 | inp->promptmode = 0; /* PS1 */ |
| 6074 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6075 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 6076 | if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */ |
| 6077 | /* If we are in "big" script |
| 6078 | * (not in `cmd` or something similar)... |
| 6079 | */ |
| 6080 | if (pipe_list == ERR_PTR && end_trigger == ';') { |
| 6081 | /* Discard cached input (rest of line) */ |
| 6082 | int ch = inp->last_char; |
| 6083 | while (ch != EOF && ch != '\n') { |
| 6084 | //bb_error_msg("Discarded:'%c'", ch); |
| 6085 | ch = i_getch(inp); |
| 6086 | } |
| 6087 | /* Force prompt */ |
| 6088 | inp->p = NULL; |
| 6089 | /* This stream isn't empty */ |
| 6090 | empty = 0; |
| 6091 | continue; |
| 6092 | } |
| 6093 | if (!pipe_list && empty) |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6094 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6095 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6096 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6097 | debug_print_tree(pipe_list, 0); |
| 6098 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 6099 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6100 | empty = 0; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 6101 | if (G_flag_return_in_progress == 1) |
Denys Vlasenko | 68d5cb5 | 2011-03-24 02:50:03 +0100 | [diff] [blame] | 6102 | break; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6103 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6104 | } |
| 6105 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6106 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6107 | { |
| 6108 | struct in_str input; |
| 6109 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6110 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6111 | } |
| 6112 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6113 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6114 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6115 | struct in_str input; |
| 6116 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6117 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6118 | } |
| 6119 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6120 | #if ENABLE_HUSH_TICK |
| 6121 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
| 6122 | { |
| 6123 | pid_t pid; |
| 6124 | int channel[2]; |
| 6125 | # if !BB_MMU |
| 6126 | char **to_free = NULL; |
| 6127 | # endif |
| 6128 | |
| 6129 | xpipe(channel); |
| 6130 | pid = BB_MMU ? xfork() : xvfork(); |
| 6131 | if (pid == 0) { /* child */ |
| 6132 | disable_restore_tty_pgrp_on_exit(); |
| 6133 | /* Process substitution is not considered to be usual |
| 6134 | * 'command execution'. |
| 6135 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 6136 | */ |
| 6137 | bb_signals(0 |
| 6138 | + (1 << SIGTSTP) |
| 6139 | + (1 << SIGTTIN) |
| 6140 | + (1 << SIGTTOU) |
| 6141 | , SIG_IGN); |
| 6142 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 6143 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 6144 | xmove_fd(channel[1], 1); |
| 6145 | /* Prevent it from trying to handle ctrl-z etc */ |
| 6146 | IF_HUSH_JOB(G.run_list_level = 1;) |
| 6147 | /* Awful hack for `trap` or $(trap). |
| 6148 | * |
| 6149 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 6150 | * contains an example where "trap" is executed in a subshell: |
| 6151 | * |
| 6152 | * save_traps=$(trap) |
| 6153 | * ... |
| 6154 | * eval "$save_traps" |
| 6155 | * |
| 6156 | * Standard does not say that "trap" in subshell shall print |
| 6157 | * parent shell's traps. It only says that its output |
| 6158 | * must have suitable form, but then, in the above example |
| 6159 | * (which is not supposed to be normative), it implies that. |
| 6160 | * |
| 6161 | * bash (and probably other shell) does implement it |
| 6162 | * (traps are reset to defaults, but "trap" still shows them), |
| 6163 | * but as a result, "trap" logic is hopelessly messed up: |
| 6164 | * |
| 6165 | * # trap |
| 6166 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 6167 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 6168 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 6169 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 6170 | * trap -- 'echo Ho' SIGWINCH |
| 6171 | * # echo `(trap)` <--- in subshell in subshell - output |
| 6172 | * trap -- 'echo Ho' SIGWINCH |
| 6173 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 6174 | * trap -- 'echo Ho' SIGWINCH |
| 6175 | * |
| 6176 | * The rules when to forget and when to not forget traps |
| 6177 | * get really complex and nonsensical. |
| 6178 | * |
| 6179 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 6180 | */ |
| 6181 | s = skip_whitespace(s); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 6182 | if (is_prefixed_with(s, "trap") |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6183 | && skip_whitespace(s + 4)[0] == '\0' |
| 6184 | ) { |
| 6185 | static const char *const argv[] = { NULL, NULL }; |
| 6186 | builtin_trap((char**)argv); |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 6187 | fflush_all(); /* important */ |
| 6188 | _exit(0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6189 | } |
| 6190 | # if BB_MMU |
| 6191 | reset_traps_to_defaults(); |
| 6192 | parse_and_run_string(s); |
| 6193 | _exit(G.last_exitcode); |
| 6194 | # else |
| 6195 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 6196 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 6197 | * huge=`cat BIG` # was blocking here forever |
| 6198 | * echo OK |
| 6199 | */ |
| 6200 | re_execute_shell(&to_free, |
| 6201 | s, |
| 6202 | G.global_argv[0], |
| 6203 | G.global_argv + 1, |
| 6204 | NULL); |
| 6205 | # endif |
| 6206 | } |
| 6207 | |
| 6208 | /* parent */ |
| 6209 | *pid_p = pid; |
| 6210 | # if ENABLE_HUSH_FAST |
| 6211 | G.count_SIGCHLD++; |
| 6212 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 6213 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 6214 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6215 | # endif |
| 6216 | enable_restore_tty_pgrp_on_exit(); |
| 6217 | # if !BB_MMU |
| 6218 | free(to_free); |
| 6219 | # endif |
| 6220 | close(channel[1]); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6221 | return remember_FILE(xfdopen_for_read(channel[0])); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6222 | } |
| 6223 | |
| 6224 | /* Return code is exit status of the process that is run. */ |
| 6225 | static int process_command_subs(o_string *dest, const char *s) |
| 6226 | { |
| 6227 | FILE *fp; |
| 6228 | struct in_str pipe_str; |
| 6229 | pid_t pid; |
| 6230 | int status, ch, eol_cnt; |
| 6231 | |
| 6232 | fp = generate_stream_from_string(s, &pid); |
| 6233 | |
| 6234 | /* Now send results of command back into original context */ |
| 6235 | setup_file_in_str(&pipe_str, fp); |
| 6236 | eol_cnt = 0; |
| 6237 | while ((ch = i_getch(&pipe_str)) != EOF) { |
| 6238 | if (ch == '\n') { |
| 6239 | eol_cnt++; |
| 6240 | continue; |
| 6241 | } |
| 6242 | while (eol_cnt) { |
| 6243 | o_addchr(dest, '\n'); |
| 6244 | eol_cnt--; |
| 6245 | } |
| 6246 | o_addQchr(dest, ch); |
| 6247 | } |
| 6248 | |
| 6249 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6250 | fclose_and_forget(fp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6251 | /* We need to extract exitcode. Test case |
| 6252 | * "true; echo `sleep 1; false` $?" |
| 6253 | * should print 1 */ |
| 6254 | safe_waitpid(pid, &status, 0); |
| 6255 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 6256 | return WEXITSTATUS(status); |
| 6257 | } |
| 6258 | #endif /* ENABLE_HUSH_TICK */ |
| 6259 | |
| 6260 | |
| 6261 | static void setup_heredoc(struct redir_struct *redir) |
| 6262 | { |
| 6263 | struct fd_pair pair; |
| 6264 | pid_t pid; |
| 6265 | int len, written; |
| 6266 | /* the _body_ of heredoc (misleading field name) */ |
| 6267 | const char *heredoc = redir->rd_filename; |
| 6268 | char *expanded; |
| 6269 | #if !BB_MMU |
| 6270 | char **to_free; |
| 6271 | #endif |
| 6272 | |
| 6273 | expanded = NULL; |
| 6274 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6275 | expanded = encode_then_expand_string(heredoc, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6276 | if (expanded) |
| 6277 | heredoc = expanded; |
| 6278 | } |
| 6279 | len = strlen(heredoc); |
| 6280 | |
| 6281 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 6282 | xpiped_pair(pair); |
| 6283 | xmove_fd(pair.rd, redir->rd_fd); |
| 6284 | |
| 6285 | /* Try writing without forking. Newer kernels have |
| 6286 | * dynamically growing pipes. Must use non-blocking write! */ |
| 6287 | ndelay_on(pair.wr); |
| 6288 | while (1) { |
| 6289 | written = write(pair.wr, heredoc, len); |
| 6290 | if (written <= 0) |
| 6291 | break; |
| 6292 | len -= written; |
| 6293 | if (len == 0) { |
| 6294 | close(pair.wr); |
| 6295 | free(expanded); |
| 6296 | return; |
| 6297 | } |
| 6298 | heredoc += written; |
| 6299 | } |
| 6300 | ndelay_off(pair.wr); |
| 6301 | |
| 6302 | /* Okay, pipe buffer was not big enough */ |
| 6303 | /* Note: we must not create a stray child (bastard? :) |
| 6304 | * for the unsuspecting parent process. Child creates a grandchild |
| 6305 | * and exits before parent execs the process which consumes heredoc |
| 6306 | * (that exec happens after we return from this function) */ |
| 6307 | #if !BB_MMU |
| 6308 | to_free = NULL; |
| 6309 | #endif |
| 6310 | pid = xvfork(); |
| 6311 | if (pid == 0) { |
| 6312 | /* child */ |
| 6313 | disable_restore_tty_pgrp_on_exit(); |
| 6314 | pid = BB_MMU ? xfork() : xvfork(); |
| 6315 | if (pid != 0) |
| 6316 | _exit(0); |
| 6317 | /* grandchild */ |
| 6318 | close(redir->rd_fd); /* read side of the pipe */ |
| 6319 | #if BB_MMU |
| 6320 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 6321 | _exit(0); |
| 6322 | #else |
| 6323 | /* Delegate blocking writes to another process */ |
| 6324 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 6325 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 6326 | #endif |
| 6327 | } |
| 6328 | /* parent */ |
| 6329 | #if ENABLE_HUSH_FAST |
| 6330 | G.count_SIGCHLD++; |
| 6331 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6332 | #endif |
| 6333 | enable_restore_tty_pgrp_on_exit(); |
| 6334 | #if !BB_MMU |
| 6335 | free(to_free); |
| 6336 | #endif |
| 6337 | close(pair.wr); |
| 6338 | free(expanded); |
| 6339 | wait(NULL); /* wait till child has died */ |
| 6340 | } |
| 6341 | |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6342 | /* fd: redirect wants this fd to be used (e.g. 3>file). |
| 6343 | * Move all conflicting internally used fds, |
| 6344 | * and remember them so that we can restore them later. |
| 6345 | */ |
| 6346 | static int save_fds_on_redirect(int fd, int squirrel[3]) |
| 6347 | { |
| 6348 | if (squirrel) { |
| 6349 | /* Handle redirects of fds 0,1,2 */ |
| 6350 | |
| 6351 | /* If we collide with an already moved stdio fd... */ |
| 6352 | if (fd == squirrel[0]) { |
| 6353 | squirrel[0] = xdup_and_close(squirrel[0], F_DUPFD); |
| 6354 | return 1; |
| 6355 | } |
| 6356 | if (fd == squirrel[1]) { |
| 6357 | squirrel[1] = xdup_and_close(squirrel[1], F_DUPFD); |
| 6358 | return 1; |
| 6359 | } |
| 6360 | if (fd == squirrel[2]) { |
| 6361 | squirrel[2] = xdup_and_close(squirrel[2], F_DUPFD); |
| 6362 | return 1; |
| 6363 | } |
| 6364 | /* If we are about to redirect stdio fd, and did not yet move it... */ |
| 6365 | if (fd <= 2 && squirrel[fd] < 0) { |
| 6366 | /* We avoid taking stdio fds */ |
| 6367 | squirrel[fd] = fcntl(fd, F_DUPFD, 10); |
| 6368 | if (squirrel[fd] < 0 && errno != EBADF) |
| 6369 | xfunc_die(); |
| 6370 | return 0; /* "we did not close fd" */ |
| 6371 | } |
| 6372 | } |
| 6373 | |
| 6374 | #if ENABLE_HUSH_INTERACTIVE |
| 6375 | if (fd != 0 && fd == G.interactive_fd) { |
| 6376 | G.interactive_fd = xdup_and_close(G.interactive_fd, F_DUPFD_CLOEXEC); |
| 6377 | return 1; |
| 6378 | } |
| 6379 | #endif |
| 6380 | |
| 6381 | /* Are we called from setup_redirects(squirrel==NULL)? Two cases: |
| 6382 | * (1) Redirect in a forked child. No need to save FILEs' fds, |
| 6383 | * we aren't going to use them anymore, ok to trash. |
| 6384 | * (2) "exec 3>FILE". Bummer. We can save FILEs' fds, |
| 6385 | * but how are we doing to use them? |
| 6386 | * "fileno(fd) = new_fd" can't be done. |
| 6387 | */ |
| 6388 | if (!squirrel) |
| 6389 | return 0; |
| 6390 | |
| 6391 | return save_FILEs_on_redirect(fd); |
| 6392 | } |
| 6393 | |
| 6394 | static void restore_redirects(int squirrel[3]) |
| 6395 | { |
| 6396 | int i, fd; |
| 6397 | for (i = 0; i <= 2; i++) { |
| 6398 | fd = squirrel[i]; |
| 6399 | if (fd != -1) { |
| 6400 | /* We simply die on error */ |
| 6401 | xmove_fd(fd, i); |
| 6402 | } |
| 6403 | } |
| 6404 | |
| 6405 | /* Moved G.interactive_fd stays on new fd, not doing anything for it */ |
| 6406 | |
| 6407 | restore_redirected_FILEs(); |
| 6408 | } |
| 6409 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6410 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 6411 | * and stderr if they are redirected. */ |
| 6412 | static int setup_redirects(struct command *prog, int squirrel[]) |
| 6413 | { |
| 6414 | int openfd, mode; |
| 6415 | struct redir_struct *redir; |
| 6416 | |
| 6417 | for (redir = prog->redirects; redir; redir = redir->next) { |
| 6418 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 6419 | /* "rd_fd<<HERE" case */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6420 | save_fds_on_redirect(redir->rd_fd, squirrel); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6421 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 6422 | * of the heredoc */ |
| 6423 | debug_printf_parse("set heredoc '%s'\n", |
| 6424 | redir->rd_filename); |
| 6425 | setup_heredoc(redir); |
| 6426 | continue; |
| 6427 | } |
| 6428 | |
| 6429 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 6430 | /* "rd_fd<*>file" case (<*> is <,>,>>,<>) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6431 | char *p; |
| 6432 | if (redir->rd_filename == NULL) { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 6433 | /* |
| 6434 | * Examples: |
| 6435 | * "cmd >" (no filename) |
| 6436 | * "cmd > <file" (2nd redirect starts too early) |
| 6437 | */ |
| 6438 | die_if_script("syntax error: %s", "invalid redirect"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6439 | continue; |
| 6440 | } |
| 6441 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6442 | p = expand_string_to_string(redir->rd_filename, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6443 | openfd = open_or_warn(p, mode); |
| 6444 | free(p); |
| 6445 | if (openfd < 0) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 6446 | /* Error message from open_or_warn can be lost |
| 6447 | * if stderr has been redirected, but bash |
| 6448 | * and ash both lose it as well |
| 6449 | * (though zsh doesn't!) |
| 6450 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6451 | return 1; |
| 6452 | } |
| 6453 | } else { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 6454 | /* "rd_fd<*>rd_dup" or "rd_fd<*>-" cases */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6455 | openfd = redir->rd_dup; |
| 6456 | } |
| 6457 | |
| 6458 | if (openfd != redir->rd_fd) { |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6459 | int closed = save_fds_on_redirect(redir->rd_fd, squirrel); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6460 | if (openfd == REDIRFD_CLOSE) { |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6461 | /* "rd_fd >&-" means "close me" */ |
| 6462 | if (!closed) { |
| 6463 | /* ^^^ optimization: saving may already |
| 6464 | * have closed it. If not... */ |
| 6465 | close(redir->rd_fd); |
| 6466 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6467 | } else { |
| 6468 | xdup2(openfd, redir->rd_fd); |
| 6469 | if (redir->rd_dup == REDIRFD_TO_FILE) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6470 | /* "rd_fd > FILE" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6471 | close(openfd); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6472 | /* else: "rd_fd > rd_dup" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6473 | } |
| 6474 | } |
| 6475 | } |
| 6476 | return 0; |
| 6477 | } |
| 6478 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6479 | static char *find_in_path(const char *arg) |
| 6480 | { |
| 6481 | char *ret = NULL; |
| 6482 | const char *PATH = get_local_var_value("PATH"); |
| 6483 | |
| 6484 | if (!PATH) |
| 6485 | return NULL; |
| 6486 | |
| 6487 | while (1) { |
| 6488 | const char *end = strchrnul(PATH, ':'); |
| 6489 | int sz = end - PATH; /* must be int! */ |
| 6490 | |
| 6491 | free(ret); |
| 6492 | if (sz != 0) { |
| 6493 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 6494 | } else { |
| 6495 | /* We have xxx::yyyy in $PATH, |
| 6496 | * it means "use current dir" */ |
| 6497 | ret = xstrdup(arg); |
| 6498 | } |
| 6499 | if (access(ret, F_OK) == 0) |
| 6500 | break; |
| 6501 | |
| 6502 | if (*end == '\0') { |
| 6503 | free(ret); |
| 6504 | return NULL; |
| 6505 | } |
| 6506 | PATH = end + 1; |
| 6507 | } |
| 6508 | |
| 6509 | return ret; |
| 6510 | } |
| 6511 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6512 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6513 | const struct built_in_command *x, |
| 6514 | const struct built_in_command *end) |
| 6515 | { |
| 6516 | while (x != end) { |
| 6517 | if (strcmp(name, x->b_cmd) != 0) { |
| 6518 | x++; |
| 6519 | continue; |
| 6520 | } |
| 6521 | debug_printf_exec("found builtin '%s'\n", name); |
| 6522 | return x; |
| 6523 | } |
| 6524 | return NULL; |
| 6525 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6526 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6527 | { |
| 6528 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 6529 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6530 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6531 | { |
| 6532 | const struct built_in_command *x = find_builtin1(name); |
| 6533 | if (x) |
| 6534 | return x; |
| 6535 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 6536 | } |
| 6537 | |
| 6538 | #if ENABLE_HUSH_FUNCTIONS |
| 6539 | static struct function **find_function_slot(const char *name) |
| 6540 | { |
| 6541 | struct function **funcpp = &G.top_func; |
| 6542 | while (*funcpp) { |
| 6543 | if (strcmp(name, (*funcpp)->name) == 0) { |
| 6544 | break; |
| 6545 | } |
| 6546 | funcpp = &(*funcpp)->next; |
| 6547 | } |
| 6548 | return funcpp; |
| 6549 | } |
| 6550 | |
| 6551 | static const struct function *find_function(const char *name) |
| 6552 | { |
| 6553 | const struct function *funcp = *find_function_slot(name); |
| 6554 | if (funcp) |
| 6555 | debug_printf_exec("found function '%s'\n", name); |
| 6556 | return funcp; |
| 6557 | } |
| 6558 | |
| 6559 | /* Note: takes ownership on name ptr */ |
| 6560 | static struct function *new_function(char *name) |
| 6561 | { |
| 6562 | struct function **funcpp = find_function_slot(name); |
| 6563 | struct function *funcp = *funcpp; |
| 6564 | |
| 6565 | if (funcp != NULL) { |
| 6566 | struct command *cmd = funcp->parent_cmd; |
| 6567 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 6568 | if (!cmd) { |
| 6569 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 6570 | free(funcp->name); |
| 6571 | /* Note: if !funcp->body, do not free body_as_string! |
| 6572 | * This is a special case of "-F name body" function: |
| 6573 | * body_as_string was not malloced! */ |
| 6574 | if (funcp->body) { |
| 6575 | free_pipe_list(funcp->body); |
| 6576 | # if !BB_MMU |
| 6577 | free(funcp->body_as_string); |
| 6578 | # endif |
| 6579 | } |
| 6580 | } else { |
| 6581 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 6582 | cmd->argv[0] = funcp->name; |
| 6583 | cmd->group = funcp->body; |
| 6584 | # if !BB_MMU |
| 6585 | cmd->group_as_string = funcp->body_as_string; |
| 6586 | # endif |
| 6587 | } |
| 6588 | } else { |
| 6589 | debug_printf_exec("remembering new function '%s'\n", name); |
| 6590 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 6591 | /*funcp->next = NULL;*/ |
| 6592 | } |
| 6593 | |
| 6594 | funcp->name = name; |
| 6595 | return funcp; |
| 6596 | } |
| 6597 | |
| 6598 | static void unset_func(const char *name) |
| 6599 | { |
| 6600 | struct function **funcpp = find_function_slot(name); |
| 6601 | struct function *funcp = *funcpp; |
| 6602 | |
| 6603 | if (funcp != NULL) { |
| 6604 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 6605 | *funcpp = funcp->next; |
| 6606 | /* funcp is unlinked now, deleting it. |
| 6607 | * Note: if !funcp->body, the function was created by |
| 6608 | * "-F name body", do not free ->body_as_string |
| 6609 | * and ->name as they were not malloced. */ |
| 6610 | if (funcp->body) { |
| 6611 | free_pipe_list(funcp->body); |
| 6612 | free(funcp->name); |
| 6613 | # if !BB_MMU |
| 6614 | free(funcp->body_as_string); |
| 6615 | # endif |
| 6616 | } |
| 6617 | free(funcp); |
| 6618 | } |
| 6619 | } |
| 6620 | |
| 6621 | # if BB_MMU |
| 6622 | #define exec_function(to_free, funcp, argv) \ |
| 6623 | exec_function(funcp, argv) |
| 6624 | # endif |
| 6625 | static void exec_function(char ***to_free, |
| 6626 | const struct function *funcp, |
| 6627 | char **argv) NORETURN; |
| 6628 | static void exec_function(char ***to_free, |
| 6629 | const struct function *funcp, |
| 6630 | char **argv) |
| 6631 | { |
| 6632 | # if BB_MMU |
| 6633 | int n = 1; |
| 6634 | |
| 6635 | argv[0] = G.global_argv[0]; |
| 6636 | G.global_argv = argv; |
| 6637 | while (*++argv) |
| 6638 | n++; |
| 6639 | G.global_argc = n; |
| 6640 | /* On MMU, funcp->body is always non-NULL */ |
| 6641 | n = run_list(funcp->body); |
| 6642 | fflush_all(); |
| 6643 | _exit(n); |
| 6644 | # else |
| 6645 | re_execute_shell(to_free, |
| 6646 | funcp->body_as_string, |
| 6647 | G.global_argv[0], |
| 6648 | argv + 1, |
| 6649 | NULL); |
| 6650 | # endif |
| 6651 | } |
| 6652 | |
| 6653 | static int run_function(const struct function *funcp, char **argv) |
| 6654 | { |
| 6655 | int rc; |
| 6656 | save_arg_t sv; |
| 6657 | smallint sv_flg; |
| 6658 | |
| 6659 | save_and_replace_G_args(&sv, argv); |
| 6660 | |
| 6661 | /* "we are in function, ok to use return" */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 6662 | sv_flg = G_flag_return_in_progress; |
| 6663 | G_flag_return_in_progress = -1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6664 | # if ENABLE_HUSH_LOCAL |
| 6665 | G.func_nest_level++; |
| 6666 | # endif |
| 6667 | |
| 6668 | /* On MMU, funcp->body is always non-NULL */ |
| 6669 | # if !BB_MMU |
| 6670 | if (!funcp->body) { |
| 6671 | /* Function defined by -F */ |
| 6672 | parse_and_run_string(funcp->body_as_string); |
| 6673 | rc = G.last_exitcode; |
| 6674 | } else |
| 6675 | # endif |
| 6676 | { |
| 6677 | rc = run_list(funcp->body); |
| 6678 | } |
| 6679 | |
| 6680 | # if ENABLE_HUSH_LOCAL |
| 6681 | { |
| 6682 | struct variable *var; |
| 6683 | struct variable **var_pp; |
| 6684 | |
| 6685 | var_pp = &G.top_var; |
| 6686 | while ((var = *var_pp) != NULL) { |
| 6687 | if (var->func_nest_level < G.func_nest_level) { |
| 6688 | var_pp = &var->next; |
| 6689 | continue; |
| 6690 | } |
| 6691 | /* Unexport */ |
| 6692 | if (var->flg_export) |
| 6693 | bb_unsetenv(var->varstr); |
| 6694 | /* Remove from global list */ |
| 6695 | *var_pp = var->next; |
| 6696 | /* Free */ |
| 6697 | if (!var->max_len) |
| 6698 | free(var->varstr); |
| 6699 | free(var); |
| 6700 | } |
| 6701 | G.func_nest_level--; |
| 6702 | } |
| 6703 | # endif |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 6704 | G_flag_return_in_progress = sv_flg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6705 | |
| 6706 | restore_G_args(&sv, argv); |
| 6707 | |
| 6708 | return rc; |
| 6709 | } |
| 6710 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 6711 | |
| 6712 | |
| 6713 | #if BB_MMU |
| 6714 | #define exec_builtin(to_free, x, argv) \ |
| 6715 | exec_builtin(x, argv) |
| 6716 | #else |
| 6717 | #define exec_builtin(to_free, x, argv) \ |
| 6718 | exec_builtin(to_free, argv) |
| 6719 | #endif |
| 6720 | static void exec_builtin(char ***to_free, |
| 6721 | const struct built_in_command *x, |
| 6722 | char **argv) NORETURN; |
| 6723 | static void exec_builtin(char ***to_free, |
| 6724 | const struct built_in_command *x, |
| 6725 | char **argv) |
| 6726 | { |
| 6727 | #if BB_MMU |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 6728 | int rcode; |
| 6729 | fflush_all(); |
| 6730 | rcode = x->b_function(argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6731 | fflush_all(); |
| 6732 | _exit(rcode); |
| 6733 | #else |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 6734 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6735 | /* On NOMMU, we must never block! |
| 6736 | * Example: { sleep 99 | read line; } & echo Ok |
| 6737 | */ |
| 6738 | re_execute_shell(to_free, |
| 6739 | argv[0], |
| 6740 | G.global_argv[0], |
| 6741 | G.global_argv + 1, |
| 6742 | argv); |
| 6743 | #endif |
| 6744 | } |
| 6745 | |
| 6746 | |
| 6747 | static void execvp_or_die(char **argv) NORETURN; |
| 6748 | static void execvp_or_die(char **argv) |
| 6749 | { |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 6750 | int e; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6751 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 6752 | /* Don't propagate SIG_IGN to the child */ |
| 6753 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6754 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6755 | execvp(argv[0], argv); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 6756 | e = 2; |
| 6757 | if (errno == EACCES) e = 126; |
| 6758 | if (errno == ENOENT) e = 127; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6759 | bb_perror_msg("can't execute '%s'", argv[0]); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 6760 | _exit(e); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6761 | } |
| 6762 | |
| 6763 | #if ENABLE_HUSH_MODE_X |
| 6764 | static void dump_cmd_in_x_mode(char **argv) |
| 6765 | { |
| 6766 | if (G_x_mode && argv) { |
| 6767 | /* We want to output the line in one write op */ |
| 6768 | char *buf, *p; |
| 6769 | int len; |
| 6770 | int n; |
| 6771 | |
| 6772 | len = 3; |
| 6773 | n = 0; |
| 6774 | while (argv[n]) |
| 6775 | len += strlen(argv[n++]) + 1; |
| 6776 | buf = xmalloc(len); |
| 6777 | buf[0] = '+'; |
| 6778 | p = buf + 1; |
| 6779 | n = 0; |
| 6780 | while (argv[n]) |
| 6781 | p += sprintf(p, " %s", argv[n++]); |
| 6782 | *p++ = '\n'; |
| 6783 | *p = '\0'; |
| 6784 | fputs(buf, stderr); |
| 6785 | free(buf); |
| 6786 | } |
| 6787 | } |
| 6788 | #else |
| 6789 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 6790 | #endif |
| 6791 | |
| 6792 | #if BB_MMU |
| 6793 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 6794 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 6795 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 6796 | pseudo_exec(command, argv_expanded) |
| 6797 | #endif |
| 6798 | |
| 6799 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 6800 | * Never returns. |
| 6801 | * Don't exit() here. If you don't exec, use _exit instead. |
| 6802 | * The at_exit handlers apparently confuse the calling process, |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 6803 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 6804 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6805 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 6806 | char **argv, int assignment_cnt, |
| 6807 | char **argv_expanded) NORETURN; |
| 6808 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 6809 | char **argv, int assignment_cnt, |
| 6810 | char **argv_expanded) |
| 6811 | { |
| 6812 | char **new_env; |
| 6813 | |
| 6814 | new_env = expand_assignments(argv, assignment_cnt); |
| 6815 | dump_cmd_in_x_mode(new_env); |
| 6816 | |
| 6817 | if (!argv[assignment_cnt]) { |
| 6818 | /* Case when we are here: ... | var=val | ... |
| 6819 | * (note that we do not exit early, i.e., do not optimize out |
| 6820 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 6821 | */ |
| 6822 | free_strings(new_env); |
| 6823 | _exit(EXIT_SUCCESS); |
| 6824 | } |
| 6825 | |
| 6826 | #if BB_MMU |
| 6827 | set_vars_and_save_old(new_env); |
| 6828 | free(new_env); /* optional */ |
| 6829 | /* we can also destroy set_vars_and_save_old's return value, |
| 6830 | * to save memory */ |
| 6831 | #else |
| 6832 | nommu_save->new_env = new_env; |
| 6833 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
| 6834 | #endif |
| 6835 | |
| 6836 | if (argv_expanded) { |
| 6837 | argv = argv_expanded; |
| 6838 | } else { |
| 6839 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 6840 | #if !BB_MMU |
| 6841 | nommu_save->argv = argv; |
| 6842 | #endif |
| 6843 | } |
| 6844 | dump_cmd_in_x_mode(argv); |
| 6845 | |
| 6846 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 6847 | if (strchr(argv[0], '/') != NULL) |
| 6848 | goto skip; |
| 6849 | #endif |
| 6850 | |
| 6851 | /* Check if the command matches any of the builtins. |
| 6852 | * Depending on context, this might be redundant. But it's |
| 6853 | * easier to waste a few CPU cycles than it is to figure out |
| 6854 | * if this is one of those cases. |
| 6855 | */ |
| 6856 | { |
| 6857 | /* On NOMMU, it is more expensive to re-execute shell |
| 6858 | * just in order to run echo or test builtin. |
| 6859 | * It's better to skip it here and run corresponding |
| 6860 | * non-builtin later. */ |
| 6861 | const struct built_in_command *x; |
| 6862 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 6863 | if (x) { |
| 6864 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
| 6865 | } |
| 6866 | } |
| 6867 | #if ENABLE_HUSH_FUNCTIONS |
| 6868 | /* Check if the command matches any functions */ |
| 6869 | { |
| 6870 | const struct function *funcp = find_function(argv[0]); |
| 6871 | if (funcp) { |
| 6872 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
| 6873 | } |
| 6874 | } |
| 6875 | #endif |
| 6876 | |
| 6877 | #if ENABLE_FEATURE_SH_STANDALONE |
| 6878 | /* Check if the command matches any busybox applets */ |
| 6879 | { |
| 6880 | int a = find_applet_by_name(argv[0]); |
| 6881 | if (a >= 0) { |
| 6882 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 6883 | if (APPLET_IS_NOEXEC(a)) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6884 | /* Do not leak open fds from opened script files etc */ |
| 6885 | close_all_FILE_list(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6886 | debug_printf_exec("running applet '%s'\n", argv[0]); |
| 6887 | run_applet_no_and_exit(a, argv); |
| 6888 | } |
| 6889 | # endif |
| 6890 | /* Re-exec ourselves */ |
| 6891 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 6892 | /* Don't propagate SIG_IGN to the child */ |
| 6893 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6894 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6895 | execv(bb_busybox_exec_path, argv); |
| 6896 | /* If they called chroot or otherwise made the binary no longer |
| 6897 | * executable, fall through */ |
| 6898 | } |
| 6899 | } |
| 6900 | #endif |
| 6901 | |
| 6902 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 6903 | skip: |
| 6904 | #endif |
| 6905 | execvp_or_die(argv); |
| 6906 | } |
| 6907 | |
| 6908 | /* Called after [v]fork() in run_pipe |
| 6909 | */ |
| 6910 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 6911 | struct command *command, |
| 6912 | char **argv_expanded) NORETURN; |
| 6913 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 6914 | struct command *command, |
| 6915 | char **argv_expanded) |
| 6916 | { |
| 6917 | if (command->argv) { |
| 6918 | pseudo_exec_argv(nommu_save, command->argv, |
| 6919 | command->assignment_cnt, argv_expanded); |
| 6920 | } |
| 6921 | |
| 6922 | if (command->group) { |
| 6923 | /* Cases when we are here: |
| 6924 | * ( list ) |
| 6925 | * { list } & |
| 6926 | * ... | ( list ) | ... |
| 6927 | * ... | { list } | ... |
| 6928 | */ |
| 6929 | #if BB_MMU |
| 6930 | int rcode; |
| 6931 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 6932 | reset_traps_to_defaults(); |
| 6933 | rcode = run_list(command->group); |
| 6934 | /* OK to leak memory by not calling free_pipe_list, |
| 6935 | * since this process is about to exit */ |
| 6936 | _exit(rcode); |
| 6937 | #else |
| 6938 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 6939 | command->group_as_string, |
| 6940 | G.global_argv[0], |
| 6941 | G.global_argv + 1, |
| 6942 | NULL); |
| 6943 | #endif |
| 6944 | } |
| 6945 | |
| 6946 | /* Case when we are here: ... | >file */ |
| 6947 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 6948 | _exit(EXIT_SUCCESS); |
| 6949 | } |
| 6950 | |
| 6951 | #if ENABLE_HUSH_JOB |
| 6952 | static const char *get_cmdtext(struct pipe *pi) |
| 6953 | { |
| 6954 | char **argv; |
| 6955 | char *p; |
| 6956 | int len; |
| 6957 | |
| 6958 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 6959 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 6960 | * On subsequent bg argv is trashed, but we won't use it */ |
| 6961 | if (pi->cmdtext) |
| 6962 | return pi->cmdtext; |
| 6963 | argv = pi->cmds[0].argv; |
| 6964 | if (!argv || !argv[0]) { |
| 6965 | pi->cmdtext = xzalloc(1); |
| 6966 | return pi->cmdtext; |
| 6967 | } |
| 6968 | |
| 6969 | len = 0; |
| 6970 | do { |
| 6971 | len += strlen(*argv) + 1; |
| 6972 | } while (*++argv); |
| 6973 | p = xmalloc(len); |
| 6974 | pi->cmdtext = p; |
| 6975 | argv = pi->cmds[0].argv; |
| 6976 | do { |
| 6977 | len = strlen(*argv); |
| 6978 | memcpy(p, *argv, len); |
| 6979 | p += len; |
| 6980 | *p++ = ' '; |
| 6981 | } while (*++argv); |
| 6982 | p[-1] = '\0'; |
| 6983 | return pi->cmdtext; |
| 6984 | } |
| 6985 | |
| 6986 | static void insert_bg_job(struct pipe *pi) |
| 6987 | { |
| 6988 | struct pipe *job, **jobp; |
| 6989 | int i; |
| 6990 | |
| 6991 | /* Linear search for the ID of the job to use */ |
| 6992 | pi->jobid = 1; |
| 6993 | for (job = G.job_list; job; job = job->next) |
| 6994 | if (job->jobid >= pi->jobid) |
| 6995 | pi->jobid = job->jobid + 1; |
| 6996 | |
| 6997 | /* Add job to the list of running jobs */ |
| 6998 | jobp = &G.job_list; |
| 6999 | while ((job = *jobp) != NULL) |
| 7000 | jobp = &job->next; |
| 7001 | job = *jobp = xmalloc(sizeof(*job)); |
| 7002 | |
| 7003 | *job = *pi; /* physical copy */ |
| 7004 | job->next = NULL; |
| 7005 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 7006 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 7007 | for (i = 0; i < pi->num_cmds; i++) { |
| 7008 | job->cmds[i].pid = pi->cmds[i].pid; |
| 7009 | /* all other fields are not used and stay zero */ |
| 7010 | } |
| 7011 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 7012 | |
| 7013 | if (G_interactive_fd) |
| 7014 | printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext); |
| 7015 | G.last_jobid = job->jobid; |
| 7016 | } |
| 7017 | |
| 7018 | static void remove_bg_job(struct pipe *pi) |
| 7019 | { |
| 7020 | struct pipe *prev_pipe; |
| 7021 | |
| 7022 | if (pi == G.job_list) { |
| 7023 | G.job_list = pi->next; |
| 7024 | } else { |
| 7025 | prev_pipe = G.job_list; |
| 7026 | while (prev_pipe->next != pi) |
| 7027 | prev_pipe = prev_pipe->next; |
| 7028 | prev_pipe->next = pi->next; |
| 7029 | } |
| 7030 | if (G.job_list) |
| 7031 | G.last_jobid = G.job_list->jobid; |
| 7032 | else |
| 7033 | G.last_jobid = 0; |
| 7034 | } |
| 7035 | |
| 7036 | /* Remove a backgrounded job */ |
| 7037 | static void delete_finished_bg_job(struct pipe *pi) |
| 7038 | { |
| 7039 | remove_bg_job(pi); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7040 | free_pipe(pi); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7041 | } |
| 7042 | #endif /* JOB */ |
| 7043 | |
| 7044 | /* Check to see if any processes have exited -- if they |
| 7045 | * have, figure out why and see if a job has completed */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 7046 | static int checkjobs(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7047 | { |
| 7048 | int attributes; |
| 7049 | int status; |
| 7050 | #if ENABLE_HUSH_JOB |
| 7051 | struct pipe *pi; |
| 7052 | #endif |
| 7053 | pid_t childpid; |
| 7054 | int rcode = 0; |
| 7055 | |
| 7056 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 7057 | |
| 7058 | attributes = WUNTRACED; |
| 7059 | if (fg_pipe == NULL) |
| 7060 | attributes |= WNOHANG; |
| 7061 | |
| 7062 | errno = 0; |
| 7063 | #if ENABLE_HUSH_FAST |
| 7064 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 7065 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 7066 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 7067 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 7068 | /* Avoid doing waitpid syscall if possible */ |
| 7069 | if (!G.we_have_children) { |
| 7070 | errno = ECHILD; |
| 7071 | return -1; |
| 7072 | } |
| 7073 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 7074 | /* We have children, but they did not exit |
| 7075 | * or stop yet (we saw no SIGCHLD) */ |
| 7076 | return 0; |
| 7077 | } |
| 7078 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 7079 | } |
| 7080 | #endif |
| 7081 | |
| 7082 | /* Do we do this right? |
| 7083 | * bash-3.00# sleep 20 | false |
| 7084 | * <ctrl-Z pressed> |
| 7085 | * [3]+ Stopped sleep 20 | false |
| 7086 | * bash-3.00# echo $? |
| 7087 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 7088 | * [hush 1.14.0: yes we do it right] |
| 7089 | */ |
| 7090 | wait_more: |
| 7091 | while (1) { |
| 7092 | int i; |
| 7093 | int dead; |
| 7094 | |
| 7095 | #if ENABLE_HUSH_FAST |
| 7096 | i = G.count_SIGCHLD; |
| 7097 | #endif |
| 7098 | childpid = waitpid(-1, &status, attributes); |
| 7099 | if (childpid <= 0) { |
| 7100 | if (childpid && errno != ECHILD) |
| 7101 | bb_perror_msg("waitpid"); |
| 7102 | #if ENABLE_HUSH_FAST |
| 7103 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 7104 | G.we_have_children = (childpid == 0); |
| 7105 | G.handled_SIGCHLD = i; |
| 7106 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7107 | } |
| 7108 | #endif |
| 7109 | break; |
| 7110 | } |
| 7111 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 7112 | |
| 7113 | #if DEBUG_JOBS |
| 7114 | if (WIFSTOPPED(status)) |
| 7115 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 7116 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 7117 | if (WIFSIGNALED(status)) |
| 7118 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 7119 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 7120 | if (WIFEXITED(status)) |
| 7121 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 7122 | childpid, WEXITSTATUS(status)); |
| 7123 | #endif |
| 7124 | /* Were we asked to wait for fg pipe? */ |
| 7125 | if (fg_pipe) { |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 7126 | i = fg_pipe->num_cmds; |
| 7127 | while (--i >= 0) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7128 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 7129 | if (fg_pipe->cmds[i].pid != childpid) |
| 7130 | continue; |
| 7131 | if (dead) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7132 | int ex; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7133 | fg_pipe->cmds[i].pid = 0; |
| 7134 | fg_pipe->alive_cmds--; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7135 | ex = WEXITSTATUS(status); |
| 7136 | /* bash prints killer signal's name for *last* |
Denys Vlasenko | 7c6f246 | 2011-02-14 17:17:10 +0100 | [diff] [blame] | 7137 | * process in pipe (prints just newline for SIGINT/SIGPIPE). |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7138 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 7139 | */ |
| 7140 | if (WIFSIGNALED(status)) { |
| 7141 | int sig = WTERMSIG(status); |
| 7142 | if (i == fg_pipe->num_cmds-1) |
Denys Vlasenko | 7c6f246 | 2011-02-14 17:17:10 +0100 | [diff] [blame] | 7143 | /* TODO: use strsignal() instead for bash compat? but that's bloat... */ |
Denys Vlasenko | d60752f | 2015-10-07 22:42:45 +0200 | [diff] [blame] | 7144 | puts(sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig)); |
Denys Vlasenko | 7c6f246 | 2011-02-14 17:17:10 +0100 | [diff] [blame] | 7145 | /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */ |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7146 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 7147 | * Maybe we need to use sig | 128? */ |
| 7148 | ex = sig + 128; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7149 | } |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7150 | fg_pipe->cmds[i].cmd_exitcode = ex; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7151 | } else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7152 | fg_pipe->stopped_cmds++; |
| 7153 | } |
| 7154 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 7155 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 7156 | if (fg_pipe->alive_cmds == fg_pipe->stopped_cmds) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7157 | /* All processes in fg pipe have exited or stopped */ |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7158 | i = fg_pipe->num_cmds; |
| 7159 | while (--i >= 0) { |
| 7160 | rcode = fg_pipe->cmds[i].cmd_exitcode; |
| 7161 | /* usually last process gives overall exitstatus, |
| 7162 | * but with "set -o pipefail", last *failed* process does */ |
| 7163 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 7164 | break; |
| 7165 | } |
| 7166 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7167 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 7168 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 7169 | * and "killall -STOP cat" */ |
| 7170 | if (G_interactive_fd) { |
| 7171 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 7172 | if (fg_pipe->alive_cmds != 0) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7173 | insert_bg_job(fg_pipe); |
| 7174 | #endif |
| 7175 | return rcode; |
| 7176 | } |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 7177 | if (fg_pipe->alive_cmds == 0) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7178 | return rcode; |
| 7179 | } |
| 7180 | /* There are still running processes in the fg pipe */ |
| 7181 | goto wait_more; /* do waitpid again */ |
| 7182 | } |
| 7183 | /* it wasnt fg_pipe, look for process in bg pipes */ |
| 7184 | } |
| 7185 | |
| 7186 | #if ENABLE_HUSH_JOB |
| 7187 | /* We asked to wait for bg or orphaned children */ |
| 7188 | /* No need to remember exitcode in this case */ |
| 7189 | for (pi = G.job_list; pi; pi = pi->next) { |
| 7190 | for (i = 0; i < pi->num_cmds; i++) { |
| 7191 | if (pi->cmds[i].pid == childpid) |
| 7192 | goto found_pi_and_prognum; |
| 7193 | } |
| 7194 | } |
| 7195 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 7196 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 7197 | continue; /* do waitpid again */ |
| 7198 | |
| 7199 | found_pi_and_prognum: |
| 7200 | if (dead) { |
| 7201 | /* child exited */ |
| 7202 | pi->cmds[i].pid = 0; |
| 7203 | pi->alive_cmds--; |
| 7204 | if (!pi->alive_cmds) { |
| 7205 | if (G_interactive_fd) |
| 7206 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 7207 | "Done", pi->cmdtext); |
| 7208 | delete_finished_bg_job(pi); |
| 7209 | } |
| 7210 | } else { |
| 7211 | /* child stopped */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7212 | pi->stopped_cmds++; |
| 7213 | } |
| 7214 | #endif |
| 7215 | } /* while (waitpid succeeds)... */ |
| 7216 | |
| 7217 | return rcode; |
| 7218 | } |
| 7219 | |
| 7220 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7221 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7222 | { |
| 7223 | pid_t p; |
| 7224 | int rcode = checkjobs(fg_pipe); |
| 7225 | if (G_saved_tty_pgrp) { |
| 7226 | /* Job finished, move the shell to the foreground */ |
| 7227 | p = getpgrp(); /* our process group id */ |
| 7228 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 7229 | tcsetpgrp(G_interactive_fd, p); |
| 7230 | } |
| 7231 | return rcode; |
| 7232 | } |
| 7233 | #endif |
| 7234 | |
| 7235 | /* Start all the jobs, but don't wait for anything to finish. |
| 7236 | * See checkjobs(). |
| 7237 | * |
| 7238 | * Return code is normally -1, when the caller has to wait for children |
| 7239 | * to finish to determine the exit status of the pipe. If the pipe |
| 7240 | * is a simple builtin command, however, the action is done by the |
| 7241 | * time run_pipe returns, and the exit code is provided as the |
| 7242 | * return value. |
| 7243 | * |
| 7244 | * Returns -1 only if started some children. IOW: we have to |
| 7245 | * mask out retvals of builtins etc with 0xff! |
| 7246 | * |
| 7247 | * The only case when we do not need to [v]fork is when the pipe |
| 7248 | * is single, non-backgrounded, non-subshell command. Examples: |
| 7249 | * cmd ; ... { list } ; ... |
| 7250 | * cmd && ... { list } && ... |
| 7251 | * cmd || ... { list } || ... |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7252 | * If it is, then we can run cmd as a builtin, NOFORK, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7253 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 7254 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 7255 | * |
| 7256 | * Cases when we must fork: |
| 7257 | * non-single: cmd | cmd |
| 7258 | * backgrounded: cmd & { list } & |
| 7259 | * subshell: ( list ) [&] |
| 7260 | */ |
| 7261 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 7262 | #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] | 7263 | redirect_and_varexp_helper(new_env_p, old_vars_p, command, squirrel) |
| 7264 | #endif |
| 7265 | static int redirect_and_varexp_helper(char ***new_env_p, |
| 7266 | struct variable **old_vars_p, |
| 7267 | struct command *command, |
| 7268 | int squirrel[3], |
| 7269 | char **argv_expanded) |
| 7270 | { |
| 7271 | /* setup_redirects acts on file descriptors, not FILEs. |
| 7272 | * This is perfect for work that comes after exec(). |
| 7273 | * Is it really safe for inline use? Experimentally, |
| 7274 | * things seem to work. */ |
| 7275 | int rcode = setup_redirects(command, squirrel); |
| 7276 | if (rcode == 0) { |
| 7277 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 7278 | *new_env_p = new_env; |
| 7279 | dump_cmd_in_x_mode(new_env); |
| 7280 | dump_cmd_in_x_mode(argv_expanded); |
| 7281 | if (old_vars_p) |
| 7282 | *old_vars_p = set_vars_and_save_old(new_env); |
| 7283 | } |
| 7284 | return rcode; |
| 7285 | } |
| 7286 | static NOINLINE int run_pipe(struct pipe *pi) |
| 7287 | { |
| 7288 | static const char *const null_ptr = NULL; |
| 7289 | |
| 7290 | int cmd_no; |
| 7291 | int next_infd; |
| 7292 | struct command *command; |
| 7293 | char **argv_expanded; |
| 7294 | char **argv; |
| 7295 | /* it is not always needed, but we aim to smaller code */ |
| 7296 | int squirrel[] = { -1, -1, -1 }; |
| 7297 | int rcode; |
| 7298 | |
| 7299 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 7300 | debug_enter(); |
| 7301 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 7302 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 7303 | * Result should be 3 lines: q w e, qwe, q w e |
| 7304 | */ |
| 7305 | G.ifs = get_local_var_value("IFS"); |
| 7306 | if (!G.ifs) |
| 7307 | G.ifs = defifs; |
| 7308 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7309 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 7310 | pi->stopped_cmds = 0; |
| 7311 | command = &pi->cmds[0]; |
| 7312 | argv_expanded = NULL; |
| 7313 | |
| 7314 | if (pi->num_cmds != 1 |
| 7315 | || pi->followup == PIPE_BG |
| 7316 | || command->cmd_type == CMD_SUBSHELL |
| 7317 | ) { |
| 7318 | goto must_fork; |
| 7319 | } |
| 7320 | |
| 7321 | pi->alive_cmds = 1; |
| 7322 | |
| 7323 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 7324 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 7325 | |
| 7326 | if (command->group) { |
| 7327 | #if ENABLE_HUSH_FUNCTIONS |
| 7328 | if (command->cmd_type == CMD_FUNCDEF) { |
| 7329 | /* "executing" func () { list } */ |
| 7330 | struct function *funcp; |
| 7331 | |
| 7332 | funcp = new_function(command->argv[0]); |
| 7333 | /* funcp->name is already set to argv[0] */ |
| 7334 | funcp->body = command->group; |
| 7335 | # if !BB_MMU |
| 7336 | funcp->body_as_string = command->group_as_string; |
| 7337 | command->group_as_string = NULL; |
| 7338 | # endif |
| 7339 | command->group = NULL; |
| 7340 | command->argv[0] = NULL; |
| 7341 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 7342 | funcp->parent_cmd = command; |
| 7343 | command->child_func = funcp; |
| 7344 | |
| 7345 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 7346 | debug_leave(); |
| 7347 | return EXIT_SUCCESS; |
| 7348 | } |
| 7349 | #endif |
| 7350 | /* { list } */ |
| 7351 | debug_printf("non-subshell group\n"); |
| 7352 | rcode = 1; /* exitcode if redir failed */ |
| 7353 | if (setup_redirects(command, squirrel) == 0) { |
| 7354 | debug_printf_exec(": run_list\n"); |
| 7355 | rcode = run_list(command->group) & 0xff; |
| 7356 | } |
| 7357 | restore_redirects(squirrel); |
| 7358 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7359 | debug_leave(); |
| 7360 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 7361 | return rcode; |
| 7362 | } |
| 7363 | |
| 7364 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 7365 | { |
| 7366 | const struct built_in_command *x; |
| 7367 | #if ENABLE_HUSH_FUNCTIONS |
| 7368 | const struct function *funcp; |
| 7369 | #else |
| 7370 | enum { funcp = 0 }; |
| 7371 | #endif |
| 7372 | char **new_env = NULL; |
| 7373 | struct variable *old_vars = NULL; |
| 7374 | |
| 7375 | if (argv[command->assignment_cnt] == NULL) { |
| 7376 | /* Assignments, but no command */ |
| 7377 | /* Ensure redirects take effect (that is, create files). |
| 7378 | * Try "a=t >file" */ |
| 7379 | #if 0 /* A few cases in testsuite fail with this code. FIXME */ |
| 7380 | rcode = redirect_and_varexp_helper(&new_env, /*old_vars:*/ NULL, command, squirrel, /*argv_expanded:*/ NULL); |
| 7381 | /* Set shell variables */ |
| 7382 | if (new_env) { |
| 7383 | argv = new_env; |
| 7384 | while (*argv) { |
| 7385 | set_local_var(*argv, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7386 | /* Do we need to flag set_local_var() errors? |
| 7387 | * "assignment to readonly var" and "putenv error" |
| 7388 | */ |
| 7389 | argv++; |
| 7390 | } |
| 7391 | } |
| 7392 | /* Redirect error sets $? to 1. Otherwise, |
| 7393 | * if evaluating assignment value set $?, retain it. |
| 7394 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 7395 | if (rcode == 0) |
| 7396 | rcode = G.last_exitcode; |
| 7397 | /* Exit, _skipping_ variable restoring code: */ |
| 7398 | goto clean_up_and_ret0; |
| 7399 | |
| 7400 | #else /* Older, bigger, but more correct code */ |
| 7401 | |
| 7402 | rcode = setup_redirects(command, squirrel); |
| 7403 | restore_redirects(squirrel); |
| 7404 | /* Set shell variables */ |
| 7405 | if (G_x_mode) |
| 7406 | bb_putchar_stderr('+'); |
| 7407 | while (*argv) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7408 | char *p = expand_string_to_string(*argv, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7409 | if (G_x_mode) |
| 7410 | fprintf(stderr, " %s", p); |
| 7411 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 7412 | *argv, p); |
| 7413 | set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7414 | /* Do we need to flag set_local_var() errors? |
| 7415 | * "assignment to readonly var" and "putenv error" |
| 7416 | */ |
| 7417 | argv++; |
| 7418 | } |
| 7419 | if (G_x_mode) |
| 7420 | bb_putchar_stderr('\n'); |
| 7421 | /* Redirect error sets $? to 1. Otherwise, |
| 7422 | * if evaluating assignment value set $?, retain it. |
| 7423 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 7424 | if (rcode == 0) |
| 7425 | rcode = G.last_exitcode; |
| 7426 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7427 | debug_leave(); |
| 7428 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 7429 | return rcode; |
| 7430 | #endif |
| 7431 | } |
| 7432 | |
| 7433 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7434 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7435 | if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7436 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7437 | } else |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7438 | #endif |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7439 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7440 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
| 7441 | } |
| 7442 | |
| 7443 | /* if someone gives us an empty string: `cmd with empty output` */ |
| 7444 | if (!argv_expanded[0]) { |
| 7445 | free(argv_expanded); |
| 7446 | debug_leave(); |
| 7447 | return G.last_exitcode; |
| 7448 | } |
| 7449 | |
| 7450 | x = find_builtin(argv_expanded[0]); |
| 7451 | #if ENABLE_HUSH_FUNCTIONS |
| 7452 | funcp = NULL; |
| 7453 | if (!x) |
| 7454 | funcp = find_function(argv_expanded[0]); |
| 7455 | #endif |
| 7456 | if (x || funcp) { |
| 7457 | if (!funcp) { |
| 7458 | if (x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 7459 | debug_printf("exec with redirects only\n"); |
| 7460 | rcode = setup_redirects(command, NULL); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7461 | /* rcode=1 can be if redir file can't be opened */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7462 | goto clean_up_and_ret1; |
| 7463 | } |
| 7464 | } |
| 7465 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
| 7466 | if (rcode == 0) { |
| 7467 | if (!funcp) { |
| 7468 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 7469 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7470 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7471 | rcode = x->b_function(argv_expanded) & 0xff; |
| 7472 | fflush_all(); |
| 7473 | } |
| 7474 | #if ENABLE_HUSH_FUNCTIONS |
| 7475 | else { |
| 7476 | # if ENABLE_HUSH_LOCAL |
| 7477 | struct variable **sv; |
| 7478 | sv = G.shadowed_vars_pp; |
| 7479 | G.shadowed_vars_pp = &old_vars; |
| 7480 | # endif |
| 7481 | debug_printf_exec(": function '%s' '%s'...\n", |
| 7482 | funcp->name, argv_expanded[1]); |
| 7483 | rcode = run_function(funcp, argv_expanded) & 0xff; |
| 7484 | # if ENABLE_HUSH_LOCAL |
| 7485 | G.shadowed_vars_pp = sv; |
| 7486 | # endif |
| 7487 | } |
| 7488 | #endif |
| 7489 | } |
| 7490 | clean_up_and_ret: |
| 7491 | unset_vars(new_env); |
| 7492 | add_vars(old_vars); |
| 7493 | /* clean_up_and_ret0: */ |
| 7494 | restore_redirects(squirrel); |
| 7495 | clean_up_and_ret1: |
| 7496 | free(argv_expanded); |
| 7497 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7498 | debug_leave(); |
| 7499 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 7500 | return rcode; |
| 7501 | } |
| 7502 | |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7503 | if (ENABLE_FEATURE_SH_NOFORK) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7504 | int n = find_applet_by_name(argv_expanded[0]); |
| 7505 | if (n >= 0 && APPLET_IS_NOFORK(n)) { |
| 7506 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
| 7507 | if (rcode == 0) { |
| 7508 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 7509 | argv_expanded[0], argv_expanded[1]); |
| 7510 | rcode = run_nofork_applet(n, argv_expanded); |
| 7511 | } |
| 7512 | goto clean_up_and_ret; |
| 7513 | } |
| 7514 | } |
| 7515 | /* It is neither builtin nor applet. We must fork. */ |
| 7516 | } |
| 7517 | |
| 7518 | must_fork: |
| 7519 | /* NB: argv_expanded may already be created, and that |
| 7520 | * might include `cmd` runs! Do not rerun it! We *must* |
| 7521 | * use argv_expanded if it's non-NULL */ |
| 7522 | |
| 7523 | /* Going to fork a child per each pipe member */ |
| 7524 | pi->alive_cmds = 0; |
| 7525 | next_infd = 0; |
| 7526 | |
| 7527 | cmd_no = 0; |
| 7528 | while (cmd_no < pi->num_cmds) { |
| 7529 | struct fd_pair pipefds; |
| 7530 | #if !BB_MMU |
| 7531 | volatile nommu_save_t nommu_save; |
| 7532 | nommu_save.new_env = NULL; |
| 7533 | nommu_save.old_vars = NULL; |
| 7534 | nommu_save.argv = NULL; |
| 7535 | nommu_save.argv_from_re_execing = NULL; |
| 7536 | #endif |
| 7537 | command = &pi->cmds[cmd_no]; |
| 7538 | cmd_no++; |
| 7539 | if (command->argv) { |
| 7540 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 7541 | command->argv[0], command->argv[1]); |
| 7542 | } else { |
| 7543 | debug_printf_exec(": pipe member with no argv\n"); |
| 7544 | } |
| 7545 | |
| 7546 | /* pipes are inserted between pairs of commands */ |
| 7547 | pipefds.rd = 0; |
| 7548 | pipefds.wr = 1; |
| 7549 | if (cmd_no < pi->num_cmds) |
| 7550 | xpiped_pair(pipefds); |
| 7551 | |
| 7552 | command->pid = BB_MMU ? fork() : vfork(); |
| 7553 | if (!command->pid) { /* child */ |
| 7554 | #if ENABLE_HUSH_JOB |
| 7555 | disable_restore_tty_pgrp_on_exit(); |
| 7556 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 7557 | |
| 7558 | /* Every child adds itself to new process group |
| 7559 | * with pgid == pid_of_first_child_in_pipe */ |
| 7560 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 7561 | pid_t pgrp; |
| 7562 | pgrp = pi->pgrp; |
| 7563 | if (pgrp < 0) /* true for 1st process only */ |
| 7564 | pgrp = getpid(); |
| 7565 | if (setpgid(0, pgrp) == 0 |
| 7566 | && pi->followup != PIPE_BG |
| 7567 | && G_saved_tty_pgrp /* we have ctty */ |
| 7568 | ) { |
| 7569 | /* We do it in *every* child, not just first, |
| 7570 | * to avoid races */ |
| 7571 | tcsetpgrp(G_interactive_fd, pgrp); |
| 7572 | } |
| 7573 | } |
| 7574 | #endif |
| 7575 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 7576 | /* 1st cmd in backgrounded pipe |
| 7577 | * should have its stdin /dev/null'ed */ |
| 7578 | close(0); |
| 7579 | if (open(bb_dev_null, O_RDONLY)) |
| 7580 | xopen("/", O_RDONLY); |
| 7581 | } else { |
| 7582 | xmove_fd(next_infd, 0); |
| 7583 | } |
| 7584 | xmove_fd(pipefds.wr, 1); |
| 7585 | if (pipefds.rd > 1) |
| 7586 | close(pipefds.rd); |
| 7587 | /* Like bash, explicit redirects override pipes, |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7588 | * and the pipe fd (fd#1) is available for dup'ing: |
| 7589 | * "cmd1 2>&1 | cmd2": fd#1 is duped to fd#2, thus stderr |
| 7590 | * of cmd1 goes into pipe. |
| 7591 | */ |
| 7592 | if (setup_redirects(command, NULL)) { |
| 7593 | /* Happens when redir file can't be opened: |
| 7594 | * $ hush -c 'echo FOO >&2 | echo BAR 3>/qwe/rty; echo BAZ' |
| 7595 | * FOO |
| 7596 | * hush: can't open '/qwe/rty': No such file or directory |
| 7597 | * BAZ |
| 7598 | * (echo BAR is not executed, it hits _exit(1) below) |
| 7599 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7600 | _exit(1); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7601 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7602 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7603 | /* Stores to nommu_save list of env vars putenv'ed |
| 7604 | * (NOMMU, on MMU we don't need that) */ |
| 7605 | /* cast away volatility... */ |
| 7606 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 7607 | /* pseudo_exec() does not return */ |
| 7608 | } |
| 7609 | |
| 7610 | /* parent or error */ |
| 7611 | #if ENABLE_HUSH_FAST |
| 7612 | G.count_SIGCHLD++; |
| 7613 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7614 | #endif |
| 7615 | enable_restore_tty_pgrp_on_exit(); |
| 7616 | #if !BB_MMU |
| 7617 | /* Clean up after vforked child */ |
| 7618 | free(nommu_save.argv); |
| 7619 | free(nommu_save.argv_from_re_execing); |
| 7620 | unset_vars(nommu_save.new_env); |
| 7621 | add_vars(nommu_save.old_vars); |
| 7622 | #endif |
| 7623 | free(argv_expanded); |
| 7624 | argv_expanded = NULL; |
| 7625 | if (command->pid < 0) { /* [v]fork failed */ |
| 7626 | /* Clearly indicate, was it fork or vfork */ |
| 7627 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
| 7628 | } else { |
| 7629 | pi->alive_cmds++; |
| 7630 | #if ENABLE_HUSH_JOB |
| 7631 | /* Second and next children need to know pid of first one */ |
| 7632 | if (pi->pgrp < 0) |
| 7633 | pi->pgrp = command->pid; |
| 7634 | #endif |
| 7635 | } |
| 7636 | |
| 7637 | if (cmd_no > 1) |
| 7638 | close(next_infd); |
| 7639 | if (cmd_no < pi->num_cmds) |
| 7640 | close(pipefds.wr); |
| 7641 | /* Pass read (output) pipe end to next iteration */ |
| 7642 | next_infd = pipefds.rd; |
| 7643 | } |
| 7644 | |
| 7645 | if (!pi->alive_cmds) { |
| 7646 | debug_leave(); |
| 7647 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 7648 | return 1; |
| 7649 | } |
| 7650 | |
| 7651 | debug_leave(); |
| 7652 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 7653 | return -1; |
| 7654 | } |
| 7655 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7656 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 7657 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 7658 | static int run_list(struct pipe *pi) |
| 7659 | { |
| 7660 | #if ENABLE_HUSH_CASE |
| 7661 | char *case_word = NULL; |
| 7662 | #endif |
| 7663 | #if ENABLE_HUSH_LOOPS |
| 7664 | struct pipe *loop_top = NULL; |
| 7665 | char **for_lcur = NULL; |
| 7666 | char **for_list = NULL; |
| 7667 | #endif |
| 7668 | smallint last_followup; |
| 7669 | smalluint rcode; |
| 7670 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 7671 | smalluint cond_code = 0; |
| 7672 | #else |
| 7673 | enum { cond_code = 0 }; |
| 7674 | #endif |
| 7675 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 7676 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7677 | smallint last_rword; /* ditto */ |
| 7678 | #endif |
| 7679 | |
| 7680 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 7681 | debug_enter(); |
| 7682 | |
| 7683 | #if ENABLE_HUSH_LOOPS |
| 7684 | /* Check syntax for "for" */ |
Denys Vlasenko | 0d6a4ec | 2010-12-18 01:34:49 +0100 | [diff] [blame] | 7685 | { |
| 7686 | struct pipe *cpipe; |
| 7687 | for (cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 7688 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 7689 | continue; |
| 7690 | /* current word is FOR or IN (BOLD in comments below) */ |
| 7691 | if (cpipe->next == NULL) { |
| 7692 | syntax_error("malformed for"); |
| 7693 | debug_leave(); |
| 7694 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 7695 | return 1; |
| 7696 | } |
| 7697 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 7698 | if (cpipe->next->res_word == RES_DO) |
| 7699 | continue; |
| 7700 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 7701 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 7702 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 7703 | ) { |
| 7704 | syntax_error("malformed for"); |
| 7705 | debug_leave(); |
| 7706 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 7707 | return 1; |
| 7708 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7709 | } |
| 7710 | } |
| 7711 | #endif |
| 7712 | |
| 7713 | /* Past this point, all code paths should jump to ret: label |
| 7714 | * in order to return, no direct "return" statements please. |
| 7715 | * This helps to ensure that no memory is leaked. */ |
| 7716 | |
| 7717 | #if ENABLE_HUSH_JOB |
| 7718 | G.run_list_level++; |
| 7719 | #endif |
| 7720 | |
| 7721 | #if HAS_KEYWORDS |
| 7722 | rword = RES_NONE; |
| 7723 | last_rword = RES_XXXX; |
| 7724 | #endif |
| 7725 | last_followup = PIPE_SEQ; |
| 7726 | rcode = G.last_exitcode; |
| 7727 | |
| 7728 | /* Go through list of pipes, (maybe) executing them. */ |
| 7729 | for (; pi; pi = IF_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
| 7730 | if (G.flag_SIGINT) |
| 7731 | break; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7732 | if (G_flag_return_in_progress == 1) |
| 7733 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7734 | |
| 7735 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 7736 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 7737 | rword, cond_code, last_rword); |
| 7738 | #if ENABLE_HUSH_LOOPS |
| 7739 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 7740 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 7741 | ) { |
| 7742 | /* start of a loop: remember where loop starts */ |
| 7743 | loop_top = pi; |
| 7744 | G.depth_of_loop++; |
| 7745 | } |
| 7746 | #endif |
| 7747 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 7748 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 7749 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 7750 | || (rcode != 0 && last_followup == PIPE_AND) |
| 7751 | ) { |
| 7752 | /* It is "<true> || CMD" or "<false> && CMD" |
| 7753 | * and we should not execute CMD */ |
| 7754 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 7755 | last_followup = pi->followup; |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 7756 | goto dont_check_jobs_but_continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7757 | } |
| 7758 | } |
| 7759 | last_followup = pi->followup; |
| 7760 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 7761 | #if ENABLE_HUSH_IF |
| 7762 | if (cond_code) { |
| 7763 | if (rword == RES_THEN) { |
| 7764 | /* if false; then ... fi has exitcode 0! */ |
| 7765 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7766 | /* "if <false> THEN cmd": skip cmd */ |
| 7767 | continue; |
| 7768 | } |
| 7769 | } else { |
| 7770 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 7771 | /* "if <true> then ... ELSE/ELIF cmd": |
| 7772 | * skip cmd and all following ones */ |
| 7773 | break; |
| 7774 | } |
| 7775 | } |
| 7776 | #endif |
| 7777 | #if ENABLE_HUSH_LOOPS |
| 7778 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 7779 | if (!for_lcur) { |
| 7780 | /* first loop through for */ |
| 7781 | |
| 7782 | static const char encoded_dollar_at[] ALIGN1 = { |
| 7783 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 7784 | }; /* encoded representation of "$@" */ |
| 7785 | static const char *const encoded_dollar_at_argv[] = { |
| 7786 | encoded_dollar_at, NULL |
| 7787 | }; /* argv list with one element: "$@" */ |
| 7788 | char **vals; |
| 7789 | |
| 7790 | vals = (char**)encoded_dollar_at_argv; |
| 7791 | if (pi->next->res_word == RES_IN) { |
| 7792 | /* if no variable values after "in" we skip "for" */ |
| 7793 | if (!pi->next->cmds[0].argv) { |
| 7794 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7795 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 7796 | break; |
| 7797 | } |
| 7798 | vals = pi->next->cmds[0].argv; |
| 7799 | } /* else: "for var; do..." -> assume "$@" list */ |
| 7800 | /* create list of variable values */ |
| 7801 | debug_print_strings("for_list made from", vals); |
| 7802 | for_list = expand_strvec_to_strvec(vals); |
| 7803 | for_lcur = for_list; |
| 7804 | debug_print_strings("for_list", for_list); |
| 7805 | } |
| 7806 | if (!*for_lcur) { |
| 7807 | /* "for" loop is over, clean up */ |
| 7808 | free(for_list); |
| 7809 | for_list = NULL; |
| 7810 | for_lcur = NULL; |
| 7811 | break; |
| 7812 | } |
| 7813 | /* Insert next value from for_lcur */ |
| 7814 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
| 7815 | set_local_var(xasprintf("%s=%s", pi->cmds[0].argv[0], *for_lcur++), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7816 | continue; |
| 7817 | } |
| 7818 | if (rword == RES_IN) { |
| 7819 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 7820 | } |
| 7821 | if (rword == RES_DONE) { |
| 7822 | continue; /* "done" has no cmds too */ |
| 7823 | } |
| 7824 | #endif |
| 7825 | #if ENABLE_HUSH_CASE |
| 7826 | if (rword == RES_CASE) { |
| 7827 | case_word = expand_strvec_to_string(pi->cmds->argv); |
| 7828 | continue; |
| 7829 | } |
| 7830 | if (rword == RES_MATCH) { |
| 7831 | char **argv; |
| 7832 | |
| 7833 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 7834 | break; |
| 7835 | /* all prev words didn't match, does this one match? */ |
| 7836 | argv = pi->cmds->argv; |
| 7837 | while (*argv) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7838 | char *pattern = expand_string_to_string(*argv, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7839 | /* TODO: which FNM_xxx flags to use? */ |
| 7840 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 7841 | free(pattern); |
| 7842 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 7843 | free(case_word); /* make future "word)" stop */ |
| 7844 | case_word = NULL; |
| 7845 | break; |
| 7846 | } |
| 7847 | argv++; |
| 7848 | } |
| 7849 | continue; |
| 7850 | } |
| 7851 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
| 7852 | if (cond_code != 0) |
| 7853 | continue; /* not matched yet, skip this pipe */ |
| 7854 | } |
| 7855 | #endif |
| 7856 | /* Just pressing <enter> in shell should check for jobs. |
| 7857 | * OTOH, in non-interactive shell this is useless |
| 7858 | * and only leads to extra job checks */ |
| 7859 | if (pi->num_cmds == 0) { |
| 7860 | if (G_interactive_fd) |
| 7861 | goto check_jobs_and_continue; |
| 7862 | continue; |
| 7863 | } |
| 7864 | |
| 7865 | /* After analyzing all keywords and conditions, we decided |
| 7866 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 7867 | * after run_pipe to collect any background children, |
| 7868 | * even if list execution is to be stopped. */ |
| 7869 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
| 7870 | { |
| 7871 | int r; |
| 7872 | #if ENABLE_HUSH_LOOPS |
| 7873 | G.flag_break_continue = 0; |
| 7874 | #endif |
| 7875 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 7876 | if (r != -1) { |
| 7877 | /* We ran a builtin, function, or group. |
| 7878 | * rcode is already known |
| 7879 | * and we don't need to wait for anything. */ |
| 7880 | G.last_exitcode = rcode; |
| 7881 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7882 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7883 | #if ENABLE_HUSH_LOOPS |
| 7884 | /* Was it "break" or "continue"? */ |
| 7885 | if (G.flag_break_continue) { |
| 7886 | smallint fbc = G.flag_break_continue; |
| 7887 | /* We might fall into outer *loop*, |
| 7888 | * don't want to break it too */ |
| 7889 | if (loop_top) { |
| 7890 | G.depth_break_continue--; |
| 7891 | if (G.depth_break_continue == 0) |
| 7892 | G.flag_break_continue = 0; |
| 7893 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 7894 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 7895 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) { |
| 7896 | checkjobs(NULL); |
| 7897 | break; |
| 7898 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7899 | /* "continue": simulate end of loop */ |
| 7900 | rword = RES_DONE; |
| 7901 | continue; |
| 7902 | } |
| 7903 | #endif |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7904 | if (G_flag_return_in_progress == 1) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7905 | checkjobs(NULL); |
| 7906 | break; |
| 7907 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7908 | } else if (pi->followup == PIPE_BG) { |
| 7909 | /* What does bash do with attempts to background builtins? */ |
| 7910 | /* even bash 3.2 doesn't do that well with nested bg: |
| 7911 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 7912 | * I'm NOT treating inner &'s as jobs */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7913 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7914 | #if ENABLE_HUSH_JOB |
| 7915 | if (G.run_list_level == 1) |
| 7916 | insert_bg_job(pi); |
| 7917 | #endif |
| 7918 | /* Last command's pid goes to $! */ |
| 7919 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
| 7920 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7921 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
| 7922 | } else { |
| 7923 | #if ENABLE_HUSH_JOB |
| 7924 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 7925 | /* Waits for completion, then fg's main shell */ |
| 7926 | rcode = checkjobs_and_fg_shell(pi); |
| 7927 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7928 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7929 | } else |
| 7930 | #endif |
| 7931 | { /* This one just waits for completion */ |
| 7932 | rcode = checkjobs(pi); |
| 7933 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7934 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7935 | } |
| 7936 | G.last_exitcode = rcode; |
| 7937 | } |
| 7938 | } |
| 7939 | |
| 7940 | /* Analyze how result affects subsequent commands */ |
| 7941 | #if ENABLE_HUSH_IF |
| 7942 | if (rword == RES_IF || rword == RES_ELIF) |
| 7943 | cond_code = rcode; |
| 7944 | #endif |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 7945 | check_jobs_and_continue: |
| 7946 | checkjobs(NULL); |
| 7947 | dont_check_jobs_but_continue: ; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7948 | #if ENABLE_HUSH_LOOPS |
| 7949 | /* Beware of "while false; true; do ..."! */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 7950 | if (pi->next |
| 7951 | && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE) |
Denys Vlasenko | 56a3b82 | 2011-06-01 12:47:07 +0200 | [diff] [blame] | 7952 | /* check for RES_DONE is needed for "while ...; do \n done" case */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 7953 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7954 | if (rword == RES_WHILE) { |
| 7955 | if (rcode) { |
| 7956 | /* "while false; do...done" - exitcode 0 */ |
| 7957 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7958 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 7959 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7960 | } |
| 7961 | } |
| 7962 | if (rword == RES_UNTIL) { |
| 7963 | if (!rcode) { |
| 7964 | debug_printf_exec(": until expr is true: breaking\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7965 | break; |
| 7966 | } |
| 7967 | } |
| 7968 | } |
| 7969 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7970 | } /* for (pi) */ |
| 7971 | |
| 7972 | #if ENABLE_HUSH_JOB |
| 7973 | G.run_list_level--; |
| 7974 | #endif |
| 7975 | #if ENABLE_HUSH_LOOPS |
| 7976 | if (loop_top) |
| 7977 | G.depth_of_loop--; |
| 7978 | free(for_list); |
| 7979 | #endif |
| 7980 | #if ENABLE_HUSH_CASE |
| 7981 | free(case_word); |
| 7982 | #endif |
| 7983 | debug_leave(); |
| 7984 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 7985 | return rcode; |
| 7986 | } |
| 7987 | |
| 7988 | /* Select which version we will use */ |
| 7989 | static int run_and_free_list(struct pipe *pi) |
| 7990 | { |
| 7991 | int rcode = 0; |
| 7992 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 7993 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7994 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 7995 | rcode = run_list(pi); |
| 7996 | } |
| 7997 | /* free_pipe_list has the side effect of clearing memory. |
| 7998 | * In the long run that function can be merged with run_list, |
| 7999 | * but doing that now would hobble the debugging effort. */ |
| 8000 | free_pipe_list(pi); |
| 8001 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 8002 | return rcode; |
| 8003 | } |
| 8004 | |
| 8005 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8006 | static void install_sighandlers(unsigned mask) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 8007 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8008 | sighandler_t old_handler; |
| 8009 | unsigned sig = 0; |
| 8010 | while ((mask >>= 1) != 0) { |
| 8011 | sig++; |
| 8012 | if (!(mask & 1)) |
| 8013 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 8014 | old_handler = install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8015 | /* POSIX allows shell to re-enable SIGCHLD |
| 8016 | * even if it was SIG_IGN on entry. |
| 8017 | * Therefore we skip IGN check for it: |
| 8018 | */ |
| 8019 | if (sig == SIGCHLD) |
| 8020 | continue; |
| 8021 | if (old_handler == SIG_IGN) { |
| 8022 | /* oops... restore back to IGN, and record this fact */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 8023 | install_sighandler(sig, old_handler); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8024 | if (!G.traps) |
| 8025 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 8026 | free(G.traps[sig]); |
| 8027 | G.traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 8028 | } |
| 8029 | } |
| 8030 | } |
| 8031 | |
| 8032 | /* Called a few times only (or even once if "sh -c") */ |
| 8033 | static void install_special_sighandlers(void) |
| 8034 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8035 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8036 | |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8037 | /* Which signals are shell-special? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8038 | mask = (1 << SIGQUIT) | (1 << SIGCHLD); |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8039 | if (G_interactive_fd) { |
| 8040 | mask |= SPECIAL_INTERACTIVE_SIGS; |
| 8041 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8042 | mask |= SPECIAL_JOBSTOP_SIGS; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8043 | } |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8044 | /* Careful, do not re-install handlers we already installed */ |
| 8045 | if (G.special_sig_mask != mask) { |
| 8046 | unsigned diff = mask & ~G.special_sig_mask; |
| 8047 | G.special_sig_mask = mask; |
| 8048 | install_sighandlers(diff); |
| 8049 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8050 | } |
| 8051 | |
| 8052 | #if ENABLE_HUSH_JOB |
| 8053 | /* helper */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8054 | /* Set handlers to restore tty pgrp and exit */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8055 | static void install_fatal_sighandlers(void) |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8056 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8057 | unsigned mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8058 | |
| 8059 | /* We will restore tty pgrp on these signals */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8060 | mask = 0 |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8061 | + (1 << SIGILL ) * HUSH_DEBUG |
| 8062 | + (1 << SIGFPE ) * HUSH_DEBUG |
| 8063 | + (1 << SIGBUS ) * HUSH_DEBUG |
| 8064 | + (1 << SIGSEGV) * HUSH_DEBUG |
| 8065 | + (1 << SIGTRAP) * HUSH_DEBUG |
| 8066 | + (1 << SIGABRT) |
| 8067 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 8068 | + (1 << SIGPIPE) |
| 8069 | + (1 << SIGALRM) |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8070 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs. |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8071 | * if we aren't interactive... but in this case |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8072 | * we never want to restore pgrp on exit, and this fn is not called |
| 8073 | */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8074 | /*+ (1 << SIGHUP )*/ |
| 8075 | /*+ (1 << SIGTERM)*/ |
| 8076 | /*+ (1 << SIGINT )*/ |
| 8077 | ; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8078 | G_fatal_sig_mask = mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8079 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8080 | install_sighandlers(mask); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8081 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 8082 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 8083 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8084 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8085 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8086 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8087 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8088 | case 'n': |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 8089 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8090 | break; |
| 8091 | case 'x': |
| 8092 | IF_HUSH_MODE_X(G_x_mode = state;) |
| 8093 | break; |
| 8094 | case 'o': |
| 8095 | if (!o_opt) { |
| 8096 | /* "set -+o" without parameter. |
| 8097 | * in bash, set -o produces this output: |
| 8098 | * pipefail off |
| 8099 | * and set +o: |
| 8100 | * set +o pipefail |
| 8101 | * We always use the second form. |
| 8102 | */ |
| 8103 | const char *p = o_opt_strings; |
| 8104 | idx = 0; |
| 8105 | while (*p) { |
| 8106 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 8107 | idx++; |
| 8108 | p += strlen(p) + 1; |
| 8109 | } |
| 8110 | break; |
| 8111 | } |
| 8112 | idx = index_in_strings(o_opt_strings, o_opt); |
| 8113 | if (idx >= 0) { |
| 8114 | G.o_opt[idx] = state; |
| 8115 | break; |
| 8116 | } |
| 8117 | default: |
| 8118 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8119 | } |
| 8120 | return EXIT_SUCCESS; |
| 8121 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8122 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 8123 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 8124 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8125 | { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8126 | enum { |
| 8127 | OPT_login = (1 << 0), |
| 8128 | }; |
| 8129 | unsigned flags; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8130 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8131 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8132 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8133 | struct variable *cur_var; |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8134 | struct variable *shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 8135 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 8136 | INIT_G(); |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 8137 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8138 | G.last_exitcode = EXIT_SUCCESS; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 8139 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 8140 | #if ENABLE_HUSH_FAST |
| 8141 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 8142 | #endif |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8143 | #if !BB_MMU |
| 8144 | G.argv0_for_re_execing = argv[0]; |
| 8145 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 8146 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8147 | shell_ver = xzalloc(sizeof(*shell_ver)); |
| 8148 | shell_ver->flg_export = 1; |
| 8149 | shell_ver->flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 8150 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 8151 | * therefore we xstrdup: */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8152 | shell_ver->varstr = xstrdup(hush_version_str); |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 8153 | /* Create shell local variables from the values |
| 8154 | * currently living in the environment */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 8155 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 8156 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8157 | G.top_var = shell_ver; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8158 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 8159 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8160 | if (e) while (*e) { |
| 8161 | char *value = strchr(*e, '='); |
| 8162 | if (value) { /* paranoia */ |
| 8163 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 8164 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 8165 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8166 | cur_var->max_len = strlen(*e); |
| 8167 | cur_var->flg_export = 1; |
| 8168 | } |
| 8169 | e++; |
| 8170 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 8171 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8172 | debug_printf_env("putenv '%s'\n", shell_ver->varstr); |
| 8173 | putenv(shell_ver->varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8174 | |
| 8175 | /* Export PWD */ |
| 8176 | set_pwd_var(/*exp:*/ 1); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 8177 | |
| 8178 | #if ENABLE_HUSH_BASH_COMPAT |
| 8179 | /* Set (but not export) HOSTNAME unless already set */ |
| 8180 | if (!get_local_var_value("HOSTNAME")) { |
| 8181 | struct utsname uts; |
| 8182 | uname(&uts); |
| 8183 | set_local_var_from_halves("HOSTNAME", uts.nodename); |
| 8184 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8185 | /* bash also exports SHLVL and _, |
| 8186 | * and sets (but doesn't export) the following variables: |
| 8187 | * BASH=/bin/bash |
| 8188 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 8189 | * BASH_VERSION='3.2.0(1)-release' |
| 8190 | * HOSTTYPE=i386 |
| 8191 | * MACHTYPE=i386-pc-linux-gnu |
| 8192 | * OSTYPE=linux-gnu |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8193 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8194 | * EUID=<NNNNN> |
| 8195 | * UID=<NNNNN> |
| 8196 | * GROUPS=() |
| 8197 | * LINES=<NNN> |
| 8198 | * COLUMNS=<NNN> |
| 8199 | * BASH_ARGC=() |
| 8200 | * BASH_ARGV=() |
| 8201 | * BASH_LINENO=() |
| 8202 | * BASH_SOURCE=() |
| 8203 | * DIRSTACK=() |
| 8204 | * PIPESTATUS=([0]="0") |
| 8205 | * HISTFILE=/<xxx>/.bash_history |
| 8206 | * HISTFILESIZE=500 |
| 8207 | * HISTSIZE=500 |
| 8208 | * MAILCHECK=60 |
| 8209 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 8210 | * SHELL=/bin/bash |
| 8211 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 8212 | * TERM=dumb |
| 8213 | * OPTERR=1 |
| 8214 | * OPTIND=1 |
| 8215 | * IFS=$' \t\n' |
| 8216 | * PS1='\s-\v\$ ' |
| 8217 | * PS2='> ' |
| 8218 | * PS4='+ ' |
| 8219 | */ |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 8220 | #endif |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8221 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 8222 | #if ENABLE_FEATURE_EDITING |
Denys Vlasenko | e45af7a | 2011-09-04 16:15:24 +0200 | [diff] [blame] | 8223 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 8224 | #endif |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 8225 | |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 8226 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 8227 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 8228 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 8229 | die_func = restore_ttypgrp_and__exit; |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 8230 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 8231 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8232 | * install_special_sighandlers() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 8233 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8234 | * 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] | 8235 | * in order to intercept (more) signals. |
| 8236 | */ |
| 8237 | |
| 8238 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 8239 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8240 | flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8241 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8242 | while (1) { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8243 | opt = getopt(argc, argv, "+c:xinsl" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8244 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 8245 | "<:$:R:V:" |
| 8246 | # if ENABLE_HUSH_FUNCTIONS |
| 8247 | "F:" |
| 8248 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8249 | #endif |
| 8250 | ); |
| 8251 | if (opt <= 0) |
| 8252 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8253 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8254 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8255 | /* Possibilities: |
| 8256 | * sh ... -c 'script' |
| 8257 | * sh ... -c 'script' ARG0 [ARG1...] |
| 8258 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8259 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8260 | * "" needs to be replaced with NULL |
| 8261 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8262 | * Note: the form without ARG0 never happens: |
| 8263 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8264 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8265 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8266 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8267 | G.root_ppid = getppid(); |
| 8268 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8269 | G.global_argv = argv + optind; |
| 8270 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8271 | if (builtin_argc) { |
| 8272 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 8273 | const struct built_in_command *x; |
| 8274 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8275 | install_special_sighandlers(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8276 | x = find_builtin(optarg); |
| 8277 | if (x) { /* paranoia */ |
| 8278 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 8279 | G.global_argv += builtin_argc; |
| 8280 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8281 | fflush_all(); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8282 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8283 | } |
| 8284 | goto final_return; |
| 8285 | } |
| 8286 | if (!G.global_argv[0]) { |
| 8287 | /* -c 'script' (no params): prevent empty $0 */ |
| 8288 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 8289 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 8290 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8291 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8292 | install_special_sighandlers(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8293 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8294 | goto final_return; |
| 8295 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 8296 | /* Well, we cannot just declare interactiveness, |
| 8297 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8298 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8299 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 8300 | case 's': |
| 8301 | /* "-s" means "read from stdin", but this is how we always |
| 8302 | * operate, so simply do nothing here. */ |
| 8303 | break; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8304 | case 'l': |
| 8305 | flags |= OPT_login; |
| 8306 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8307 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 8308 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 8309 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 8310 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8311 | case '$': { |
| 8312 | unsigned long long empty_trap_mask; |
| 8313 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 8314 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 8315 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8316 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 8317 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 8318 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 8319 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8320 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8321 | optarg++; |
| 8322 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8323 | optarg++; |
| 8324 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 8325 | if (empty_trap_mask != 0) { |
| 8326 | int sig; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8327 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8328 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 8329 | for (sig = 1; sig < NSIG; sig++) { |
| 8330 | if (empty_trap_mask & (1LL << sig)) { |
| 8331 | G.traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 8332 | install_sighandler(sig, SIG_IGN); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8333 | } |
| 8334 | } |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8335 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 8336 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 8337 | optarg++; |
| 8338 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 8339 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 8340 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8341 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8342 | case 'R': |
| 8343 | case 'V': |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8344 | set_local_var(xstrdup(optarg), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ opt == 'R'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8345 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 8346 | # if ENABLE_HUSH_FUNCTIONS |
| 8347 | case 'F': { |
| 8348 | struct function *funcp = new_function(optarg); |
| 8349 | /* funcp->name is already set to optarg */ |
| 8350 | /* funcp->body is set to NULL. It's a special case. */ |
| 8351 | funcp->body_as_string = argv[optind]; |
| 8352 | optind++; |
| 8353 | break; |
| 8354 | } |
| 8355 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8356 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8357 | case 'n': |
| 8358 | case 'x': |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8359 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8360 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8361 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 8362 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8363 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 8364 | " or: sh -c command [args]...\n\n"); |
| 8365 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 8366 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8367 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 8368 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8369 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8370 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8371 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8372 | /* Skip options. Try "hush -l": $1 should not be "-l"! */ |
| 8373 | G.global_argc = argc - (optind - 1); |
| 8374 | G.global_argv = argv + (optind - 1); |
| 8375 | G.global_argv[0] = argv[0]; |
| 8376 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8377 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8378 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8379 | G.root_ppid = getppid(); |
| 8380 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8381 | |
| 8382 | /* If we are login shell... */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8383 | if (flags & OPT_login) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8384 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8385 | debug_printf("sourcing /etc/profile\n"); |
| 8386 | input = fopen_for_read("/etc/profile"); |
| 8387 | if (input != NULL) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 8388 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8389 | install_special_sighandlers(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8390 | parse_and_run_file(input); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 8391 | fclose_and_forget(input); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8392 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8393 | /* bash: after sourcing /etc/profile, |
| 8394 | * tries to source (in the given order): |
| 8395 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 8396 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8397 | * bash also sources ~/.bash_logout on exit. |
| 8398 | * If called as sh, skips .bash_XXX files. |
| 8399 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8400 | } |
| 8401 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8402 | if (G.global_argv[1]) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8403 | FILE *input; |
| 8404 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 8405 | * "bash <script>" (which is never interactive (unless -i?)) |
| 8406 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8407 | * If called as sh, does the same but with $ENV. |
| 8408 | */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8409 | G.global_argc--; |
| 8410 | G.global_argv++; |
| 8411 | debug_printf("running script '%s'\n", G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 8412 | xfunc_error_retval = 127; /* for "hush /does/not/exist" case */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8413 | input = xfopen_for_read(G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 8414 | xfunc_error_retval = 1; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 8415 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8416 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8417 | parse_and_run_file(input); |
| 8418 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 8419 | fclose_and_forget(input); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8420 | #endif |
| 8421 | goto final_return; |
| 8422 | } |
| 8423 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 8424 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8425 | * NB: don't forget to (re)run install_special_sighandlers() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 8426 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8427 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 8428 | /* A shell is interactive if the '-i' flag was given, |
| 8429 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 8430 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8431 | * no arguments remaining or the -s flag given |
| 8432 | * standard input is a terminal |
| 8433 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8434 | * Refer to Posix.2, the description of the 'sh' utility. |
| 8435 | */ |
| 8436 | #if ENABLE_HUSH_JOB |
| 8437 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8438 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 8439 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 8440 | if (G_saved_tty_pgrp < 0) |
| 8441 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8442 | |
| 8443 | /* try to dup stdin to high fd#, >= 255 */ |
| 8444 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 8445 | if (G_interactive_fd < 0) { |
| 8446 | /* try to dup to any fd */ |
| 8447 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8448 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8449 | /* give up */ |
| 8450 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8451 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 8452 | } |
| 8453 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8454 | // TODO: track & disallow any attempts of user |
| 8455 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8456 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8457 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8458 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8459 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8460 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8461 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8462 | /* If we were run as 'hush &', sleep until we are |
| 8463 | * in the foreground (tty pgrp == our pgrp). |
| 8464 | * If we get started under a job aware app (like bash), |
| 8465 | * make sure we are now in charge so we don't fight over |
| 8466 | * who gets the foreground */ |
| 8467 | while (1) { |
| 8468 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8469 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 8470 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8471 | break; |
| 8472 | /* send TTIN to ourself (should stop us) */ |
| 8473 | kill(- shell_pgrp, SIGTTIN); |
| 8474 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8475 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8476 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8477 | /* Install more signal handlers */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8478 | install_special_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8479 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8480 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8481 | /* Set other signals to restore saved_tty_pgrp */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8482 | install_fatal_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8483 | /* Put ourselves in our own process group |
| 8484 | * (bash, too, does this only if ctty is available) */ |
| 8485 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 8486 | /* Grab control of the terminal */ |
| 8487 | tcsetpgrp(G_interactive_fd, getpid()); |
| 8488 | } |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 8489 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 8490 | |
| 8491 | # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 |
| 8492 | { |
| 8493 | const char *hp = get_local_var_value("HISTFILE"); |
| 8494 | if (!hp) { |
| 8495 | hp = get_local_var_value("HOME"); |
| 8496 | if (hp) |
| 8497 | hp = concat_path_file(hp, ".hush_history"); |
| 8498 | } else { |
| 8499 | hp = xstrdup(hp); |
| 8500 | } |
| 8501 | if (hp) { |
| 8502 | G.line_input_state->hist_file = hp; |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 8503 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 8504 | } |
| 8505 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 8506 | hp = get_local_var_value("HISTFILESIZE"); |
| 8507 | G.line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 8508 | # endif |
| 8509 | } |
| 8510 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8511 | } else { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8512 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8513 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8514 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8515 | /* No job control compiled in, only prompt/line editing */ |
| 8516 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8517 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 8518 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8519 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8520 | G_interactive_fd = dup(STDIN_FILENO); |
| 8521 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8522 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8523 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8524 | } |
| 8525 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8526 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8527 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8528 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8529 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8530 | #else |
| 8531 | /* We have interactiveness code disabled */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8532 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8533 | #endif |
| 8534 | /* bash: |
| 8535 | * if interactive but not a login shell, sources ~/.bashrc |
| 8536 | * (--norc turns this off, --rcfile <file> overrides) |
| 8537 | */ |
| 8538 | |
| 8539 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8540 | /* note: ash and hush share this string */ |
| 8541 | printf("\n\n%s %s\n" |
| 8542 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 8543 | "\n", |
| 8544 | bb_banner, |
| 8545 | "hush - the humble shell" |
| 8546 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 8547 | } |
| 8548 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8549 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8550 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8551 | final_return: |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8552 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8553 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 8554 | |
| 8555 | |
Denys Vlasenko | 1cc4b13 | 2009-08-21 00:05:51 +0200 | [diff] [blame] | 8556 | #if ENABLE_MSH |
| 8557 | int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 8558 | int msh_main(int argc, char **argv) |
| 8559 | { |
Denys Vlasenko | ed6ff5e | 2016-09-30 12:28:37 +0200 | [diff] [blame] | 8560 | bb_error_msg("msh is deprecated, please use hush instead"); |
Denys Vlasenko | 1cc4b13 | 2009-08-21 00:05:51 +0200 | [diff] [blame] | 8561 | return hush_main(argc, argv); |
| 8562 | } |
| 8563 | #endif |
| 8564 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8565 | |
| 8566 | /* |
| 8567 | * Built-ins |
| 8568 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8569 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8570 | { |
| 8571 | return 0; |
| 8572 | } |
| 8573 | |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 8574 | 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] | 8575 | { |
| 8576 | int argc = 0; |
| 8577 | while (*argv) { |
| 8578 | argc++; |
| 8579 | argv++; |
| 8580 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 8581 | return applet_main_func(argc, argv - argc); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 8582 | } |
| 8583 | |
| 8584 | static int FAST_FUNC builtin_test(char **argv) |
| 8585 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8586 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8587 | } |
| 8588 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8589 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8590 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8591 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8592 | } |
| 8593 | |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 8594 | #if ENABLE_PRINTF |
| 8595 | static int FAST_FUNC builtin_printf(char **argv) |
| 8596 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8597 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 8598 | } |
| 8599 | #endif |
| 8600 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8601 | static char **skip_dash_dash(char **argv) |
| 8602 | { |
| 8603 | argv++; |
| 8604 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 8605 | argv++; |
| 8606 | return argv; |
| 8607 | } |
| 8608 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8609 | static int FAST_FUNC builtin_eval(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8610 | { |
| 8611 | int rcode = EXIT_SUCCESS; |
| 8612 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8613 | argv = skip_dash_dash(argv); |
| 8614 | if (*argv) { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 8615 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8616 | /* bash: |
| 8617 | * eval "echo Hi; done" ("done" is syntax error): |
| 8618 | * "echo Hi" will not execute too. |
| 8619 | */ |
| 8620 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8621 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8622 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8623 | } |
| 8624 | return rcode; |
| 8625 | } |
| 8626 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8627 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8628 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8629 | const char *newdir; |
| 8630 | |
| 8631 | argv = skip_dash_dash(argv); |
| 8632 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8633 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8634 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8635 | * bash says "bash: cd: HOME not set" and does nothing |
| 8636 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8637 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 8638 | const char *home = get_local_var_value("HOME"); |
| 8639 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 8640 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8641 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8642 | /* Mimic bash message exactly */ |
| 8643 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8644 | return EXIT_FAILURE; |
| 8645 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8646 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 8647 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 8648 | * set it again, but do not export. bash does the same. |
| 8649 | */ |
| 8650 | set_pwd_var(/*exp:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8651 | return EXIT_SUCCESS; |
| 8652 | } |
| 8653 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8654 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8655 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8656 | argv = skip_dash_dash(argv); |
| 8657 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8658 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 8659 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 8660 | /* Careful: we can end up here after [v]fork. Do not restore |
| 8661 | * tty pgrp then, only top-level shell process does that */ |
| 8662 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 8663 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 8664 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 8665 | /* TODO: if exec fails, bash does NOT exit! We do. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8666 | * 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] | 8667 | * and tcsetpgrp, and this is inherently racy. |
| 8668 | */ |
| 8669 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8670 | } |
| 8671 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8672 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8673 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 8674 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8675 | |
| 8676 | /* interactive bash: |
| 8677 | * # trap "echo EEE" EXIT |
| 8678 | * # exit |
| 8679 | * exit |
| 8680 | * There are stopped jobs. |
| 8681 | * (if there are _stopped_ jobs, running ones don't count) |
| 8682 | * # exit |
| 8683 | * exit |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 8684 | * EEE (then bash exits) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8685 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 8686 | * 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] | 8687 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 8688 | |
| 8689 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8690 | argv = skip_dash_dash(argv); |
| 8691 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8692 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8693 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 8694 | xfunc_error_retval = 255; |
| 8695 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8696 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8697 | } |
| 8698 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8699 | static void print_escaped(const char *s) |
| 8700 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8701 | if (*s == '\'') |
| 8702 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8703 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8704 | const char *p = strchrnul(s, '\''); |
| 8705 | /* print 'xxxx', possibly just '' */ |
| 8706 | printf("'%.*s'", (int)(p - s), s); |
| 8707 | if (*p == '\0') |
| 8708 | break; |
| 8709 | s = p; |
| 8710 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8711 | /* s points to '; print "'''...'''" */ |
| 8712 | putchar('"'); |
| 8713 | do putchar('\''); while (*++s == '\''); |
| 8714 | putchar('"'); |
| 8715 | } while (*s); |
| 8716 | } |
| 8717 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8718 | #if !ENABLE_HUSH_LOCAL |
| 8719 | #define helper_export_local(argv, exp, lvl) \ |
| 8720 | helper_export_local(argv, exp) |
| 8721 | #endif |
| 8722 | static void helper_export_local(char **argv, int exp, int lvl) |
| 8723 | { |
| 8724 | do { |
| 8725 | char *name = *argv; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8726 | char *name_end = strchrnul(name, '='); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8727 | |
| 8728 | /* So far we do not check that name is valid (TODO?) */ |
| 8729 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8730 | if (*name_end == '\0') { |
| 8731 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8732 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8733 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 8734 | var = vpp ? *vpp : NULL; |
| 8735 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8736 | if (exp == -1) { /* unexporting? */ |
| 8737 | /* export -n NAME (without =VALUE) */ |
| 8738 | if (var) { |
| 8739 | var->flg_export = 0; |
| 8740 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 8741 | unsetenv(name); |
| 8742 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 8743 | continue; |
| 8744 | } |
| 8745 | if (exp == 1) { /* exporting? */ |
| 8746 | /* export NAME (without =VALUE) */ |
| 8747 | if (var) { |
| 8748 | var->flg_export = 1; |
| 8749 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 8750 | putenv(var->varstr); |
| 8751 | continue; |
| 8752 | } |
| 8753 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 8754 | #if ENABLE_HUSH_LOCAL |
| 8755 | if (exp == 0 /* local? */ |
| 8756 | && var && var->func_nest_level == lvl |
| 8757 | ) { |
| 8758 | /* "local x=abc; ...; local x" - ignore second local decl */ |
Denys Vlasenko | 80729a4 | 2016-10-02 22:33:15 +0200 | [diff] [blame] | 8759 | continue; |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 8760 | } |
| 8761 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8762 | /* Exporting non-existing variable. |
| 8763 | * bash does not put it in environment, |
| 8764 | * but remembers that it is exported, |
| 8765 | * and does put it in env when it is set later. |
| 8766 | * We just set it to "" and export. */ |
| 8767 | /* Or, it's "local NAME" (without =VALUE). |
| 8768 | * bash sets the value to "". */ |
| 8769 | name = xasprintf("%s=", name); |
| 8770 | } else { |
| 8771 | /* (Un)exporting/making local NAME=VALUE */ |
| 8772 | name = xstrdup(name); |
| 8773 | } |
| 8774 | set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0); |
| 8775 | } while (*++argv); |
| 8776 | } |
| 8777 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8778 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8779 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 8780 | unsigned opt_unexport; |
| 8781 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 8782 | #if ENABLE_HUSH_EXPORT_N |
| 8783 | /* "!": do not abort on errors */ |
| 8784 | opt_unexport = getopt32(argv, "!n"); |
| 8785 | if (opt_unexport == (uint32_t)-1) |
| 8786 | return EXIT_FAILURE; |
| 8787 | argv += optind; |
| 8788 | #else |
| 8789 | opt_unexport = 0; |
| 8790 | argv++; |
| 8791 | #endif |
| 8792 | |
| 8793 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8794 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8795 | if (e) { |
| 8796 | while (*e) { |
| 8797 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8798 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8799 | #else |
| 8800 | /* ash emits: export VAR='VAL' |
| 8801 | * bash: declare -x VAR="VAL" |
| 8802 | * we follow ash example */ |
| 8803 | const char *s = *e++; |
| 8804 | const char *p = strchr(s, '='); |
| 8805 | |
| 8806 | if (!p) /* wtf? take next variable */ |
| 8807 | continue; |
| 8808 | /* export var= */ |
| 8809 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8810 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8811 | putchar('\n'); |
| 8812 | #endif |
| 8813 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8814 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8815 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8816 | return EXIT_SUCCESS; |
| 8817 | } |
| 8818 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8819 | helper_export_local(argv, (opt_unexport ? -1 : 1), 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8820 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8821 | return EXIT_SUCCESS; |
| 8822 | } |
| 8823 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8824 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8825 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8826 | { |
| 8827 | if (G.func_nest_level == 0) { |
| 8828 | bb_error_msg("%s: not in a function", argv[0]); |
| 8829 | return EXIT_FAILURE; /* bash compat */ |
| 8830 | } |
| 8831 | helper_export_local(argv, 0, G.func_nest_level); |
| 8832 | return EXIT_SUCCESS; |
| 8833 | } |
| 8834 | #endif |
| 8835 | |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 8836 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
| 8837 | static int FAST_FUNC builtin_unset(char **argv) |
| 8838 | { |
| 8839 | int ret; |
| 8840 | unsigned opts; |
| 8841 | |
| 8842 | /* "!": do not abort on errors */ |
| 8843 | /* "+": stop at 1st non-option */ |
| 8844 | opts = getopt32(argv, "!+vf"); |
| 8845 | if (opts == (unsigned)-1) |
| 8846 | return EXIT_FAILURE; |
| 8847 | if (opts == 3) { |
| 8848 | bb_error_msg("unset: -v and -f are exclusive"); |
| 8849 | return EXIT_FAILURE; |
| 8850 | } |
| 8851 | argv += optind; |
| 8852 | |
| 8853 | ret = EXIT_SUCCESS; |
| 8854 | while (*argv) { |
| 8855 | if (!(opts & 2)) { /* not -f */ |
| 8856 | if (unset_local_var(*argv)) { |
| 8857 | /* unset <nonexistent_var> doesn't fail. |
| 8858 | * Error is when one tries to unset RO var. |
| 8859 | * Message was printed by unset_local_var. */ |
| 8860 | ret = EXIT_FAILURE; |
| 8861 | } |
| 8862 | } |
| 8863 | #if ENABLE_HUSH_FUNCTIONS |
| 8864 | else { |
| 8865 | unset_func(*argv); |
| 8866 | } |
| 8867 | #endif |
| 8868 | argv++; |
| 8869 | } |
| 8870 | return ret; |
| 8871 | } |
| 8872 | |
| 8873 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 8874 | * built-in 'set' handler |
| 8875 | * SUSv3 says: |
| 8876 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 8877 | * set [+abCefhmnuvx] [+o option] [argument...] |
| 8878 | * set -- [argument...] |
| 8879 | * set -o |
| 8880 | * set +o |
| 8881 | * Implementations shall support the options in both their hyphen and |
| 8882 | * plus-sign forms. These options can also be specified as options to sh. |
| 8883 | * Examples: |
| 8884 | * Write out all variables and their values: set |
| 8885 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 8886 | * Turn on the -x and -v options: set -xv |
| 8887 | * Unset all positional parameters: set -- |
| 8888 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 8889 | * Set the positional parameters to the expansion of x, even if x expands |
| 8890 | * with a leading '-' or '+': set -- $x |
| 8891 | * |
| 8892 | * So far, we only support "set -- [argument...]" and some of the short names. |
| 8893 | */ |
| 8894 | static int FAST_FUNC builtin_set(char **argv) |
| 8895 | { |
| 8896 | int n; |
| 8897 | char **pp, **g_argv; |
| 8898 | char *arg = *++argv; |
| 8899 | |
| 8900 | if (arg == NULL) { |
| 8901 | struct variable *e; |
| 8902 | for (e = G.top_var; e; e = e->next) |
| 8903 | puts(e->varstr); |
| 8904 | return EXIT_SUCCESS; |
| 8905 | } |
| 8906 | |
| 8907 | do { |
| 8908 | if (strcmp(arg, "--") == 0) { |
| 8909 | ++argv; |
| 8910 | goto set_argv; |
| 8911 | } |
| 8912 | if (arg[0] != '+' && arg[0] != '-') |
| 8913 | break; |
| 8914 | for (n = 1; arg[n]; ++n) { |
| 8915 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) |
| 8916 | goto error; |
| 8917 | if (arg[n] == 'o' && argv[1]) |
| 8918 | argv++; |
| 8919 | } |
| 8920 | } while ((arg = *++argv) != NULL); |
| 8921 | /* Now argv[0] is 1st argument */ |
| 8922 | |
| 8923 | if (arg == NULL) |
| 8924 | return EXIT_SUCCESS; |
| 8925 | set_argv: |
| 8926 | |
| 8927 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 8928 | g_argv = G.global_argv; |
| 8929 | if (G.global_args_malloced) { |
| 8930 | pp = g_argv; |
| 8931 | while (*++pp) |
| 8932 | free(*pp); |
| 8933 | g_argv[1] = NULL; |
| 8934 | } else { |
| 8935 | G.global_args_malloced = 1; |
| 8936 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 8937 | pp[0] = g_argv[0]; /* retain $0 */ |
| 8938 | g_argv = pp; |
| 8939 | } |
| 8940 | /* This realloc's G.global_argv */ |
| 8941 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 8942 | |
| 8943 | n = 1; |
| 8944 | while (*++pp) |
| 8945 | n++; |
| 8946 | G.global_argc = n; |
| 8947 | |
| 8948 | return EXIT_SUCCESS; |
| 8949 | |
| 8950 | /* Nothing known, so abort */ |
| 8951 | error: |
| 8952 | bb_error_msg("set: %s: invalid option", arg); |
| 8953 | return EXIT_FAILURE; |
| 8954 | } |
| 8955 | |
| 8956 | static int FAST_FUNC builtin_shift(char **argv) |
| 8957 | { |
| 8958 | int n = 1; |
| 8959 | argv = skip_dash_dash(argv); |
| 8960 | if (argv[0]) { |
| 8961 | n = atoi(argv[0]); |
| 8962 | } |
| 8963 | if (n >= 0 && n < G.global_argc) { |
| 8964 | if (G.global_args_malloced) { |
| 8965 | int m = 1; |
| 8966 | while (m <= n) |
| 8967 | free(G.global_argv[m++]); |
| 8968 | } |
| 8969 | G.global_argc -= n; |
| 8970 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 8971 | G.global_argc * sizeof(G.global_argv[0])); |
| 8972 | return EXIT_SUCCESS; |
| 8973 | } |
| 8974 | return EXIT_FAILURE; |
| 8975 | } |
| 8976 | |
| 8977 | /* Interruptibility of read builtin in bash |
| 8978 | * (tested on bash-4.2.8 by sending signals (not by ^C)): |
| 8979 | * |
| 8980 | * Empty trap makes read ignore corresponding signal, for any signal. |
| 8981 | * |
| 8982 | * SIGINT: |
| 8983 | * - terminates non-interactive shell; |
| 8984 | * - interrupts read in interactive shell; |
| 8985 | * if it has non-empty trap: |
| 8986 | * - executes trap and returns to command prompt in interactive shell; |
| 8987 | * - executes trap and returns to read in non-interactive shell; |
| 8988 | * SIGTERM: |
| 8989 | * - is ignored (does not interrupt) read in interactive shell; |
| 8990 | * - terminates non-interactive shell; |
| 8991 | * if it has non-empty trap: |
| 8992 | * - executes trap and returns to read; |
| 8993 | * SIGHUP: |
| 8994 | * - terminates shell (regardless of interactivity); |
| 8995 | * if it has non-empty trap: |
| 8996 | * - executes trap and returns to read; |
| 8997 | */ |
| 8998 | static int FAST_FUNC builtin_read(char **argv) |
| 8999 | { |
| 9000 | const char *r; |
| 9001 | char *opt_n = NULL; |
| 9002 | char *opt_p = NULL; |
| 9003 | char *opt_t = NULL; |
| 9004 | char *opt_u = NULL; |
| 9005 | const char *ifs; |
| 9006 | int read_flags; |
| 9007 | |
| 9008 | /* "!": do not abort on errors. |
| 9009 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 9010 | */ |
| 9011 | read_flags = getopt32(argv, "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u); |
| 9012 | if (read_flags == (uint32_t)-1) |
| 9013 | return EXIT_FAILURE; |
| 9014 | argv += optind; |
| 9015 | ifs = get_local_var_value("IFS"); /* can be NULL */ |
| 9016 | |
| 9017 | again: |
| 9018 | r = shell_builtin_read(set_local_var_from_halves, |
| 9019 | argv, |
| 9020 | ifs, |
| 9021 | read_flags, |
| 9022 | opt_n, |
| 9023 | opt_p, |
| 9024 | opt_t, |
| 9025 | opt_u |
| 9026 | ); |
| 9027 | |
| 9028 | if ((uintptr_t)r == 1 && errno == EINTR) { |
| 9029 | unsigned sig = check_and_run_traps(); |
| 9030 | if (sig && sig != SIGINT) |
| 9031 | goto again; |
| 9032 | } |
| 9033 | |
| 9034 | if ((uintptr_t)r > 1) { |
| 9035 | bb_error_msg("%s", r); |
| 9036 | r = (char*)(uintptr_t)1; |
| 9037 | } |
| 9038 | |
| 9039 | return (uintptr_t)r; |
| 9040 | } |
| 9041 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9042 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9043 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9044 | int sig; |
| 9045 | char *new_cmd; |
| 9046 | |
| 9047 | if (!G.traps) |
| 9048 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 9049 | |
| 9050 | argv++; |
| 9051 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 9052 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9053 | /* No args: print all trapped */ |
| 9054 | for (i = 0; i < NSIG; ++i) { |
| 9055 | if (G.traps[i]) { |
| 9056 | printf("trap -- "); |
| 9057 | print_escaped(G.traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 9058 | /* note: bash adds "SIG", but only if invoked |
| 9059 | * as "bash". If called as "sh", or if set -o posix, |
| 9060 | * then it prints short signal names. |
| 9061 | * We are printing short names: */ |
| 9062 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9063 | } |
| 9064 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 9065 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9066 | return EXIT_SUCCESS; |
| 9067 | } |
| 9068 | |
| 9069 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9070 | /* If first arg is a number: reset all specified signals */ |
| 9071 | sig = bb_strtou(*argv, NULL, 10); |
| 9072 | if (errno == 0) { |
| 9073 | int ret; |
| 9074 | process_sig_list: |
| 9075 | ret = EXIT_SUCCESS; |
| 9076 | while (*argv) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9077 | sighandler_t handler; |
| 9078 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9079 | sig = get_signum(*argv++); |
| 9080 | if (sig < 0 || sig >= NSIG) { |
| 9081 | ret = EXIT_FAILURE; |
| 9082 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 9083 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9084 | continue; |
| 9085 | } |
| 9086 | |
| 9087 | free(G.traps[sig]); |
| 9088 | G.traps[sig] = xstrdup(new_cmd); |
| 9089 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9090 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9091 | get_signame(sig), sig, G.traps[sig]); |
| 9092 | |
| 9093 | /* There is no signal for 0 (EXIT) */ |
| 9094 | if (sig == 0) |
| 9095 | continue; |
| 9096 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9097 | if (new_cmd) |
| 9098 | handler = (new_cmd[0] ? record_pending_signo : SIG_IGN); |
| 9099 | else |
| 9100 | /* We are removing trap handler */ |
| 9101 | handler = pick_sighandler(sig); |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9102 | install_sighandler(sig, handler); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9103 | } |
| 9104 | return ret; |
| 9105 | } |
| 9106 | |
| 9107 | if (!argv[1]) { /* no second arg */ |
| 9108 | bb_error_msg("trap: invalid arguments"); |
| 9109 | return EXIT_FAILURE; |
| 9110 | } |
| 9111 | |
| 9112 | /* First arg is "-": reset all specified to default */ |
| 9113 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 9114 | /* Everything else: set arg as signal handler |
| 9115 | * (includes "" case, which ignores signal) */ |
| 9116 | if (argv[0][0] == '-') { |
| 9117 | if (argv[0][1] == '\0') { /* "-" */ |
| 9118 | /* new_cmd remains NULL: "reset these sigs" */ |
| 9119 | goto reset_traps; |
| 9120 | } |
| 9121 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 9122 | argv++; |
| 9123 | } |
| 9124 | /* else: "-something", no special meaning */ |
| 9125 | } |
| 9126 | new_cmd = *argv; |
| 9127 | reset_traps: |
| 9128 | argv++; |
| 9129 | goto process_sig_list; |
| 9130 | } |
| 9131 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9132 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9133 | static int FAST_FUNC builtin_type(char **argv) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9134 | { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9135 | int ret = EXIT_SUCCESS; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9136 | |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9137 | while (*++argv) { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9138 | const char *type; |
Denys Vlasenko | 171932d | 2009-05-28 17:07:22 +0200 | [diff] [blame] | 9139 | char *path = NULL; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9140 | |
| 9141 | if (0) {} /* make conditional compile easier below */ |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9142 | /*else if (find_alias(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9143 | type = "an alias";*/ |
| 9144 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9145 | else if (find_function(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9146 | type = "a function"; |
| 9147 | #endif |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9148 | else if (find_builtin(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9149 | type = "a shell builtin"; |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9150 | else if ((path = find_in_path(*argv)) != NULL) |
| 9151 | type = path; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 9152 | else { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9153 | bb_error_msg("type: %s: not found", *argv); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9154 | ret = EXIT_FAILURE; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 9155 | continue; |
| 9156 | } |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9157 | |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 9158 | printf("%s is %s\n", *argv, type); |
| 9159 | free(path); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9160 | } |
| 9161 | |
| 9162 | return ret; |
| 9163 | } |
| 9164 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9165 | #if ENABLE_HUSH_JOB |
| 9166 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9167 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9168 | { |
| 9169 | int i, jobnum; |
| 9170 | struct pipe *pi; |
| 9171 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9172 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9173 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9174 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9175 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 9176 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9177 | for (pi = G.job_list; pi; pi = pi->next) { |
| 9178 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9179 | goto found; |
| 9180 | } |
| 9181 | } |
| 9182 | bb_error_msg("%s: no current job", argv[0]); |
| 9183 | return EXIT_FAILURE; |
| 9184 | } |
| 9185 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 9186 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 9187 | return EXIT_FAILURE; |
| 9188 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9189 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9190 | if (pi->jobid == jobnum) { |
| 9191 | goto found; |
| 9192 | } |
| 9193 | } |
| 9194 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 9195 | return EXIT_FAILURE; |
| 9196 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 9197 | /* TODO: bash prints a string representation |
| 9198 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9199 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9200 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9201 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9202 | } |
| 9203 | |
| 9204 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 9205 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 9206 | for (i = 0; i < pi->num_cmds; i++) { |
| 9207 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9208 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 9209 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9210 | |
| 9211 | i = kill(- pi->pgrp, SIGCONT); |
| 9212 | if (i < 0) { |
| 9213 | if (errno == ESRCH) { |
| 9214 | delete_finished_bg_job(pi); |
| 9215 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9216 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 9217 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9218 | } |
| 9219 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 9220 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9221 | remove_bg_job(pi); |
| 9222 | return checkjobs_and_fg_shell(pi); |
| 9223 | } |
| 9224 | return EXIT_SUCCESS; |
| 9225 | } |
| 9226 | #endif |
| 9227 | |
| 9228 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9229 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9230 | { |
| 9231 | const struct built_in_command *x; |
| 9232 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9233 | printf( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 9234 | "Built-in commands:\n" |
| 9235 | "------------------\n"); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9236 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9237 | if (x->b_descr) |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9238 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9239 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9240 | return EXIT_SUCCESS; |
| 9241 | } |
| 9242 | #endif |
| 9243 | |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 9244 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 9245 | static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM) |
| 9246 | { |
| 9247 | show_history(G.line_input_state); |
| 9248 | return EXIT_SUCCESS; |
| 9249 | } |
| 9250 | #endif |
| 9251 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9252 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9253 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9254 | { |
| 9255 | struct pipe *job; |
| 9256 | const char *status_string; |
| 9257 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9258 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 9259 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9260 | status_string = "Stopped"; |
| 9261 | else |
| 9262 | status_string = "Running"; |
| 9263 | |
| 9264 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 9265 | } |
| 9266 | return EXIT_SUCCESS; |
| 9267 | } |
| 9268 | #endif |
| 9269 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 9270 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9271 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 9272 | { |
| 9273 | void *p; |
| 9274 | unsigned long l; |
| 9275 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9276 | # ifdef M_TRIM_THRESHOLD |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 9277 | /* Optional. Reduces probability of false positives */ |
| 9278 | malloc_trim(0); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9279 | # endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 9280 | /* Crude attempt to find where "free memory" starts, |
| 9281 | * sans fragmentation. */ |
| 9282 | p = malloc(240); |
| 9283 | l = (unsigned long)p; |
| 9284 | free(p); |
| 9285 | p = malloc(3400); |
| 9286 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 9287 | free(p); |
| 9288 | |
Denys Vlasenko | 7f0ebbc | 2016-10-03 17:42:53 +0200 | [diff] [blame] | 9289 | |
| 9290 | # if 0 /* debug */ |
| 9291 | { |
| 9292 | struct mallinfo mi = mallinfo(); |
| 9293 | printf("top alloc:0x%lx malloced:%d+%d=%d\n", l, |
| 9294 | mi.arena, mi.hblkhd, mi.arena + mi.hblkhd); |
| 9295 | } |
| 9296 | # endif |
| 9297 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 9298 | if (!G.memleak_value) |
| 9299 | G.memleak_value = l; |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 9300 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 9301 | l -= G.memleak_value; |
| 9302 | if ((long)l < 0) |
| 9303 | l = 0; |
| 9304 | l /= 1024; |
| 9305 | if (l > 127) |
| 9306 | l = 127; |
| 9307 | |
| 9308 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 9309 | return l; |
| 9310 | } |
| 9311 | #endif |
| 9312 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9313 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9314 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9315 | puts(get_cwd(0)); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9316 | return EXIT_SUCCESS; |
| 9317 | } |
| 9318 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9319 | static int FAST_FUNC builtin_source(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9320 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 9321 | char *arg_path, *filename; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9322 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 9323 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 9324 | #if ENABLE_HUSH_FUNCTIONS |
| 9325 | smallint sv_flg; |
| 9326 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9327 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9328 | argv = skip_dash_dash(argv); |
| 9329 | filename = argv[0]; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 9330 | if (!filename) { |
| 9331 | /* bash says: "bash: .: filename argument required" */ |
| 9332 | return 2; /* bash compat */ |
| 9333 | } |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9334 | arg_path = NULL; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 9335 | if (!strchr(filename, '/')) { |
| 9336 | arg_path = find_in_path(filename); |
| 9337 | if (arg_path) |
| 9338 | filename = arg_path; |
| 9339 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9340 | input = remember_FILE(fopen_or_warn(filename, "r")); |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 9341 | free(arg_path); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9342 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9343 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denys Vlasenko | 88b532d | 2013-03-17 14:11:04 +0100 | [diff] [blame] | 9344 | /* POSIX: non-interactive shell should abort here, |
| 9345 | * not merely fail. So far no one complained :) |
| 9346 | */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9347 | return EXIT_FAILURE; |
| 9348 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9349 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 9350 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9351 | sv_flg = G_flag_return_in_progress; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9352 | /* "we are inside sourced file, ok to use return" */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9353 | G_flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 9354 | #endif |
Denys Vlasenko | 88b532d | 2013-03-17 14:11:04 +0100 | [diff] [blame] | 9355 | if (argv[1]) |
| 9356 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9357 | |
Denys Vlasenko | 992e0ff | 2016-09-29 01:27:09 +0200 | [diff] [blame] | 9358 | /* "false; . ./empty_line; echo Zero:$?" should print 0 */ |
| 9359 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9360 | parse_and_run_file(input); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9361 | fclose_and_forget(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 9362 | |
Denys Vlasenko | 88b532d | 2013-03-17 14:11:04 +0100 | [diff] [blame] | 9363 | if (argv[1]) |
| 9364 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 9365 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9366 | G_flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 9367 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9368 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9369 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9370 | } |
| 9371 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9372 | static int FAST_FUNC builtin_umask(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9373 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9374 | int rc; |
| 9375 | mode_t mask; |
| 9376 | |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 9377 | rc = 1; |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9378 | mask = umask(0); |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9379 | argv = skip_dash_dash(argv); |
| 9380 | if (argv[0]) { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9381 | mode_t old_mask = mask; |
| 9382 | |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 9383 | /* numeric umasks are taken as-is */ |
| 9384 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
| 9385 | if (!isdigit(argv[0][0])) |
| 9386 | mask ^= 0777; |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 9387 | mask = bb_parse_mode(argv[0], mask); |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 9388 | if (!isdigit(argv[0][0])) |
| 9389 | mask ^= 0777; |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 9390 | if ((unsigned)mask > 0777) { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9391 | mask = old_mask; |
| 9392 | /* bash messages: |
| 9393 | * bash: umask: 'q': invalid symbolic mode operator |
| 9394 | * bash: umask: 999: octal number out of range |
| 9395 | */ |
Denys Vlasenko | 44c86ce | 2010-05-20 04:22:55 +0200 | [diff] [blame] | 9396 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 9397 | rc = 0; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9398 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9399 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9400 | /* Mimic bash */ |
| 9401 | printf("%04o\n", (unsigned) mask); |
| 9402 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9403 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9404 | umask(mask); |
| 9405 | |
| 9406 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9407 | } |
| 9408 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9409 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9410 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9411 | { |
| 9412 | int ret = EXIT_SUCCESS; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9413 | int status; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9414 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9415 | argv = skip_dash_dash(argv); |
| 9416 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9417 | /* Don't care about wait results */ |
| 9418 | /* Note 1: must wait until there are no more children */ |
| 9419 | /* Note 2: must be interruptible */ |
| 9420 | /* Examples: |
| 9421 | * $ sleep 3 & sleep 6 & wait |
| 9422 | * [1] 30934 sleep 3 |
| 9423 | * [2] 30935 sleep 6 |
| 9424 | * [1] Done sleep 3 |
| 9425 | * [2] Done sleep 6 |
| 9426 | * $ sleep 3 & sleep 6 & wait |
| 9427 | * [1] 30936 sleep 3 |
| 9428 | * [2] 30937 sleep 6 |
| 9429 | * [1] Done sleep 3 |
| 9430 | * ^C <-- after ~4 sec from keyboard |
| 9431 | * $ |
| 9432 | */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9433 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9434 | int sig; |
| 9435 | sigset_t oldset, allsigs; |
| 9436 | |
| 9437 | /* waitpid is not interruptible by SA_RESTARTed |
| 9438 | * signals which we use. Thus, this ugly dance: |
| 9439 | */ |
| 9440 | |
| 9441 | /* Make sure possible SIGCHLD is stored in kernel's |
| 9442 | * pending signal mask before we call waitpid. |
| 9443 | * Or else we may race with SIGCHLD, lose it, |
| 9444 | * and get stuck in sigwaitinfo... |
| 9445 | */ |
| 9446 | sigfillset(&allsigs); |
| 9447 | sigprocmask(SIG_SETMASK, &allsigs, &oldset); |
| 9448 | |
| 9449 | if (!sigisemptyset(&G.pending_set)) { |
| 9450 | /* Crap! we raced with some signal! */ |
| 9451 | // sig = 0; |
| 9452 | goto restore; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9453 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9454 | |
| 9455 | checkjobs(NULL); /* waitpid(WNOHANG) inside */ |
| 9456 | if (errno == ECHILD) { |
| 9457 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 9458 | break; |
| 9459 | } |
| 9460 | |
| 9461 | /* Wait for SIGCHLD or any other signal */ |
| 9462 | //sig = sigwaitinfo(&allsigs, NULL); |
| 9463 | /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */ |
| 9464 | /* Note: sigsuspend invokes signal handler */ |
| 9465 | sigsuspend(&oldset); |
| 9466 | restore: |
| 9467 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 9468 | |
| 9469 | /* So, did we get a signal? */ |
| 9470 | //if (sig > 0) |
| 9471 | // raise(sig); /* run handler */ |
| 9472 | sig = check_and_run_traps(); |
| 9473 | if (sig /*&& sig != SIGCHLD - always true */) { |
| 9474 | /* see note 2 */ |
| 9475 | ret = 128 + sig; |
| 9476 | break; |
| 9477 | } |
| 9478 | /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9479 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9480 | return ret; |
| 9481 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9482 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9483 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9484 | while (*argv) { |
| 9485 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 9486 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9487 | /* mimic bash message */ |
| 9488 | bb_error_msg("wait: '%s': not a pid or valid job spec", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9489 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9490 | } |
| 9491 | if (waitpid(pid, &status, 0) == pid) { |
Denys Vlasenko | 85378cd | 2015-10-11 21:47:11 +0200 | [diff] [blame] | 9492 | ret = WEXITSTATUS(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9493 | if (WIFSIGNALED(status)) |
| 9494 | ret = 128 + WTERMSIG(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9495 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9496 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9497 | ret = 127; |
| 9498 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9499 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9500 | } |
| 9501 | |
| 9502 | return ret; |
| 9503 | } |
| 9504 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9505 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 9506 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 9507 | { |
| 9508 | if (argv[1]) { |
| 9509 | def = bb_strtou(argv[1], NULL, 10); |
| 9510 | if (errno || def < def_min || argv[2]) { |
| 9511 | bb_error_msg("%s: bad arguments", argv[0]); |
| 9512 | def = UINT_MAX; |
| 9513 | } |
| 9514 | } |
| 9515 | return def; |
| 9516 | } |
| 9517 | #endif |
| 9518 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 9519 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9520 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9521 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9522 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9523 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 9524 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 9525 | /* if we came from builtin_continue(), need to undo "= 1" */ |
| 9526 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 9527 | return EXIT_SUCCESS; /* bash compat */ |
| 9528 | } |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 9529 | G.flag_break_continue++; /* BC_BREAK = 1, or BC_CONTINUE = 2 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9530 | |
| 9531 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 9532 | if (depth == UINT_MAX) |
| 9533 | G.flag_break_continue = BC_BREAK; |
| 9534 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9535 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9536 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9537 | return EXIT_SUCCESS; |
| 9538 | } |
| 9539 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9540 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9541 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 9542 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 9543 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9544 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 9545 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9546 | |
| 9547 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9548 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9549 | { |
| 9550 | int rc; |
| 9551 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9552 | if (G_flag_return_in_progress != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9553 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 9554 | return EXIT_FAILURE; /* bash compat */ |
| 9555 | } |
| 9556 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9557 | G_flag_return_in_progress = 1; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9558 | |
| 9559 | /* bash: |
| 9560 | * out of range: wraps around at 256, does not error out |
| 9561 | * non-numeric param: |
| 9562 | * f() { false; return qwe; }; f; echo $? |
| 9563 | * bash: return: qwe: numeric argument required <== we do this |
| 9564 | * 255 <== we also do this |
| 9565 | */ |
| 9566 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 9567 | return rc; |
| 9568 | } |
| 9569 | #endif |