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 | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 84 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
Denys Vlasenko | 8da415e | 2010-12-05 01:30:14 +0100 | [diff] [blame] | 85 | #if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ |
| 86 | || defined(__APPLE__) \ |
| 87 | ) |
| 88 | # include <malloc.h> /* for malloc_trim */ |
| 89 | #endif |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 90 | #include <glob.h> |
| 91 | /* #include <dmalloc.h> */ |
| 92 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 93 | # include <fnmatch.h> |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 94 | #endif |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 95 | |
| 96 | #include "shell_common.h" |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 97 | #include "math.h" |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 98 | #include "match.h" |
Denys Vlasenko | cbe0b7f | 2009-10-09 22:00:58 +0200 | [diff] [blame] | 99 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 100 | # include "random.h" |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 101 | #else |
| 102 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 103 | #endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 104 | #ifndef PIPE_BUF |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 105 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 106 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 107 | |
Denys Vlasenko | b9f2d9f | 2011-01-18 13:58:01 +0100 | [diff] [blame^] | 108 | //applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP)) |
| 109 | //applet:IF_MSH(APPLET(msh, BB_DIR_BIN, BB_SUID_DROP)) |
| 110 | //applet:IF_FEATURE_SH_IS_HUSH(APPLET_ODDNAME(sh, hush, BB_DIR_BIN, BB_SUID_DROP, sh)) |
| 111 | //applet:IF_FEATURE_BASH_IS_HUSH(APPLET_ODDNAME(bash, hush, BB_DIR_BIN, BB_SUID_DROP, bash)) |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 112 | |
| 113 | //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o |
| 114 | //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o |
| 115 | |
| 116 | //config:config HUSH |
| 117 | //config: bool "hush" |
| 118 | //config: default y |
| 119 | //config: help |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 120 | //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] | 121 | //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, |
| 122 | //config: case/esac. Redirections, here documents, $((arithmetic)) |
| 123 | //config: and functions are supported. |
| 124 | //config: |
| 125 | //config: It will compile and work on no-mmu systems. |
| 126 | //config: |
Denys Vlasenko | e2069fb | 2010-10-04 00:01:47 +0200 | [diff] [blame] | 127 | //config: It does not handle select, aliases, tilde expansion, |
| 128 | //config: &>file and >&file redirection of stdout+stderr. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 129 | //config: |
| 130 | //config:config HUSH_BASH_COMPAT |
| 131 | //config: bool "bash-compatible extensions" |
| 132 | //config: default y |
| 133 | //config: depends on HUSH |
| 134 | //config: help |
| 135 | //config: Enable bash-compatible extensions. |
| 136 | //config: |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 137 | //config:config HUSH_BRACE_EXPANSION |
| 138 | //config: bool "Brace expansion" |
| 139 | //config: default y |
| 140 | //config: depends on HUSH_BASH_COMPAT |
| 141 | //config: help |
| 142 | //config: Enable {abc,def} extension. |
| 143 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 144 | //config:config HUSH_HELP |
| 145 | //config: bool "help builtin" |
| 146 | //config: default y |
| 147 | //config: depends on HUSH |
| 148 | //config: help |
| 149 | //config: Enable help builtin in hush. Code size + ~1 kbyte. |
| 150 | //config: |
| 151 | //config:config HUSH_INTERACTIVE |
| 152 | //config: bool "Interactive mode" |
| 153 | //config: default y |
| 154 | //config: depends on HUSH |
| 155 | //config: help |
| 156 | //config: Enable interactive mode (prompt and command editing). |
| 157 | //config: Without this, hush simply reads and executes commands |
| 158 | //config: from stdin just like a shell script from a file. |
| 159 | //config: No prompt, no PS1/PS2 magic shell variables. |
| 160 | //config: |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 161 | //config:config HUSH_SAVEHISTORY |
| 162 | //config: bool "Save command history to .hush_history" |
| 163 | //config: default y |
| 164 | //config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY |
| 165 | //config: help |
| 166 | //config: Enable history saving in hush. |
| 167 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 168 | //config:config HUSH_JOB |
| 169 | //config: bool "Job control" |
| 170 | //config: default y |
| 171 | //config: depends on HUSH_INTERACTIVE |
| 172 | //config: help |
| 173 | //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current |
| 174 | //config: command (not entire shell), fg/bg builtins work. Without this option, |
| 175 | //config: "cmd &" still works by simply spawning a process and immediately |
| 176 | //config: prompting for next command (or executing next command in a script), |
| 177 | //config: but no separate process group is formed. |
| 178 | //config: |
| 179 | //config:config HUSH_TICK |
| 180 | //config: bool "Process substitution" |
| 181 | //config: default y |
| 182 | //config: depends on HUSH |
| 183 | //config: help |
| 184 | //config: Enable process substitution `command` and $(command) in hush. |
| 185 | //config: |
| 186 | //config:config HUSH_IF |
| 187 | //config: bool "Support if/then/elif/else/fi" |
| 188 | //config: default y |
| 189 | //config: depends on HUSH |
| 190 | //config: help |
| 191 | //config: Enable if/then/elif/else/fi in hush. |
| 192 | //config: |
| 193 | //config:config HUSH_LOOPS |
| 194 | //config: bool "Support for, while and until loops" |
| 195 | //config: default y |
| 196 | //config: depends on HUSH |
| 197 | //config: help |
| 198 | //config: Enable for, while and until loops in hush. |
| 199 | //config: |
| 200 | //config:config HUSH_CASE |
| 201 | //config: bool "Support case ... esac statement" |
| 202 | //config: default y |
| 203 | //config: depends on HUSH |
| 204 | //config: help |
| 205 | //config: Enable case ... esac statement in hush. +400 bytes. |
| 206 | //config: |
| 207 | //config:config HUSH_FUNCTIONS |
| 208 | //config: bool "Support funcname() { commands; } syntax" |
| 209 | //config: default y |
| 210 | //config: depends on HUSH |
| 211 | //config: help |
| 212 | //config: Enable support for shell functions in hush. +800 bytes. |
| 213 | //config: |
| 214 | //config:config HUSH_LOCAL |
| 215 | //config: bool "Support local builtin" |
| 216 | //config: default y |
| 217 | //config: depends on HUSH_FUNCTIONS |
| 218 | //config: help |
| 219 | //config: Enable support for local variables in functions. |
| 220 | //config: |
| 221 | //config:config HUSH_RANDOM_SUPPORT |
| 222 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 223 | //config: default y |
| 224 | //config: depends on HUSH |
| 225 | //config: help |
| 226 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 227 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
| 228 | //config: |
| 229 | //config:config HUSH_EXPORT_N |
| 230 | //config: bool "Support 'export -n' option" |
| 231 | //config: default y |
| 232 | //config: depends on HUSH |
| 233 | //config: help |
| 234 | //config: export -n unexports variables. It is a bash extension. |
| 235 | //config: |
| 236 | //config:config HUSH_MODE_X |
| 237 | //config: bool "Support 'hush -x' option and 'set -x' command" |
| 238 | //config: default y |
| 239 | //config: depends on HUSH |
| 240 | //config: help |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 241 | //config: This instructs hush to print commands before execution. |
| 242 | //config: Adds ~300 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 243 | //config: |
Denys Vlasenko | 6adf2aa | 2010-07-16 19:26:38 +0200 | [diff] [blame] | 244 | //config:config MSH |
| 245 | //config: bool "msh (deprecated: aliased to hush)" |
| 246 | //config: default n |
| 247 | //config: select HUSH |
| 248 | //config: help |
| 249 | //config: msh is deprecated and will be removed, please migrate to hush. |
| 250 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 251 | |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 252 | /* -i (interactive) and -s (read stdin) are also accepted, |
| 253 | * but currently do nothing, therefore aren't shown in help. |
| 254 | * NOMMU-specific options are not meant to be used by users, |
| 255 | * therefore we don't show them either. |
| 256 | */ |
| 257 | //usage:#define hush_trivial_usage |
| 258 | //usage: "[-nx] [-c SCRIPT]" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 259 | //usage:#define hush_full_usage "" |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 260 | //usage:#define msh_trivial_usage hush_trivial_usage |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 261 | //usage:#define msh_full_usage "" |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 262 | //usage:#define sh_trivial_usage NOUSAGE_STR |
| 263 | //usage:#define sh_full_usage "" |
| 264 | //usage:#define bash_trivial_usage NOUSAGE_STR |
| 265 | //usage:#define bash_full_usage "" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 266 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 267 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 268 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 269 | #define LEAK_HUNTING 0 |
| 270 | #define BUILD_AS_NOMMU 0 |
| 271 | /* Enable/disable sanity checks. Ok to enable in production, |
| 272 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 273 | * Keeping 1 for now even in released versions. |
| 274 | */ |
| 275 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 276 | /* Slightly bigger (+200 bytes), but faster hush. |
| 277 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 278 | * which allows us to do fewer waitpid's. |
| 279 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 280 | * and therefore waitpid will return the same result as last time) |
| 281 | */ |
| 282 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 283 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 284 | * So far ${var%...} ops are always enabled: |
| 285 | */ |
| 286 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 287 | |
| 288 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 289 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 290 | # undef BB_MMU |
| 291 | # undef USE_FOR_NOMMU |
| 292 | # undef USE_FOR_MMU |
| 293 | # define BB_MMU 0 |
| 294 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 295 | # define USE_FOR_MMU(...) |
| 296 | #endif |
| 297 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 298 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 299 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 300 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 301 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 302 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 303 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 304 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 305 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 306 | # define IF_FEATURE_SH_STANDALONE(...) |
| 307 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 308 | #endif |
| 309 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 310 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 311 | # undef ENABLE_FEATURE_EDITING |
| 312 | # define ENABLE_FEATURE_EDITING 0 |
| 313 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 314 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 315 | #endif |
| 316 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 317 | /* Do we support ANY keywords? */ |
| 318 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 319 | # define HAS_KEYWORDS 1 |
| 320 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 321 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 322 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 323 | # define HAS_KEYWORDS 0 |
| 324 | # define IF_HAS_KEYWORDS(...) |
| 325 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 326 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 327 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 328 | /* If you comment out one of these below, it will be #defined later |
| 329 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 330 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 331 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 332 | #define debug_printf_parse(...) do {} while (0) |
| 333 | #define debug_print_tree(a, b) do {} while (0) |
| 334 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 335 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 336 | #define debug_printf_jobs(...) do {} while (0) |
| 337 | #define debug_printf_expand(...) do {} while (0) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 338 | #define debug_printf_varexp(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 339 | #define debug_printf_glob(...) do {} while (0) |
| 340 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 341 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 342 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 343 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 344 | #define ERR_PTR ((void*)(long)1) |
| 345 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 346 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 347 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 348 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 349 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 350 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 351 | #if ENABLE_HUSH_BASH_COMPAT |
| 352 | /* Support / and // replace ops */ |
| 353 | /* Note that // is stored as \ in "encoded" string representation */ |
| 354 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 355 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 356 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 357 | #else |
| 358 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 359 | # define VAR_SUBST_OPS "%#:-=+?" |
| 360 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 361 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 362 | |
| 363 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 364 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 365 | struct variable; |
| 366 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 367 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 368 | |
| 369 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 370 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 371 | */ |
| 372 | #if !BB_MMU |
| 373 | typedef struct nommu_save_t { |
| 374 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 375 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 376 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 377 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 378 | } nommu_save_t; |
| 379 | #endif |
| 380 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 381 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 382 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 383 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 384 | RES_IF , |
| 385 | RES_THEN , |
| 386 | RES_ELIF , |
| 387 | RES_ELSE , |
| 388 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 389 | #endif |
| 390 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 391 | RES_FOR , |
| 392 | RES_WHILE , |
| 393 | RES_UNTIL , |
| 394 | RES_DO , |
| 395 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 396 | #endif |
| 397 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 398 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 399 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 400 | #if ENABLE_HUSH_CASE |
| 401 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 402 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 403 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 404 | RES_MATCH , /* "word)" */ |
| 405 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 406 | RES_ESAC , |
| 407 | #endif |
| 408 | RES_XXXX , |
| 409 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 410 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 411 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 412 | typedef struct o_string { |
| 413 | char *data; |
| 414 | int length; /* position where data is appended */ |
| 415 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 416 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 417 | /* At least some part of the string was inside '' or "", |
| 418 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 419 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 420 | smallint has_empty_slot; |
| 421 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 422 | } o_string; |
| 423 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 424 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 425 | EXP_FLAG_GLOB = 0x2, |
| 426 | /* Protect newly added chars against globbing |
| 427 | * by prepending \ to *, ?, [, \ */ |
| 428 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 429 | }; |
| 430 | enum { |
| 431 | MAYBE_ASSIGNMENT = 0, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 432 | DEFINITELY_ASSIGNMENT = 1, |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 433 | NOT_ASSIGNMENT = 2, |
| 434 | /* Not an assigment, but next word may be: "if v=xyz cmd;" */ |
| 435 | WORD_IS_KEYWORD = 3, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 436 | }; |
| 437 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 438 | #define NULL_O_STRING { NULL } |
| 439 | |
| 440 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
| 441 | * available? Where is it documented? */ |
| 442 | typedef struct in_str { |
| 443 | const char *p; |
| 444 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 445 | char eof_flag; /* meaningless if ->p == NULL */ |
| 446 | char peek_buf[2]; |
| 447 | #if ENABLE_HUSH_INTERACTIVE |
| 448 | smallint promptme; |
| 449 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 450 | #endif |
| 451 | FILE *file; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 452 | int (*get) (struct in_str *) FAST_FUNC; |
| 453 | int (*peek) (struct in_str *) FAST_FUNC; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 454 | } in_str; |
| 455 | #define i_getch(input) ((input)->get(input)) |
| 456 | #define i_peek(input) ((input)->peek(input)) |
| 457 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 458 | /* The descrip member of this structure is only used to make |
| 459 | * debugging output pretty */ |
| 460 | static const struct { |
| 461 | int mode; |
| 462 | signed char default_fd; |
| 463 | char descrip[3]; |
| 464 | } redir_table[] = { |
| 465 | { O_RDONLY, 0, "<" }, |
| 466 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 467 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 468 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 469 | { O_RDONLY, 0, "<<" }, |
| 470 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 471 | /* { O_RDONLY, 77, "<<" }, */ |
| 472 | }; |
| 473 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 474 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 475 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 476 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 477 | int rd_fd; /* fd to redirect */ |
| 478 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 479 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 480 | smallint rd_type; /* (enum redir_type) */ |
| 481 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 482 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 483 | * bit 0: do we need to trim leading tabs? |
| 484 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 485 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 486 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 487 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 488 | REDIRECT_INPUT = 0, |
| 489 | REDIRECT_OVERWRITE = 1, |
| 490 | REDIRECT_APPEND = 2, |
| 491 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 492 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 493 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 494 | |
| 495 | REDIRFD_CLOSE = -3, |
| 496 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 497 | REDIRFD_TO_FILE = -1, |
| 498 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 499 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 500 | HEREDOC_SKIPTABS = 1, |
| 501 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 502 | } redir_type; |
| 503 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 504 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 505 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 506 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 507 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 508 | smallint is_stopped; /* is the command currently running? */ |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 509 | smallint cmd_type; /* CMD_xxx */ |
| 510 | #define CMD_NORMAL 0 |
| 511 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 512 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | d383b49 | 2010-09-06 10:22:13 +0200 | [diff] [blame] | 513 | /* used for "[[ EXPR ]]" */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 514 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 515 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 516 | #if ENABLE_HUSH_FUNCTIONS |
| 517 | # define CMD_FUNCDEF 3 |
| 518 | #endif |
| 519 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 520 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 521 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 522 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 523 | #if !BB_MMU |
| 524 | char *group_as_string; |
| 525 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 526 | #if ENABLE_HUSH_FUNCTIONS |
| 527 | struct function *child_func; |
| 528 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 529 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 530 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 531 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 532 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 533 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 534 | * we put those fields back into cmd->xxx |
| 535 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 536 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 537 | * Without this trick, loop would execute a;b;b;b;... |
| 538 | * instead of correct sequence a;b;a;b;... |
| 539 | * When command is freed, it severs the link |
| 540 | * (sets ->child_func->parent_cmd to NULL). |
| 541 | */ |
| 542 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 543 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 544 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 545 | * and on execution these are substituted with their values. |
| 546 | * Substitution can make _several_ words out of one argv[n]! |
| 547 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 548 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 549 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 550 | struct redir_struct *redirects; /* I/O redirections */ |
| 551 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 552 | /* Is there anything in this command at all? */ |
| 553 | #define IS_NULL_CMD(cmd) \ |
| 554 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 555 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 556 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 557 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 558 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 559 | int alive_cmds; /* number of commands running (not exited) */ |
| 560 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 561 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 562 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 563 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 564 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 565 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 566 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 567 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 568 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 569 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 570 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 571 | typedef enum pipe_style { |
| 572 | PIPE_SEQ = 1, |
| 573 | PIPE_AND = 2, |
| 574 | PIPE_OR = 3, |
| 575 | PIPE_BG = 4, |
| 576 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 577 | /* Is there anything in this pipe at all? */ |
| 578 | #define IS_NULL_PIPE(pi) \ |
| 579 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 580 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 581 | /* This holds pointers to the various results of parsing */ |
| 582 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 583 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 584 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 585 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 586 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 587 | /* last command in pipe (being constructed right now) */ |
| 588 | struct command *command; |
| 589 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 590 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 591 | #if !BB_MMU |
| 592 | o_string as_string; |
| 593 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 594 | #if HAS_KEYWORDS |
| 595 | smallint ctx_res_w; |
| 596 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 597 | #if ENABLE_HUSH_CASE |
| 598 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 599 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 600 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 601 | int old_flag; |
| 602 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 603 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 604 | * when we see "if" or "then", we malloc and copy current context, |
| 605 | * and make ->stack point to it. then we parse pipeN. |
| 606 | * when closing "then" / fi" / whatever is found, |
| 607 | * we move list_head into ->stack->command->group, |
| 608 | * copy ->stack into current context, and delete ->stack. |
| 609 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 610 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 611 | struct parse_context *stack; |
| 612 | #endif |
| 613 | }; |
| 614 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 615 | /* On program start, environ points to initial environment. |
| 616 | * putenv adds new pointers into it, unsetenv removes them. |
| 617 | * Neither of these (de)allocates the strings. |
| 618 | * setenv allocates new strings in malloc space and does putenv, |
| 619 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 620 | #define setenv(...) setenv_is_leaky_dont_use() |
| 621 | struct variable { |
| 622 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 623 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 624 | #if ENABLE_HUSH_LOCAL |
| 625 | unsigned func_nest_level; |
| 626 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 627 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 628 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 629 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 630 | }; |
| 631 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 632 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 633 | BC_BREAK = 1, |
| 634 | BC_CONTINUE = 2, |
| 635 | }; |
| 636 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 637 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 638 | struct function { |
| 639 | struct function *next; |
| 640 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 641 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 642 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 643 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 644 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 645 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 646 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 647 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 648 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 649 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 650 | /* set -/+o OPT support. (TODO: make it optional) |
| 651 | * bash supports the following opts: |
| 652 | * allexport off |
| 653 | * braceexpand on |
| 654 | * emacs on |
| 655 | * errexit off |
| 656 | * errtrace off |
| 657 | * functrace off |
| 658 | * hashall on |
| 659 | * histexpand off |
| 660 | * history on |
| 661 | * ignoreeof off |
| 662 | * interactive-comments on |
| 663 | * keyword off |
| 664 | * monitor on |
| 665 | * noclobber off |
| 666 | * noexec off |
| 667 | * noglob off |
| 668 | * nolog off |
| 669 | * notify off |
| 670 | * nounset off |
| 671 | * onecmd off |
| 672 | * physical off |
| 673 | * pipefail off |
| 674 | * posix off |
| 675 | * privileged off |
| 676 | * verbose off |
| 677 | * vi off |
| 678 | * xtrace off |
| 679 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 680 | static const char o_opt_strings[] ALIGN1 = |
| 681 | "pipefail\0" |
| 682 | "noexec\0" |
| 683 | #if ENABLE_HUSH_MODE_X |
| 684 | "xtrace\0" |
| 685 | #endif |
| 686 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 687 | enum { |
| 688 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 689 | OPT_O_NOEXEC, |
| 690 | #if ENABLE_HUSH_MODE_X |
| 691 | OPT_O_XTRACE, |
| 692 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 693 | NUM_OPT_O |
| 694 | }; |
| 695 | |
| 696 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 697 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 698 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 699 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 700 | /* interactive_fd != 0 means we are an interactive shell. |
| 701 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 702 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 703 | * job control still works, but terminal signals |
| 704 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 705 | * process groups can only be created with "cmd &". |
| 706 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 707 | * to give tty to the foreground process group, |
| 708 | * and will take it back when the group is stopped (^Z) |
| 709 | * or killed (^C). |
| 710 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 711 | #if ENABLE_HUSH_INTERACTIVE |
| 712 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 713 | * _AND_ if we decided to act interactively */ |
| 714 | int interactive_fd; |
| 715 | const char *PS1; |
| 716 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 717 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 718 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 719 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 720 | #endif |
| 721 | #if ENABLE_FEATURE_EDITING |
| 722 | line_input_t *line_input_state; |
| 723 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 724 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 725 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 726 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 727 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 728 | random_t random_gen; |
| 729 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 730 | #if ENABLE_HUSH_JOB |
| 731 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 732 | int last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 733 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 734 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 735 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 736 | #else |
| 737 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 738 | #endif |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 739 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 740 | #if ENABLE_HUSH_MODE_X |
| 741 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 742 | #else |
| 743 | # define G_x_mode 0 |
| 744 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 745 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 746 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 747 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 748 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 749 | #if ENABLE_HUSH_FUNCTIONS |
| 750 | /* 0: outside of a function (or sourced file) |
| 751 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 752 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 753 | */ |
| 754 | smallint flag_return_in_progress; |
| 755 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 756 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 757 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 758 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 759 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 760 | smalluint global_args_malloced; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 761 | smalluint inherited_set_is_saved; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 762 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 763 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 764 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 765 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 766 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 767 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 768 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 769 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 770 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 771 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 772 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 773 | const char *cwd; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 774 | struct variable *top_var; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 775 | char **expanded_assignments; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 776 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 777 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 778 | # if ENABLE_HUSH_LOCAL |
| 779 | struct variable **shadowed_vars_pp; |
| 780 | unsigned func_nest_level; |
| 781 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 782 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 783 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 784 | #if ENABLE_HUSH_FAST |
| 785 | unsigned count_SIGCHLD; |
| 786 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 787 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 788 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 789 | /* which signals have non-DFL handler (even with no traps set)? */ |
| 790 | unsigned non_DFL_mask; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 791 | char **traps; /* char *traps[NSIG] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 792 | sigset_t blocked_set; |
| 793 | sigset_t inherited_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 794 | #if HUSH_DEBUG |
| 795 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 796 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 797 | #endif |
Denys Vlasenko | aaa22d2 | 2009-10-19 16:34:39 +0200 | [diff] [blame] | 798 | char user_input_buf[ENABLE_FEATURE_EDITING ? CONFIG_FEATURE_EDITING_MAX_LEN : 2]; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 799 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 800 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 801 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 802 | * (too many defines). Also, I actually prefer to see when a variable |
| 803 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 804 | #define INIT_G() do { \ |
| 805 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 806 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 807 | |
| 808 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 809 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 810 | static int builtin_cd(char **argv) FAST_FUNC; |
| 811 | static int builtin_echo(char **argv) FAST_FUNC; |
| 812 | static int builtin_eval(char **argv) FAST_FUNC; |
| 813 | static int builtin_exec(char **argv) FAST_FUNC; |
| 814 | static int builtin_exit(char **argv) FAST_FUNC; |
| 815 | static int builtin_export(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 816 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 817 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 818 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 819 | #endif |
| 820 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 821 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 822 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 823 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 824 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 825 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 826 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 827 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 828 | #endif |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 829 | #if ENABLE_PRINTF |
| 830 | static int builtin_printf(char **argv) FAST_FUNC; |
| 831 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 832 | static int builtin_pwd(char **argv) FAST_FUNC; |
| 833 | static int builtin_read(char **argv) FAST_FUNC; |
| 834 | static int builtin_set(char **argv) FAST_FUNC; |
| 835 | static int builtin_shift(char **argv) FAST_FUNC; |
| 836 | static int builtin_source(char **argv) FAST_FUNC; |
| 837 | static int builtin_test(char **argv) FAST_FUNC; |
| 838 | static int builtin_trap(char **argv) FAST_FUNC; |
| 839 | static int builtin_type(char **argv) FAST_FUNC; |
| 840 | static int builtin_true(char **argv) FAST_FUNC; |
| 841 | static int builtin_umask(char **argv) FAST_FUNC; |
| 842 | static int builtin_unset(char **argv) FAST_FUNC; |
| 843 | static int builtin_wait(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 844 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 845 | static int builtin_break(char **argv) FAST_FUNC; |
| 846 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 847 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 848 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 849 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 850 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 851 | |
| 852 | /* Table of built-in functions. They can be forked or not, depending on |
| 853 | * context: within pipes, they fork. As simple commands, they do not. |
| 854 | * When used in non-forking context, they can change global variables |
| 855 | * in the parent shell process. If forked, of course they cannot. |
| 856 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 857 | * still be set at the end. */ |
| 858 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 859 | const char *b_cmd; |
| 860 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 861 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 862 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 863 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 864 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 865 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 866 | #endif |
| 867 | }; |
| 868 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 869 | static const struct built_in_command bltins1[] = { |
| 870 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 871 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 872 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 873 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 874 | #endif |
| 875 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 876 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 877 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 878 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 879 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 880 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 881 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 882 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 883 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 884 | BLTIN("exit" , builtin_exit , "Exit"), |
| 885 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 886 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 887 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 888 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 889 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 890 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 891 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 892 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 893 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 894 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 895 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 896 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 897 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 898 | #if HUSH_DEBUG |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 899 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 900 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 901 | BLTIN("read" , builtin_read , "Input into variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 902 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 903 | BLTIN("return" , builtin_return , "Return from a function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 904 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 905 | BLTIN("set" , builtin_set , "Set/unset positional parameters"), |
| 906 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 907 | #if ENABLE_HUSH_BASH_COMPAT |
| 908 | BLTIN("source" , builtin_source , "Run commands in a file"), |
| 909 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 910 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 911 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 912 | BLTIN("ulimit" , shell_builtin_ulimit , "Control resource limits"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 913 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 914 | BLTIN("unset" , builtin_unset , "Unset variables"), |
| 915 | BLTIN("wait" , builtin_wait , "Wait for process"), |
| 916 | }; |
| 917 | /* For now, echo and test are unconditionally enabled. |
| 918 | * Maybe make it configurable? */ |
| 919 | static const struct built_in_command bltins2[] = { |
| 920 | BLTIN("[" , builtin_test , NULL), |
| 921 | BLTIN("echo" , builtin_echo , NULL), |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 922 | #if ENABLE_PRINTF |
| 923 | BLTIN("printf" , builtin_printf , NULL), |
| 924 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 925 | BLTIN("pwd" , builtin_pwd , NULL), |
| 926 | BLTIN("test" , builtin_test , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 927 | }; |
| 928 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 929 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 930 | /* Debug printouts. |
| 931 | */ |
| 932 | #if HUSH_DEBUG |
| 933 | /* prevent disasters with G.debug_indent < 0 */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 934 | # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "") |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 935 | # define debug_enter() (G.debug_indent++) |
| 936 | # define debug_leave() (G.debug_indent--) |
| 937 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 938 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 939 | # define debug_enter() ((void)0) |
| 940 | # define debug_leave() ((void)0) |
| 941 | #endif |
| 942 | |
| 943 | #ifndef debug_printf |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 944 | # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 945 | #endif |
| 946 | |
| 947 | #ifndef debug_printf_parse |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 948 | # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 949 | #endif |
| 950 | |
| 951 | #ifndef debug_printf_exec |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 952 | #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 953 | #endif |
| 954 | |
| 955 | #ifndef debug_printf_env |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 956 | # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 957 | #endif |
| 958 | |
| 959 | #ifndef debug_printf_jobs |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 960 | # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 961 | # define DEBUG_JOBS 1 |
| 962 | #else |
| 963 | # define DEBUG_JOBS 0 |
| 964 | #endif |
| 965 | |
| 966 | #ifndef debug_printf_expand |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 967 | # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 968 | # define DEBUG_EXPAND 1 |
| 969 | #else |
| 970 | # define DEBUG_EXPAND 0 |
| 971 | #endif |
| 972 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 973 | #ifndef debug_printf_varexp |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 974 | # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 975 | #endif |
| 976 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 977 | #ifndef debug_printf_glob |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 978 | # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 979 | # define DEBUG_GLOB 1 |
| 980 | #else |
| 981 | # define DEBUG_GLOB 0 |
| 982 | #endif |
| 983 | |
| 984 | #ifndef debug_printf_list |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 985 | # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 986 | #endif |
| 987 | |
| 988 | #ifndef debug_printf_subst |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 989 | # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 990 | #endif |
| 991 | |
| 992 | #ifndef debug_printf_clean |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 993 | # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 994 | # define DEBUG_CLEAN 1 |
| 995 | #else |
| 996 | # define DEBUG_CLEAN 0 |
| 997 | #endif |
| 998 | |
| 999 | #if DEBUG_EXPAND |
| 1000 | static void debug_print_strings(const char *prefix, char **vv) |
| 1001 | { |
| 1002 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1003 | fdprintf(2, "%s:\n", prefix); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1004 | while (*vv) |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1005 | fdprintf(2, " '%s'\n", *vv++); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1006 | } |
| 1007 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1008 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1009 | #endif |
| 1010 | |
| 1011 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1012 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1013 | */ |
| 1014 | #if LEAK_HUNTING |
| 1015 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1016 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1017 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1018 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1019 | return ptr; |
| 1020 | } |
| 1021 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1022 | { |
| 1023 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1024 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1025 | return ptr; |
| 1026 | } |
| 1027 | static char *xxstrdup(int lineno, const char *str) |
| 1028 | { |
| 1029 | char *ptr = xstrdup(str); |
| 1030 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1031 | return ptr; |
| 1032 | } |
| 1033 | static void xxfree(void *ptr) |
| 1034 | { |
| 1035 | fdprintf(2, "free %p\n", ptr); |
| 1036 | free(ptr); |
| 1037 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1038 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1039 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1040 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1041 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1042 | #endif |
| 1043 | |
| 1044 | |
| 1045 | /* Syntax and runtime errors. They always abort scripts. |
| 1046 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1047 | * and return to the prompt. |
| 1048 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1049 | */ |
| 1050 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1051 | # define die_if_script(lineno, ...) die_if_script(__VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1052 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1053 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1054 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1055 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1056 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1057 | #endif |
| 1058 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1059 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1060 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1061 | va_list p; |
| 1062 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1063 | #if HUSH_DEBUG >= 2 |
| 1064 | bb_error_msg("hush.c:%u", lineno); |
| 1065 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1066 | va_start(p, fmt); |
| 1067 | bb_verror_msg(fmt, p, NULL); |
| 1068 | va_end(p); |
| 1069 | if (!G_interactive_fd) |
| 1070 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1071 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1072 | |
| 1073 | static void syntax_error(unsigned lineno, const char *msg) |
| 1074 | { |
| 1075 | if (msg) |
| 1076 | die_if_script(lineno, "syntax error: %s", msg); |
| 1077 | else |
| 1078 | die_if_script(lineno, "syntax error", NULL); |
| 1079 | } |
| 1080 | |
| 1081 | static void syntax_error_at(unsigned lineno, const char *msg) |
| 1082 | { |
| 1083 | die_if_script(lineno, "syntax error at '%s'", msg); |
| 1084 | } |
| 1085 | |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1086 | static void syntax_error_unterm_str(unsigned lineno, const char *s) |
| 1087 | { |
| 1088 | die_if_script(lineno, "syntax error: unterminated %s", s); |
| 1089 | } |
| 1090 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1091 | /* It so happens that all such cases are totally fatal |
| 1092 | * even if shell is interactive: EOF while looking for closing |
| 1093 | * delimiter. There is nowhere to read stuff from after that, |
| 1094 | * it's EOF! The only choice is to terminate. |
| 1095 | */ |
| 1096 | static void syntax_error_unterm_ch(unsigned lineno, char ch) NORETURN; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1097 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1098 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1099 | char msg[2] = { ch, '\0' }; |
| 1100 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1101 | xfunc_die(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 1104 | static void syntax_error_unexpected_ch(unsigned lineno, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1105 | { |
| 1106 | char msg[2]; |
| 1107 | msg[0] = ch; |
| 1108 | msg[1] = '\0'; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 1109 | die_if_script(lineno, "syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1112 | #if HUSH_DEBUG < 2 |
| 1113 | # undef die_if_script |
| 1114 | # undef syntax_error |
| 1115 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1116 | # undef syntax_error_unterm_ch |
| 1117 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1118 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1119 | #else |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1120 | # define die_if_script(...) die_if_script(__LINE__, __VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1121 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1122 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1123 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1124 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1125 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1126 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1127 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1128 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1129 | #if ENABLE_HUSH_INTERACTIVE |
| 1130 | static void cmdedit_update_prompt(void); |
| 1131 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1132 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1133 | #endif |
| 1134 | |
| 1135 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1136 | /* Utility functions |
| 1137 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1138 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1139 | static char *unbackslash(char *src) |
| 1140 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1141 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1142 | while (1) { |
| 1143 | if (*src == '\\') |
| 1144 | src++; |
| 1145 | if ((*dst++ = *src++) == '\0') |
| 1146 | break; |
| 1147 | } |
| 1148 | return dst; |
| 1149 | } |
| 1150 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1151 | 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] | 1152 | { |
| 1153 | int i; |
| 1154 | unsigned count1; |
| 1155 | unsigned count2; |
| 1156 | char **v; |
| 1157 | |
| 1158 | v = strings; |
| 1159 | count1 = 0; |
| 1160 | if (v) { |
| 1161 | while (*v) { |
| 1162 | count1++; |
| 1163 | v++; |
| 1164 | } |
| 1165 | } |
| 1166 | count2 = 0; |
| 1167 | v = add; |
| 1168 | while (*v) { |
| 1169 | count2++; |
| 1170 | v++; |
| 1171 | } |
| 1172 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1173 | v[count1 + count2] = NULL; |
| 1174 | i = count2; |
| 1175 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1176 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1177 | return v; |
| 1178 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1179 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1180 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1181 | { |
| 1182 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1183 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1184 | return ptr; |
| 1185 | } |
| 1186 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1187 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1188 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1189 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1190 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1191 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1192 | { |
| 1193 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1194 | v[0] = add; |
| 1195 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1196 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1197 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1198 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1199 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1200 | { |
| 1201 | char **ptr = add_string_to_strings(strings, add); |
| 1202 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1203 | return ptr; |
| 1204 | } |
| 1205 | #define add_string_to_strings(strings, add) \ |
| 1206 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1207 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1208 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1209 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1210 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1211 | char **v; |
| 1212 | |
| 1213 | if (!strings) |
| 1214 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1215 | v = strings; |
| 1216 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1217 | free(*v); |
| 1218 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1219 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1220 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1223 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1224 | /* Helpers for setting new $n and restoring them back |
| 1225 | */ |
| 1226 | typedef struct save_arg_t { |
| 1227 | char *sv_argv0; |
| 1228 | char **sv_g_argv; |
| 1229 | int sv_g_argc; |
| 1230 | smallint sv_g_malloced; |
| 1231 | } save_arg_t; |
| 1232 | |
| 1233 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1234 | { |
| 1235 | int n; |
| 1236 | |
| 1237 | sv->sv_argv0 = argv[0]; |
| 1238 | sv->sv_g_argv = G.global_argv; |
| 1239 | sv->sv_g_argc = G.global_argc; |
| 1240 | sv->sv_g_malloced = G.global_args_malloced; |
| 1241 | |
| 1242 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1243 | G.global_argv = argv; |
| 1244 | G.global_args_malloced = 0; |
| 1245 | |
| 1246 | n = 1; |
| 1247 | while (*++argv) |
| 1248 | n++; |
| 1249 | G.global_argc = n; |
| 1250 | } |
| 1251 | |
| 1252 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1253 | { |
| 1254 | char **pp; |
| 1255 | |
| 1256 | if (G.global_args_malloced) { |
| 1257 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 1258 | pp = G.global_argv; |
| 1259 | while (*++pp) /* note: does not free $0 */ |
| 1260 | free(*pp); |
| 1261 | free(G.global_argv); |
| 1262 | } |
| 1263 | argv[0] = sv->sv_argv0; |
| 1264 | G.global_argv = sv->sv_g_argv; |
| 1265 | G.global_argc = sv->sv_g_argc; |
| 1266 | G.global_args_malloced = sv->sv_g_malloced; |
| 1267 | } |
| 1268 | |
| 1269 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1270 | /* Basic theory of signal handling in shell |
| 1271 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1272 | * This does not describe what hush does, rather, it is current understanding |
| 1273 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1274 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1275 | * |
| 1276 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1277 | * is finished or backgrounded. It is the same in interactive and |
| 1278 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1279 | * 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] | 1280 | * ^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] | 1281 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1282 | * pipe. |
| 1283 | * |
| 1284 | * Wait builtin in interruptible by signals for which user trap is set |
| 1285 | * or by SIGINT in interactive shell. |
| 1286 | * |
| 1287 | * Trap handlers will execute even within trap handlers. (right?) |
| 1288 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1289 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1290 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1291 | * |
| 1292 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1293 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1294 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1295 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1296 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1297 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1298 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1299 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1300 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1301 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1302 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1303 | * |
| 1304 | * SIGQUIT: ignore |
| 1305 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1306 | * SIGHUP (interactive): |
| 1307 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1308 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1309 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1310 | * that all pipe members are stopped. Try this in bash: |
| 1311 | * while :; do :; done - ^Z does not background it |
| 1312 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1313 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1314 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1315 | * to interactive shell while shell is waiting for a pipe, |
| 1316 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1317 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1318 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1319 | * Example 2: this does not wait and does not execute ls: |
| 1320 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1321 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1322 | * "sleep 5; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1323 | * |
| 1324 | * (What happens to signals which are IGN on shell start?) |
| 1325 | * (What happens with signal mask on shell start?) |
| 1326 | * |
| 1327 | * Implementation in hush |
| 1328 | * ====================== |
| 1329 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1330 | * We block all signals which we don't want to take action immediately, |
| 1331 | * i.e. we block all signals which need to have special handling as described |
| 1332 | * above, and all signals which have traps set. |
| 1333 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1334 | * and act on them. |
| 1335 | * |
| 1336 | * unsigned non_DFL_mask: a mask of such "special" signals |
| 1337 | * sigset_t blocked_set: current blocked signal set |
| 1338 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1339 | * "trap - SIGxxx": |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1340 | * clear bit in blocked_set unless it is also in non_DFL_mask |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1341 | * "trap 'cmd' SIGxxx": |
| 1342 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1343 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1344 | * unblock signals with special interactive handling |
| 1345 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1346 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1347 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1348 | * 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] | 1349 | * 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] | 1350 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1351 | * 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] | 1352 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1353 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1354 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1355 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1356 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1357 | * 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] | 1358 | * 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] | 1359 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1360 | enum { |
| 1361 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1362 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1363 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1364 | | (1 << SIGHUP) |
| 1365 | , |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1366 | SPECIAL_JOB_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1367 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1368 | | (1 << SIGTTIN) |
| 1369 | | (1 << SIGTTOU) |
| 1370 | | (1 << SIGTSTP) |
| 1371 | #endif |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1372 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1373 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1374 | #if ENABLE_HUSH_FAST |
| 1375 | static void SIGCHLD_handler(int sig UNUSED_PARAM) |
| 1376 | { |
| 1377 | G.count_SIGCHLD++; |
| 1378 | //bb_error_msg("[%d] SIGCHLD_handler: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1379 | } |
| 1380 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1381 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1382 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1383 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1384 | /* After [v]fork, in child: do not restore tty pgrp on xfunc death */ |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1385 | # define disable_restore_tty_pgrp_on_exit() (die_sleep = 0) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1386 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1387 | # define enable_restore_tty_pgrp_on_exit() (die_sleep = -1) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1388 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1389 | /* Restores tty foreground process group, and exits. |
| 1390 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1391 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1392 | * or called directly with -EXITCODE. |
| 1393 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1394 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1395 | static void sigexit(int sig) |
| 1396 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1397 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 1398 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1399 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1400 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1401 | * tty pgrp then, only top-level shell process does that */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1402 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 1403 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1404 | |
| 1405 | /* Not a signal, just exit */ |
| 1406 | if (sig <= 0) |
| 1407 | _exit(- sig); |
| 1408 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1409 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1410 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1411 | #else |
| 1412 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1413 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1414 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1415 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1416 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1417 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1418 | /* Restores tty foreground process group, and exits. */ |
| 1419 | static void hush_exit(int exitcode) NORETURN; |
| 1420 | static void hush_exit(int exitcode) |
| 1421 | { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1422 | if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) { |
| 1423 | /* Prevent recursion: |
| 1424 | * trap "echo Hi; exit" EXIT; exit |
| 1425 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1426 | char *argv[3]; |
| 1427 | /* argv[0] is unused */ |
| 1428 | argv[1] = G.traps[0]; |
| 1429 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1430 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1431 | /* Note: G.traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 1432 | * "trap" will still show it, if executed |
| 1433 | * in the handler */ |
| 1434 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1435 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1436 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1437 | #if ENABLE_FEATURE_CLEAN_UP |
| 1438 | { |
| 1439 | struct variable *cur_var; |
| 1440 | if (G.cwd != bb_msg_unknown) |
| 1441 | free((char*)G.cwd); |
| 1442 | cur_var = G.top_var; |
| 1443 | while (cur_var) { |
| 1444 | struct variable *tmp = cur_var; |
| 1445 | if (!cur_var->max_len) |
| 1446 | free(cur_var->varstr); |
| 1447 | cur_var = cur_var->next; |
| 1448 | free(tmp); |
| 1449 | } |
| 1450 | } |
| 1451 | #endif |
| 1452 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1453 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1454 | fflush_all(); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1455 | sigexit(- (exitcode & 0xff)); |
| 1456 | #else |
| 1457 | exit(exitcode); |
| 1458 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 1461 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1462 | static int check_and_run_traps(int sig) |
| 1463 | { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 1464 | /* I want it in rodata, not in bss. |
| 1465 | * gcc 4.2.1 puts it in rodata only if it has { 0, 0 } |
| 1466 | * initializer. But other compilers may still use bss. |
| 1467 | * TODO: find more portable solution. |
| 1468 | */ |
| 1469 | static const struct timespec zero_timespec = { 0, 0 }; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1470 | smalluint save_rcode; |
| 1471 | int last_sig = 0; |
| 1472 | |
| 1473 | if (sig) |
| 1474 | goto jump_in; |
| 1475 | while (1) { |
| 1476 | sig = sigtimedwait(&G.blocked_set, NULL, &zero_timespec); |
| 1477 | if (sig <= 0) |
| 1478 | break; |
| 1479 | jump_in: |
| 1480 | last_sig = sig; |
| 1481 | if (G.traps && G.traps[sig]) { |
| 1482 | if (G.traps[sig][0]) { |
| 1483 | /* We have user-defined handler */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1484 | char *argv[3]; |
| 1485 | /* argv[0] is unused */ |
| 1486 | argv[1] = G.traps[sig]; |
| 1487 | argv[2] = NULL; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1488 | save_rcode = G.last_exitcode; |
| 1489 | builtin_eval(argv); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1490 | G.last_exitcode = save_rcode; |
| 1491 | } /* else: "" trap, ignoring signal */ |
| 1492 | continue; |
| 1493 | } |
| 1494 | /* not a trap: special action */ |
| 1495 | switch (sig) { |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1496 | #if ENABLE_HUSH_FAST |
| 1497 | case SIGCHLD: |
| 1498 | G.count_SIGCHLD++; |
| 1499 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1500 | break; |
| 1501 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1502 | case SIGINT: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1503 | /* Builtin was ^C'ed, make it look prettier: */ |
| 1504 | bb_putchar('\n'); |
| 1505 | G.flag_SIGINT = 1; |
| 1506 | break; |
| 1507 | #if ENABLE_HUSH_JOB |
| 1508 | case SIGHUP: { |
| 1509 | struct pipe *job; |
| 1510 | /* bash is observed to signal whole process groups, |
| 1511 | * not individual processes */ |
| 1512 | for (job = G.job_list; job; job = job->next) { |
| 1513 | if (job->pgrp <= 0) |
| 1514 | continue; |
| 1515 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 1516 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 1517 | kill(- job->pgrp, SIGCONT); |
| 1518 | } |
| 1519 | sigexit(SIGHUP); |
| 1520 | } |
| 1521 | #endif |
| 1522 | default: /* ignored: */ |
| 1523 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
| 1524 | break; |
| 1525 | } |
| 1526 | } |
| 1527 | return last_sig; |
| 1528 | } |
| 1529 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1530 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1531 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1532 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1533 | if (force || G.cwd == NULL) { |
| 1534 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1535 | * we must not try to free(bb_msg_unknown) */ |
| 1536 | if (G.cwd == bb_msg_unknown) |
| 1537 | G.cwd = NULL; |
| 1538 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1539 | if (!G.cwd) |
| 1540 | G.cwd = bb_msg_unknown; |
| 1541 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1542 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1545 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1546 | /* |
| 1547 | * Shell and environment variable support |
| 1548 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1549 | 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] | 1550 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1551 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1552 | struct variable *cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1553 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1554 | pp = &G.top_var; |
| 1555 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1556 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1557 | return pp; |
| 1558 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1559 | } |
| 1560 | return NULL; |
| 1561 | } |
| 1562 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1563 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1564 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1565 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1566 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1567 | |
| 1568 | if (G.expanded_assignments) { |
| 1569 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1570 | while (*cpp) { |
| 1571 | char *cp = *cpp; |
| 1572 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 1573 | return cp + len + 1; |
| 1574 | cpp++; |
| 1575 | } |
| 1576 | } |
| 1577 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1578 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1579 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1580 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1581 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 1582 | if (strcmp(name, "PPID") == 0) |
| 1583 | return utoa(G.root_ppid); |
| 1584 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1585 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1586 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1587 | return utoa(next_random(&G.random_gen)); |
| 1588 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1589 | return NULL; |
| 1590 | } |
| 1591 | |
| 1592 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1593 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1594 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1595 | * 0: do not change export flag |
| 1596 | * (if creating new variable, flag will be 0) |
| 1597 | * 1: set export flag and putenv the variable |
| 1598 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1599 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1600 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1601 | #if !BB_MMU && ENABLE_HUSH_LOCAL |
| 1602 | /* all params are used */ |
| 1603 | #elif BB_MMU && ENABLE_HUSH_LOCAL |
| 1604 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1605 | set_local_var(str, flg_export, local_lvl) |
| 1606 | #elif BB_MMU && !ENABLE_HUSH_LOCAL |
| 1607 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1608 | set_local_var(str, flg_export) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1609 | #elif !BB_MMU && !ENABLE_HUSH_LOCAL |
| 1610 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1611 | set_local_var(str, flg_export, flg_read_only) |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1612 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1613 | 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] | 1614 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1615 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1616 | struct variable *cur; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1617 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1618 | int name_len; |
| 1619 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1620 | eq_sign = strchr(str, '='); |
| 1621 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1622 | free(str); |
| 1623 | return -1; |
| 1624 | } |
| 1625 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1626 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1627 | var_pp = &G.top_var; |
| 1628 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1629 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1630 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1631 | continue; |
| 1632 | } |
| 1633 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1634 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1635 | #if !BB_MMU |
| 1636 | if (!flg_read_only) |
| 1637 | #endif |
| 1638 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1639 | free(str); |
| 1640 | return -1; |
| 1641 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1642 | if (flg_export == -1) { // "&& cur->flg_export" ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1643 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1644 | *eq_sign = '\0'; |
| 1645 | unsetenv(str); |
| 1646 | *eq_sign = '='; |
| 1647 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1648 | #if ENABLE_HUSH_LOCAL |
| 1649 | if (cur->func_nest_level < local_lvl) { |
| 1650 | /* New variable is declared as local, |
| 1651 | * and existing one is global, or local |
| 1652 | * from enclosing function. |
| 1653 | * Remove and save old one: */ |
| 1654 | *var_pp = cur->next; |
| 1655 | cur->next = *G.shadowed_vars_pp; |
| 1656 | *G.shadowed_vars_pp = cur; |
| 1657 | /* bash 3.2.33(1) and exported vars: |
| 1658 | * # export z=z |
| 1659 | * # f() { local z=a; env | grep ^z; } |
| 1660 | * # f |
| 1661 | * z=a |
| 1662 | * # env | grep ^z |
| 1663 | * z=z |
| 1664 | */ |
| 1665 | if (cur->flg_export) |
| 1666 | flg_export = 1; |
| 1667 | break; |
| 1668 | } |
| 1669 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1670 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1671 | free_and_exp: |
| 1672 | free(str); |
| 1673 | goto exp; |
| 1674 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1675 | if (cur->max_len != 0) { |
| 1676 | if (cur->max_len >= strlen(str)) { |
| 1677 | /* This one is from startup env, reuse space */ |
| 1678 | strcpy(cur->varstr, str); |
| 1679 | goto free_and_exp; |
| 1680 | } |
| 1681 | } else { |
| 1682 | /* max_len == 0 signifies "malloced" var, which we can |
| 1683 | * (and has to) free */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1684 | free(cur->varstr); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1685 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1686 | cur->max_len = 0; |
| 1687 | goto set_str_and_exp; |
| 1688 | } |
| 1689 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1690 | /* Not found - create new variable struct */ |
| 1691 | cur = xzalloc(sizeof(*cur)); |
| 1692 | #if ENABLE_HUSH_LOCAL |
| 1693 | cur->func_nest_level = local_lvl; |
| 1694 | #endif |
| 1695 | cur->next = *var_pp; |
| 1696 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1697 | |
| 1698 | set_str_and_exp: |
| 1699 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1700 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1701 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1702 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1703 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1704 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1705 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1706 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1707 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1708 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1709 | if (flg_export == -1) { |
| 1710 | cur->flg_export = 0; |
| 1711 | /* unsetenv was already done */ |
| 1712 | } else { |
| 1713 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1714 | return putenv(cur->varstr); |
| 1715 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1716 | } |
| 1717 | return 0; |
| 1718 | } |
| 1719 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1720 | /* Used at startup and after each cd */ |
| 1721 | static void set_pwd_var(int exp) |
| 1722 | { |
| 1723 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), |
| 1724 | /*exp:*/ exp, /*lvl:*/ 0, /*ro:*/ 0); |
| 1725 | } |
| 1726 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1727 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1728 | { |
| 1729 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1730 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1731 | |
| 1732 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1733 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1734 | var_pp = &G.top_var; |
| 1735 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1736 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1737 | if (cur->flg_read_only) { |
| 1738 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1739 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1740 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1741 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1742 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1743 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1744 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1745 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1746 | if (!cur->max_len) |
| 1747 | free(cur->varstr); |
| 1748 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1749 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1750 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1751 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1752 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1753 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1756 | static int unset_local_var(const char *name) |
| 1757 | { |
| 1758 | return unset_local_var_len(name, strlen(name)); |
| 1759 | } |
| 1760 | |
| 1761 | static void unset_vars(char **strings) |
| 1762 | { |
| 1763 | char **v; |
| 1764 | |
| 1765 | if (!strings) |
| 1766 | return; |
| 1767 | v = strings; |
| 1768 | while (*v) { |
| 1769 | const char *eq = strchrnul(*v, '='); |
| 1770 | unset_local_var_len(*v, (int)(eq - *v)); |
| 1771 | v++; |
| 1772 | } |
| 1773 | free(strings); |
| 1774 | } |
| 1775 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1776 | 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] | 1777 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1778 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1779 | set_local_var(var, /*flags:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1780 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1781 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1782 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1783 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1784 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 1785 | */ |
| 1786 | static void add_vars(struct variable *var) |
| 1787 | { |
| 1788 | struct variable *next; |
| 1789 | |
| 1790 | while (var) { |
| 1791 | next = var->next; |
| 1792 | var->next = G.top_var; |
| 1793 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1794 | if (var->flg_export) { |
| 1795 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1796 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1797 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1798 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1799 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1800 | var = next; |
| 1801 | } |
| 1802 | } |
| 1803 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1804 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1805 | { |
| 1806 | char **s; |
| 1807 | struct variable *old = NULL; |
| 1808 | |
| 1809 | if (!strings) |
| 1810 | return old; |
| 1811 | s = strings; |
| 1812 | while (*s) { |
| 1813 | struct variable *var_p; |
| 1814 | struct variable **var_pp; |
| 1815 | char *eq; |
| 1816 | |
| 1817 | eq = strchr(*s, '='); |
| 1818 | if (eq) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1819 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1820 | if (var_pp) { |
| 1821 | /* Remove variable from global linked list */ |
| 1822 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1823 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1824 | *var_pp = var_p->next; |
| 1825 | /* Add it to returned list */ |
| 1826 | var_p->next = old; |
| 1827 | old = var_p; |
| 1828 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1829 | set_local_var(*s, /*exp:*/ 1, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1830 | } |
| 1831 | s++; |
| 1832 | } |
| 1833 | return old; |
| 1834 | } |
| 1835 | |
| 1836 | |
| 1837 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1838 | * in_str support |
| 1839 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1840 | static int FAST_FUNC static_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1841 | { |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1842 | int ch = *i->p; |
| 1843 | if (ch != '\0') { |
| 1844 | i->p++; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1845 | return ch; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1846 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1847 | return EOF; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1850 | static int FAST_FUNC static_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1851 | { |
| 1852 | return *i->p; |
| 1853 | } |
| 1854 | |
| 1855 | #if ENABLE_HUSH_INTERACTIVE |
| 1856 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1857 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1858 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1859 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1860 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1861 | if (G.PS1 == NULL) |
| 1862 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1863 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1864 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1865 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1866 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1867 | if (G.PS2 == NULL) |
| 1868 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1869 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1870 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 1871 | static const char *setup_prompt_string(int promptmode) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1872 | { |
| 1873 | const char *prompt_str; |
| 1874 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1875 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1876 | /* Set up the prompt */ |
| 1877 | if (promptmode == 0) { /* PS1 */ |
| 1878 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1879 | /* bash uses $PWD value, even if it is set by user. |
| 1880 | * It uses current dir only if PWD is unset. |
| 1881 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1882 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1883 | prompt_str = G.PS1; |
| 1884 | } else |
| 1885 | prompt_str = G.PS2; |
| 1886 | } else |
| 1887 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1888 | debug_printf("result '%s'\n", prompt_str); |
| 1889 | return prompt_str; |
| 1890 | } |
| 1891 | |
| 1892 | static void get_user_input(struct in_str *i) |
| 1893 | { |
| 1894 | int r; |
| 1895 | const char *prompt_str; |
| 1896 | |
| 1897 | prompt_str = setup_prompt_string(i->promptmode); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1898 | # if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1899 | /* Enable command line editing only while a command line |
| 1900 | * is actually being read */ |
| 1901 | do { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1902 | G.flag_SIGINT = 0; |
| 1903 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 1904 | * only after <Enter>. (^C will work) */ |
Denys Vlasenko | aaa22d2 | 2009-10-19 16:34:39 +0200 | [diff] [blame] | 1905 | r = read_line_input(prompt_str, G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1, G.line_input_state); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1906 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1907 | check_and_run_traps(0); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1908 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1909 | i->eof_flag = (r < 0); |
| 1910 | if (i->eof_flag) { /* EOF/error detected */ |
| 1911 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1912 | G.user_input_buf[1] = '\0'; |
| 1913 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1914 | # else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1915 | do { |
| 1916 | G.flag_SIGINT = 0; |
| 1917 | fputs(prompt_str, stdout); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1918 | fflush_all(); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1919 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1920 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1921 | //do we need check_and_run_traps(0)? (maybe only if stdin) |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1922 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1923 | i->eof_flag = (r == EOF); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1924 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1925 | i->p = G.user_input_buf; |
| 1926 | } |
| 1927 | |
| 1928 | #endif /* INTERACTIVE */ |
| 1929 | |
| 1930 | /* This is the magic location that prints prompts |
| 1931 | * and gets data back from the user */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1932 | static int FAST_FUNC file_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1933 | { |
| 1934 | int ch; |
| 1935 | |
| 1936 | /* If there is data waiting, eat it up */ |
| 1937 | if (i->p && *i->p) { |
| 1938 | #if ENABLE_HUSH_INTERACTIVE |
| 1939 | take_cached: |
| 1940 | #endif |
| 1941 | ch = *i->p++; |
| 1942 | if (i->eof_flag && !*i->p) |
| 1943 | ch = EOF; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1944 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1945 | } else { |
| 1946 | /* need to double check i->file because we might be doing something |
| 1947 | * more complicated by now, like sourcing or substituting. */ |
| 1948 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 1949 | if (G_interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1950 | do { |
| 1951 | get_user_input(i); |
| 1952 | } while (!*i->p); /* need non-empty line */ |
| 1953 | i->promptmode = 1; /* PS2 */ |
| 1954 | i->promptme = 0; |
| 1955 | goto take_cached; |
| 1956 | } |
| 1957 | #endif |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1958 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1959 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1960 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1961 | #if ENABLE_HUSH_INTERACTIVE |
| 1962 | if (ch == '\n') |
| 1963 | i->promptme = 1; |
| 1964 | #endif |
| 1965 | return ch; |
| 1966 | } |
| 1967 | |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1968 | /* All callers guarantee this routine will never |
| 1969 | * be used right after a newline, so prompting is not needed. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1970 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1971 | static int FAST_FUNC file_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1972 | { |
| 1973 | int ch; |
| 1974 | if (i->p && *i->p) { |
| 1975 | if (i->eof_flag && !i->p[1]) |
| 1976 | return EOF; |
| 1977 | return *i->p; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1978 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1979 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1980 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1981 | i->eof_flag = (ch == EOF); |
| 1982 | i->peek_buf[0] = ch; |
| 1983 | i->peek_buf[1] = '\0'; |
| 1984 | i->p = i->peek_buf; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1985 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1986 | return ch; |
| 1987 | } |
| 1988 | |
| 1989 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1990 | { |
| 1991 | i->peek = file_peek; |
| 1992 | i->get = file_get; |
| 1993 | #if ENABLE_HUSH_INTERACTIVE |
| 1994 | i->promptme = 1; |
| 1995 | i->promptmode = 0; /* PS1 */ |
| 1996 | #endif |
| 1997 | i->file = f; |
| 1998 | i->p = NULL; |
| 1999 | } |
| 2000 | |
| 2001 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 2002 | { |
| 2003 | i->peek = static_peek; |
| 2004 | i->get = static_get; |
| 2005 | #if ENABLE_HUSH_INTERACTIVE |
| 2006 | i->promptme = 1; |
| 2007 | i->promptmode = 0; /* PS1 */ |
| 2008 | #endif |
| 2009 | i->p = s; |
| 2010 | i->eof_flag = 0; |
| 2011 | } |
| 2012 | |
| 2013 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2014 | /* |
| 2015 | * o_string support |
| 2016 | */ |
| 2017 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2018 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2019 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2020 | { |
| 2021 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2022 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2023 | if (o->data) |
| 2024 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2027 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2028 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2029 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2030 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2033 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 2034 | { |
| 2035 | free(o->data); |
| 2036 | } |
| 2037 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2038 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2039 | { |
| 2040 | if (o->length + len > o->maxlen) { |
| 2041 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 2042 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2043 | } |
| 2044 | } |
| 2045 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2046 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2047 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2048 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 2049 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2050 | o->data[o->length] = ch; |
| 2051 | o->length++; |
| 2052 | o->data[o->length] = '\0'; |
| 2053 | } |
| 2054 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2055 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2056 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2057 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2058 | memcpy(&o->data[o->length], str, len); |
| 2059 | o->length += len; |
| 2060 | o->data[o->length] = '\0'; |
| 2061 | } |
| 2062 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2063 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2064 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2065 | o_addblock(o, str, strlen(str)); |
| 2066 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2067 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2068 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2069 | static void nommu_addchr(o_string *o, int ch) |
| 2070 | { |
| 2071 | if (o) |
| 2072 | o_addchr(o, ch); |
| 2073 | } |
| 2074 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2075 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2076 | #endif |
| 2077 | |
| 2078 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2079 | { |
| 2080 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2083 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2084 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2085 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2086 | * Apparently, on unquoted $v bash still does globbing |
| 2087 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2088 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2089 | * quoting mechanisms on $v expansion side: one protects |
| 2090 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2091 | * We have only second one. |
| 2092 | */ |
| 2093 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2094 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2095 | # define MAYBE_BRACES "{}" |
| 2096 | #else |
| 2097 | # define MAYBE_BRACES "" |
| 2098 | #endif |
| 2099 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2100 | /* My analysis of quoting semantics tells me that state information |
| 2101 | * is associated with a destination, not a source. |
| 2102 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2103 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2104 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2105 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2106 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2107 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2108 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2109 | o_grow_by(o, sz); |
| 2110 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2111 | o->data[o->length] = '\\'; |
| 2112 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2113 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2114 | o->data[o->length] = ch; |
| 2115 | o->length++; |
| 2116 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2119 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2120 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2121 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2122 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
| 2123 | && strchr("*?[\\" MAYBE_BRACES, ch) |
| 2124 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2125 | sz++; |
| 2126 | o->data[o->length] = '\\'; |
| 2127 | o->length++; |
| 2128 | } |
| 2129 | o_grow_by(o, sz); |
| 2130 | o->data[o->length] = ch; |
| 2131 | o->length++; |
| 2132 | o->data[o->length] = '\0'; |
| 2133 | } |
| 2134 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2135 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2136 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2137 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2138 | char ch; |
| 2139 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2140 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2141 | if (ordinary_cnt > len) /* paranoia */ |
| 2142 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2143 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2144 | if (ordinary_cnt == len) |
| 2145 | return; |
| 2146 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2147 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2148 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2149 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2150 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2151 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2152 | sz++; |
| 2153 | o->data[o->length] = '\\'; |
| 2154 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2155 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2156 | o_grow_by(o, sz); |
| 2157 | o->data[o->length] = ch; |
| 2158 | o->length++; |
| 2159 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2160 | } |
| 2161 | } |
| 2162 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2163 | static void o_addQblock(o_string *o, const char *str, int len) |
| 2164 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2165 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2166 | o_addblock(o, str, len); |
| 2167 | return; |
| 2168 | } |
| 2169 | o_addqblock(o, str, len); |
| 2170 | } |
| 2171 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2172 | static void o_addQstr(o_string *o, const char *str) |
| 2173 | { |
| 2174 | o_addQblock(o, str, strlen(str)); |
| 2175 | } |
| 2176 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2177 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2178 | * 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] | 2179 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2180 | * list[i] contains an INDEX (int!) into this string data. |
| 2181 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2182 | * but list[i]'s need not be modified. |
| 2183 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2184 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2185 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2186 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2187 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2188 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2189 | { |
| 2190 | char **list = (char**)o->data; |
| 2191 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2192 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2193 | |
| 2194 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2195 | 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] | 2196 | prefix, list, n, string_start, o->length, o->maxlen, |
| 2197 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 2198 | o->has_quoted_part, |
| 2199 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2200 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2201 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2202 | fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i], |
| 2203 | o->data + (int)(uintptr_t)list[i] + string_start, |
| 2204 | o->data + (int)(uintptr_t)list[i] + string_start); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2205 | i++; |
| 2206 | } |
| 2207 | if (n) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2208 | 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] | 2209 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2210 | 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] | 2211 | } |
| 2212 | } |
| 2213 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2214 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2215 | #endif |
| 2216 | |
| 2217 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 2218 | * in list[n] so that it points past last stored byte so far. |
| 2219 | * It returns n+1. */ |
| 2220 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2221 | { |
| 2222 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2223 | int string_start; |
| 2224 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2225 | |
| 2226 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2227 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2228 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2229 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2230 | 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] | 2231 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 2232 | o->maxlen += 0x10 * sizeof(list[0]); |
| 2233 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 2234 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2235 | memmove(list + n + 0x10, list + n, string_len); |
| 2236 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2237 | } else { |
| 2238 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 2239 | n, string_len, string_start); |
| 2240 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2241 | } else { |
| 2242 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2243 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 2244 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2245 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 2246 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2247 | o->has_empty_slot = 0; |
| 2248 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2249 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2250 | return n + 1; |
| 2251 | } |
| 2252 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2253 | /* "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] | 2254 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2255 | { |
| 2256 | char **list = (char**)o->data; |
| 2257 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2258 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2259 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2262 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2263 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 2264 | * first, it processes even {a} (no commas), second, |
| 2265 | * 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] | 2266 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2267 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2268 | |
| 2269 | /* Helper */ |
| 2270 | static int glob_needed(const char *s) |
| 2271 | { |
| 2272 | while (*s) { |
| 2273 | if (*s == '\\') { |
| 2274 | if (!s[1]) |
| 2275 | return 0; |
| 2276 | s += 2; |
| 2277 | continue; |
| 2278 | } |
| 2279 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 2280 | return 1; |
| 2281 | s++; |
| 2282 | } |
| 2283 | return 0; |
| 2284 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2285 | /* Return pointer to next closing brace or to comma */ |
| 2286 | static const char *next_brace_sub(const char *cp) |
| 2287 | { |
| 2288 | unsigned depth = 0; |
| 2289 | cp++; |
| 2290 | while (*cp != '\0') { |
| 2291 | if (*cp == '\\') { |
| 2292 | if (*++cp == '\0') |
| 2293 | break; |
| 2294 | cp++; |
| 2295 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 2296 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2297 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2298 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2299 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2300 | depth++; |
| 2301 | } |
| 2302 | |
| 2303 | return *cp != '\0' ? cp : NULL; |
| 2304 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2305 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 2306 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2307 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2308 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2309 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2310 | const char *next; |
| 2311 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2312 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2313 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2314 | |
| 2315 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 2316 | |
| 2317 | begin = pattern; |
| 2318 | while (1) { |
| 2319 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2320 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2321 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2322 | /* Find the first sub-pattern and at the same time |
| 2323 | * find the rest after the closing brace */ |
| 2324 | next = next_brace_sub(begin); |
| 2325 | if (next == NULL) { |
| 2326 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2327 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2328 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2329 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2330 | /* "{abc}" with no commas - illegal |
| 2331 | * brace expr, disregard and skip it */ |
| 2332 | begin = next + 1; |
| 2333 | continue; |
| 2334 | } |
| 2335 | break; |
| 2336 | } |
| 2337 | if (*begin == '\\' && begin[1] != '\0') |
| 2338 | begin++; |
| 2339 | begin++; |
| 2340 | } |
| 2341 | debug_printf_glob("begin:%s\n", begin); |
| 2342 | debug_printf_glob("next:%s\n", next); |
| 2343 | |
| 2344 | /* Now find the end of the whole brace expression */ |
| 2345 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2346 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2347 | rest = next_brace_sub(rest); |
| 2348 | if (rest == NULL) { |
| 2349 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2350 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2351 | } |
| 2352 | debug_printf_glob("rest:%s\n", rest); |
| 2353 | } |
| 2354 | rest_len = strlen(++rest) + 1; |
| 2355 | |
| 2356 | /* We are sure the brace expression is well-formed */ |
| 2357 | |
| 2358 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2359 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2360 | |
| 2361 | /* We have a brace expression. BEGIN points to the opening {, |
| 2362 | * NEXT points past the terminator of the first element, and REST |
| 2363 | * points past the final }. We will accumulate result names from |
| 2364 | * recursive runs for each brace alternative in the buffer using |
| 2365 | * GLOB_APPEND. */ |
| 2366 | |
| 2367 | p = begin + 1; |
| 2368 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2369 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2370 | memcpy( |
| 2371 | mempcpy( |
| 2372 | mempcpy(new_pattern_buf, |
| 2373 | /* We know the prefix for all sub-patterns */ |
| 2374 | pattern, begin - pattern), |
| 2375 | p, next - p), |
| 2376 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2377 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2378 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 2379 | * That's why we re-copy prefix every time (1st memcpy above). |
| 2380 | */ |
| 2381 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2382 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2383 | /* We saw the last entry */ |
| 2384 | break; |
| 2385 | } |
| 2386 | p = next + 1; |
| 2387 | next = next_brace_sub(next); |
| 2388 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2389 | free(new_pattern_buf); |
| 2390 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2391 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2392 | simple_glob: |
| 2393 | { |
| 2394 | int gr; |
| 2395 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2396 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2397 | memset(&globdata, 0, sizeof(globdata)); |
| 2398 | gr = glob(pattern, 0, NULL, &globdata); |
| 2399 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 2400 | if (gr != 0) { |
| 2401 | if (gr == GLOB_NOMATCH) { |
| 2402 | globfree(&globdata); |
| 2403 | /* NB: garbles parameter */ |
| 2404 | unbackslash(pattern); |
| 2405 | o_addstr_with_NUL(o, pattern); |
| 2406 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2407 | return o_save_ptr_helper(o, n); |
| 2408 | } |
| 2409 | if (gr == GLOB_NOSPACE) |
| 2410 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
| 2411 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2412 | * but we didn't specify it. Paranoia again. */ |
| 2413 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 2414 | } |
| 2415 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2416 | char **argv = globdata.gl_pathv; |
| 2417 | while (1) { |
| 2418 | o_addstr_with_NUL(o, *argv); |
| 2419 | n = o_save_ptr_helper(o, n); |
| 2420 | argv++; |
| 2421 | if (!*argv) |
| 2422 | break; |
| 2423 | } |
| 2424 | } |
| 2425 | globfree(&globdata); |
| 2426 | } |
| 2427 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2428 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2429 | /* Performs globbing on last list[], |
| 2430 | * saving each result as a new list[]. |
| 2431 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2432 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2433 | { |
| 2434 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2435 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2436 | 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] | 2437 | if (!o->data) |
| 2438 | return o_save_ptr_helper(o, n); |
| 2439 | pattern = o->data + o_get_last_ptr(o, n); |
| 2440 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 2441 | if (!glob_needed(pattern)) { |
| 2442 | /* unbackslash last string in o in place, fix length */ |
| 2443 | o->length = unbackslash(pattern) - o->data; |
| 2444 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2445 | return o_save_ptr_helper(o, n); |
| 2446 | } |
| 2447 | |
| 2448 | copy = xstrdup(pattern); |
| 2449 | /* "forget" pattern in o */ |
| 2450 | o->length = pattern - o->data; |
| 2451 | n = glob_brace(copy, o, n); |
| 2452 | free(copy); |
| 2453 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2454 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2455 | return n; |
| 2456 | } |
| 2457 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2458 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2459 | |
| 2460 | /* Helper */ |
| 2461 | static int glob_needed(const char *s) |
| 2462 | { |
| 2463 | while (*s) { |
| 2464 | if (*s == '\\') { |
| 2465 | if (!s[1]) |
| 2466 | return 0; |
| 2467 | s += 2; |
| 2468 | continue; |
| 2469 | } |
| 2470 | if (*s == '*' || *s == '[' || *s == '?') |
| 2471 | return 1; |
| 2472 | s++; |
| 2473 | } |
| 2474 | return 0; |
| 2475 | } |
| 2476 | /* Performs globbing on last list[], |
| 2477 | * saving each result as a new list[]. |
| 2478 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2479 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2480 | { |
| 2481 | glob_t globdata; |
| 2482 | int gr; |
| 2483 | char *pattern; |
| 2484 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2485 | 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] | 2486 | if (!o->data) |
| 2487 | return o_save_ptr_helper(o, n); |
| 2488 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2489 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2490 | if (!glob_needed(pattern)) { |
| 2491 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2492 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2493 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2494 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2495 | return o_save_ptr_helper(o, n); |
| 2496 | } |
| 2497 | |
| 2498 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2499 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 2500 | * If we glob "*.\*" and don't find anything, we need |
| 2501 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 2502 | * will return "*.\*"! |
| 2503 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2504 | gr = glob(pattern, 0, NULL, &globdata); |
| 2505 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2506 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2507 | if (gr == GLOB_NOMATCH) { |
| 2508 | globfree(&globdata); |
| 2509 | goto literal; |
| 2510 | } |
| 2511 | if (gr == GLOB_NOSPACE) |
| 2512 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2513 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2514 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2515 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2516 | } |
| 2517 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2518 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2519 | /* "forget" pattern in o */ |
| 2520 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2521 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2522 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2523 | n = o_save_ptr_helper(o, n); |
| 2524 | argv++; |
| 2525 | if (!*argv) |
| 2526 | break; |
| 2527 | } |
| 2528 | } |
| 2529 | globfree(&globdata); |
| 2530 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2531 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2532 | return n; |
| 2533 | } |
| 2534 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2535 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2536 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2537 | /* 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] | 2538 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2539 | static int o_save_ptr(o_string *o, int n) |
| 2540 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2541 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2542 | /* If o->has_empty_slot, list[n] was already globbed |
| 2543 | * (if it was requested back then when it was filled) |
| 2544 | * so don't do that again! */ |
| 2545 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2546 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2547 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2548 | return o_save_ptr_helper(o, n); |
| 2549 | } |
| 2550 | |
| 2551 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2552 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2553 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2554 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2555 | int string_start; |
| 2556 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2557 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 2558 | if (DEBUG_EXPAND) |
| 2559 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2560 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2561 | list = (char**)o->data; |
| 2562 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2563 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2564 | while (n) { |
| 2565 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2566 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2567 | } |
| 2568 | return list; |
| 2569 | } |
| 2570 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2571 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2572 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2573 | /* Returns pi->next - next pipe in the list */ |
| 2574 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2575 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2576 | struct pipe *next; |
| 2577 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2578 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2579 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2580 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2581 | struct command *command; |
| 2582 | struct redir_struct *r, *rnext; |
| 2583 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2584 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2585 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2586 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2587 | if (DEBUG_CLEAN) { |
| 2588 | int a; |
| 2589 | char **p; |
| 2590 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 2591 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 2592 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2593 | } |
| 2594 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2595 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2596 | } |
| 2597 | /* not "else if": on syntax error, we may have both! */ |
| 2598 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2599 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 2600 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2601 | free_pipe_list(command->group); |
| 2602 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2603 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2604 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 2605 | /* else is crucial here. |
| 2606 | * If group != NULL, child_func is meaningless */ |
| 2607 | #if ENABLE_HUSH_FUNCTIONS |
| 2608 | else if (command->child_func) { |
| 2609 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 2610 | command->child_func->parent_cmd = NULL; |
| 2611 | } |
| 2612 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2613 | #if !BB_MMU |
| 2614 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2615 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2616 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2617 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2618 | debug_printf_clean(" redirect %d%s", |
| 2619 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2620 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 2621 | if (r->rd_filename) { |
| 2622 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 2623 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2624 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2625 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2626 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2627 | rnext = r->next; |
| 2628 | free(r); |
| 2629 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2630 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2631 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2632 | 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] | 2633 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2634 | #if ENABLE_HUSH_JOB |
| 2635 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2636 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2637 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2638 | |
| 2639 | next = pi->next; |
| 2640 | free(pi); |
| 2641 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2642 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2643 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2644 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2645 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2646 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2647 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2648 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2649 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2650 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2651 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2652 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
| 2655 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 2656 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2657 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2658 | #ifndef debug_print_tree |
| 2659 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 2660 | { |
| 2661 | static const char *const PIPE[] = { |
| 2662 | [PIPE_SEQ] = "SEQ", |
| 2663 | [PIPE_AND] = "AND", |
| 2664 | [PIPE_OR ] = "OR" , |
| 2665 | [PIPE_BG ] = "BG" , |
| 2666 | }; |
| 2667 | static const char *RES[] = { |
| 2668 | [RES_NONE ] = "NONE" , |
| 2669 | # if ENABLE_HUSH_IF |
| 2670 | [RES_IF ] = "IF" , |
| 2671 | [RES_THEN ] = "THEN" , |
| 2672 | [RES_ELIF ] = "ELIF" , |
| 2673 | [RES_ELSE ] = "ELSE" , |
| 2674 | [RES_FI ] = "FI" , |
| 2675 | # endif |
| 2676 | # if ENABLE_HUSH_LOOPS |
| 2677 | [RES_FOR ] = "FOR" , |
| 2678 | [RES_WHILE] = "WHILE", |
| 2679 | [RES_UNTIL] = "UNTIL", |
| 2680 | [RES_DO ] = "DO" , |
| 2681 | [RES_DONE ] = "DONE" , |
| 2682 | # endif |
| 2683 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 2684 | [RES_IN ] = "IN" , |
| 2685 | # endif |
| 2686 | # if ENABLE_HUSH_CASE |
| 2687 | [RES_CASE ] = "CASE" , |
| 2688 | [RES_CASE_IN ] = "CASE_IN" , |
| 2689 | [RES_MATCH] = "MATCH", |
| 2690 | [RES_CASE_BODY] = "CASE_BODY", |
| 2691 | [RES_ESAC ] = "ESAC" , |
| 2692 | # endif |
| 2693 | [RES_XXXX ] = "XXXX" , |
| 2694 | [RES_SNTX ] = "SNTX" , |
| 2695 | }; |
| 2696 | static const char *const CMDTYPE[] = { |
| 2697 | "{}", |
| 2698 | "()", |
| 2699 | "[noglob]", |
| 2700 | # if ENABLE_HUSH_FUNCTIONS |
| 2701 | "func()", |
| 2702 | # endif |
| 2703 | }; |
| 2704 | |
| 2705 | int pin, prn; |
| 2706 | |
| 2707 | pin = 0; |
| 2708 | while (pi) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2709 | 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] | 2710 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
| 2711 | prn = 0; |
| 2712 | while (prn < pi->num_cmds) { |
| 2713 | struct command *command = &pi->cmds[prn]; |
| 2714 | char **argv = command->argv; |
| 2715 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2716 | fdprintf(2, "%*s cmd %d assignment_cnt:%d", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2717 | lvl*2, "", prn, |
| 2718 | command->assignment_cnt); |
| 2719 | if (command->group) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2720 | fdprintf(2, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2721 | CMDTYPE[command->cmd_type], |
| 2722 | argv |
| 2723 | # if !BB_MMU |
| 2724 | , " group_as_string:", command->group_as_string |
| 2725 | # else |
| 2726 | , "", "" |
| 2727 | # endif |
| 2728 | ); |
| 2729 | debug_print_tree(command->group, lvl+1); |
| 2730 | prn++; |
| 2731 | continue; |
| 2732 | } |
| 2733 | if (argv) while (*argv) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2734 | fdprintf(2, " '%s'", *argv); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2735 | argv++; |
| 2736 | } |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2737 | fdprintf(2, "\n"); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2738 | prn++; |
| 2739 | } |
| 2740 | pi = pi->next; |
| 2741 | pin++; |
| 2742 | } |
| 2743 | } |
| 2744 | #endif /* debug_print_tree */ |
| 2745 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 2746 | static struct pipe *new_pipe(void) |
| 2747 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2748 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2749 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2750 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2751 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2752 | return pi; |
| 2753 | } |
| 2754 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2755 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 2756 | * if ctx->command is NULL. |
| 2757 | * No errors possible here. |
| 2758 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2759 | static int done_command(struct parse_context *ctx) |
| 2760 | { |
| 2761 | /* The command is really already in the pipe structure, so |
| 2762 | * advance the pipe counter and make a new, null command. */ |
| 2763 | struct pipe *pi = ctx->pipe; |
| 2764 | struct command *command = ctx->command; |
| 2765 | |
| 2766 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2767 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2768 | 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] | 2769 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2770 | } |
| 2771 | pi->num_cmds++; |
| 2772 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2773 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2774 | } else { |
| 2775 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 2776 | } |
| 2777 | |
| 2778 | /* Only real trickiness here is that the uncommitted |
| 2779 | * command structure is not counted in pi->num_cmds. */ |
| 2780 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2781 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 2782 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2783 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2784 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 2785 | } |
| 2786 | |
| 2787 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 2788 | { |
| 2789 | int not_null; |
| 2790 | |
| 2791 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 2792 | /* Close previous command */ |
| 2793 | not_null = done_command(ctx); |
| 2794 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2795 | #if HAS_KEYWORDS |
| 2796 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 2797 | ctx->ctx_inverted = 0; |
| 2798 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 2799 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2800 | |
| 2801 | /* Without this check, even just <enter> on command line generates |
| 2802 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2803 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2804 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 2805 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2806 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 2807 | #endif |
| 2808 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2809 | || ctx->ctx_res_w == RES_DONE |
| 2810 | || ctx->ctx_res_w == RES_FOR |
| 2811 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 2812 | #endif |
| 2813 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2814 | || ctx->ctx_res_w == RES_ESAC |
| 2815 | #endif |
| 2816 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2817 | struct pipe *new_p; |
| 2818 | debug_printf_parse("done_pipe: adding new pipe: " |
| 2819 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 2820 | not_null, ctx->ctx_res_w); |
| 2821 | new_p = new_pipe(); |
| 2822 | ctx->pipe->next = new_p; |
| 2823 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2824 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2825 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2826 | * This is used to control execution. |
| 2827 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 2828 | * cases where variable or value happens to match a keyword): |
| 2829 | */ |
| 2830 | #if ENABLE_HUSH_LOOPS |
| 2831 | if (ctx->ctx_res_w == RES_FOR |
| 2832 | || ctx->ctx_res_w == RES_IN) |
| 2833 | ctx->ctx_res_w = RES_NONE; |
| 2834 | #endif |
| 2835 | #if ENABLE_HUSH_CASE |
| 2836 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 2837 | ctx->ctx_res_w = RES_CASE_BODY; |
| 2838 | if (ctx->ctx_res_w == RES_CASE) |
| 2839 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2840 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2841 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2842 | /* Create the memory for command, roughly: |
| 2843 | * ctx->pipe->cmds = new struct command; |
| 2844 | * ctx->command = &ctx->pipe->cmds[0]; |
| 2845 | */ |
| 2846 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2847 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2848 | } |
| 2849 | debug_printf_parse("done_pipe return\n"); |
| 2850 | } |
| 2851 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2852 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2853 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2854 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2855 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2856 | /* Create the memory for command, roughly: |
| 2857 | * ctx->pipe->cmds = new struct command; |
| 2858 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2859 | */ |
| 2860 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2863 | /* If a reserved word is found and processed, parse context is modified |
| 2864 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2865 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2866 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2867 | struct reserved_combo { |
| 2868 | char literal[6]; |
| 2869 | unsigned char res; |
| 2870 | unsigned char assignment_flag; |
| 2871 | int flag; |
| 2872 | }; |
| 2873 | enum { |
| 2874 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2875 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2876 | FLAG_IF = (1 << RES_IF ), |
| 2877 | FLAG_THEN = (1 << RES_THEN ), |
| 2878 | FLAG_ELIF = (1 << RES_ELIF ), |
| 2879 | FLAG_ELSE = (1 << RES_ELSE ), |
| 2880 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2881 | # endif |
| 2882 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2883 | FLAG_FOR = (1 << RES_FOR ), |
| 2884 | FLAG_WHILE = (1 << RES_WHILE), |
| 2885 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 2886 | FLAG_DO = (1 << RES_DO ), |
| 2887 | FLAG_DONE = (1 << RES_DONE ), |
| 2888 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2889 | # endif |
| 2890 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2891 | FLAG_MATCH = (1 << RES_MATCH), |
| 2892 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2893 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2894 | FLAG_START = (1 << RES_XXXX ), |
| 2895 | }; |
| 2896 | |
| 2897 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 2898 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2899 | /* Mostly a list of accepted follow-up reserved words. |
| 2900 | * FLAG_END means we are done with the sequence, and are ready |
| 2901 | * to turn the compound list into a command. |
| 2902 | * FLAG_START means the word must start a new compound list. |
| 2903 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2904 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2905 | # if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2906 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 2907 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 2908 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 2909 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 2910 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 2911 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2912 | # endif |
| 2913 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2914 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 2915 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 2916 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 2917 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 2918 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 2919 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2920 | # endif |
| 2921 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2922 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 2923 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2924 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2925 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2926 | const struct reserved_combo *r; |
| 2927 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 2928 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2929 | if (strcmp(word->data, r->literal) == 0) |
| 2930 | return r; |
| 2931 | } |
| 2932 | return NULL; |
| 2933 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2934 | /* Return 0: not a keyword, 1: keyword |
| 2935 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2936 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 2937 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2938 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2939 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2940 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2941 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2942 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2943 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2944 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2945 | if (word->has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2946 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2947 | r = match_reserved_word(word); |
| 2948 | if (!r) |
| 2949 | return 0; |
| 2950 | |
| 2951 | 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] | 2952 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 2953 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 2954 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2955 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 2956 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2957 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2958 | if (r->flag == 0) { /* '!' */ |
| 2959 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2960 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2961 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2962 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2963 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2964 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2965 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2966 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2967 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2968 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2969 | old = xmalloc(sizeof(*old)); |
| 2970 | debug_printf_parse("push stack %p\n", old); |
| 2971 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2972 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2973 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2974 | } 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] | 2975 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2976 | ctx->ctx_res_w = RES_SNTX; |
| 2977 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2978 | } else { |
| 2979 | /* "{...} fi" is ok. "{...} if" is not |
| 2980 | * Example: |
| 2981 | * if { echo foo; } then { echo bar; } fi */ |
| 2982 | if (ctx->command->group) |
| 2983 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2984 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2985 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2986 | ctx->ctx_res_w = r->res; |
| 2987 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2988 | word->o_assignment = r->assignment_flag; |
| 2989 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2990 | if (ctx->old_flag & FLAG_END) { |
| 2991 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2992 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2993 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2994 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2995 | old = ctx->stack; |
| 2996 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2997 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2998 | # if !BB_MMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2999 | o_addstr(&old->as_string, ctx->as_string.data); |
| 3000 | o_free_unsafe(&ctx->as_string); |
| 3001 | old->command->group_as_string = xstrdup(old->as_string.data); |
| 3002 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 3003 | old->command->group_as_string); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3004 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3005 | *ctx = *old; /* physical copy */ |
| 3006 | free(old); |
| 3007 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3008 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3009 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3010 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3011 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3012 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3013 | * Normal return is 0. Syntax errors return 1. |
| 3014 | * Note: on return, word is reset, but not o_free'd! |
| 3015 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3016 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3017 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3018 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3019 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3020 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3021 | if (word->length == 0 && !word->has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3022 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3023 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3024 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3025 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3026 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3027 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3028 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3029 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 3030 | * "2.7 Redirection |
| 3031 | * ...the word that follows the redirection operator |
| 3032 | * shall be subjected to tilde expansion, parameter expansion, |
| 3033 | * command substitution, arithmetic expansion, and quote |
| 3034 | * removal. Pathname expansion shall not be performed |
| 3035 | * on the word by a non-interactive shell; an interactive |
| 3036 | * shell may perform it, but shall do so only when |
| 3037 | * the expansion would result in one word." |
| 3038 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3039 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3040 | /* Cater for >\file case: |
| 3041 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 3042 | * Same with heredocs: |
| 3043 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 3044 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3045 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 3046 | unbackslash(ctx->pending_redirect->rd_filename); |
| 3047 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3048 | if (word->has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3049 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 3050 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3051 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3052 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3053 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3054 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3055 | /* If this word wasn't an assignment, next ones definitely |
| 3056 | * can't be assignments. Even if they look like ones. */ |
| 3057 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 3058 | && word->o_assignment != WORD_IS_KEYWORD |
| 3059 | ) { |
| 3060 | word->o_assignment = NOT_ASSIGNMENT; |
| 3061 | } else { |
| 3062 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
| 3063 | command->assignment_cnt++; |
| 3064 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 3065 | } |
| 3066 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3067 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3068 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3069 | if (ctx->ctx_dsemicolon |
| 3070 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 3071 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3072 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3073 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3074 | ctx->ctx_dsemicolon = 0; |
| 3075 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3076 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3077 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3078 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3079 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3080 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3081 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3082 | # if ENABLE_HUSH_CASE |
| 3083 | && ctx->ctx_res_w != RES_CASE |
| 3084 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3085 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3086 | debug_printf_parse("checking '%s' for reserved-ness\n", word->data); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3087 | if (reserved_word(word, ctx)) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3088 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3089 | debug_printf_parse("done_word return %d\n", |
| 3090 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3091 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3092 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3093 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3094 | if (strcmp(word->data, "[[") == 0) { |
| 3095 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 3096 | } |
| 3097 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3098 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3099 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3100 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3101 | if (command->group) { |
| 3102 | /* "{ echo foo; } echo bar" - bad */ |
| 3103 | syntax_error_at(word->data); |
| 3104 | debug_printf_parse("done_word return 1: syntax error, " |
| 3105 | "groups and arglists don't mix\n"); |
| 3106 | return 1; |
| 3107 | } |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3108 | if (word->has_quoted_part |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3109 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 3110 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3111 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3112 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3113 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3114 | char *p = word->data; |
| 3115 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 3116 | && (p[1] & 0x7f) == '@' |
| 3117 | && p[2] == SPECIAL_VAR_SYMBOL |
| 3118 | ) { |
| 3119 | p += 3; |
| 3120 | } |
| 3121 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3122 | /* saw no "$@", or not only "$@" but some |
| 3123 | * real text is there too */ |
| 3124 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3125 | * e.g. "", $empty"" etc to not disappear */ |
| 3126 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3127 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3128 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3129 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3130 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3131 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3132 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3133 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3134 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3135 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3136 | if (word->has_quoted_part |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3137 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 3138 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3139 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3140 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3141 | return 1; |
| 3142 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3143 | /* Force FOR to have just one word (variable name) */ |
| 3144 | /* NB: basically, this makes hush see "for v in ..." |
| 3145 | * syntax as if it is "for v; in ...". FOR and IN become |
| 3146 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3147 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3148 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3149 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3150 | #if ENABLE_HUSH_CASE |
| 3151 | /* Force CASE to have just one word */ |
| 3152 | if (ctx->ctx_res_w == RES_CASE) { |
| 3153 | done_pipe(ctx, PIPE_SEQ); |
| 3154 | } |
| 3155 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3156 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3157 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3158 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3159 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3160 | return 0; |
| 3161 | } |
| 3162 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3163 | |
| 3164 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 3165 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3166 | * Return: |
| 3167 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 3168 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 3169 | * REDIRFD_TO_FILE if no & was seen, |
| 3170 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3171 | */ |
| 3172 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3173 | #define parse_redir_right_fd(as_string, input) \ |
| 3174 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3175 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3176 | 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] | 3177 | { |
| 3178 | int ch, d, ok; |
| 3179 | |
| 3180 | ch = i_peek(input); |
| 3181 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3182 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3183 | |
| 3184 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3185 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3186 | ch = i_peek(input); |
| 3187 | if (ch == '-') { |
| 3188 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3189 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3190 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3191 | } |
| 3192 | d = 0; |
| 3193 | ok = 0; |
| 3194 | while (ch != EOF && isdigit(ch)) { |
| 3195 | d = d*10 + (ch-'0'); |
| 3196 | ok = 1; |
| 3197 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3198 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3199 | ch = i_peek(input); |
| 3200 | } |
| 3201 | if (ok) return d; |
| 3202 | |
| 3203 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 3204 | |
| 3205 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3206 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3209 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3210 | */ |
| 3211 | static int parse_redirect(struct parse_context *ctx, |
| 3212 | int fd, |
| 3213 | redir_type style, |
| 3214 | struct in_str *input) |
| 3215 | { |
| 3216 | struct command *command = ctx->command; |
| 3217 | struct redir_struct *redir; |
| 3218 | struct redir_struct **redirp; |
| 3219 | int dup_num; |
| 3220 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3221 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3222 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3223 | /* Check for a '>&1' type redirect */ |
| 3224 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 3225 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 3226 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3227 | } else { |
| 3228 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3229 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3230 | if (dup_num) { /* <<-... */ |
| 3231 | ch = i_getch(input); |
| 3232 | nommu_addchr(&ctx->as_string, ch); |
| 3233 | ch = i_peek(input); |
| 3234 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3235 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3236 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3237 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3238 | int ch = i_peek(input); |
| 3239 | if (ch == '|') { |
| 3240 | /* >|FILE redirect ("clobbering" >). |
| 3241 | * Since we do not support "set -o noclobber" yet, |
| 3242 | * >| and > are the same for now. Just eat |. |
| 3243 | */ |
| 3244 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3245 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3246 | } |
| 3247 | } |
| 3248 | |
| 3249 | /* Create a new redir_struct and append it to the linked list */ |
| 3250 | redirp = &command->redirects; |
| 3251 | while ((redir = *redirp) != NULL) { |
| 3252 | redirp = &(redir->next); |
| 3253 | } |
| 3254 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 3255 | /* redir->next = NULL; */ |
| 3256 | /* redir->rd_filename = NULL; */ |
| 3257 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3258 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3259 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3260 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 3261 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3262 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3263 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3264 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3265 | /* Erik had a check here that the file descriptor in question |
| 3266 | * is legit; I postpone that to "run time" |
| 3267 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3268 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 3269 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3270 | } else { |
| 3271 | /* Set ctx->pending_redirect, so we know what to do at the |
| 3272 | * end of the next parsed word. */ |
| 3273 | ctx->pending_redirect = redir; |
| 3274 | } |
| 3275 | return 0; |
| 3276 | } |
| 3277 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3278 | /* If a redirect is immediately preceded by a number, that number is |
| 3279 | * supposed to tell which file descriptor to redirect. This routine |
| 3280 | * looks for such preceding numbers. In an ideal world this routine |
| 3281 | * needs to handle all the following classes of redirects... |
| 3282 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3283 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3284 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3285 | * 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] | 3286 | * |
| 3287 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 3288 | * "2.7 Redirection |
| 3289 | * ... If n is quoted, the number shall not be recognized as part of |
| 3290 | * the redirection expression. For example: |
| 3291 | * echo \2>a |
| 3292 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3293 | * 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] | 3294 | * |
| 3295 | * A -1 return means no valid number was found, |
| 3296 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3297 | */ |
| 3298 | static int redirect_opt_num(o_string *o) |
| 3299 | { |
| 3300 | int num; |
| 3301 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3302 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3303 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3304 | num = bb_strtou(o->data, NULL, 10); |
| 3305 | if (errno || num < 0) |
| 3306 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3307 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3308 | return num; |
| 3309 | } |
| 3310 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3311 | #if BB_MMU |
| 3312 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 3313 | fetch_till_str(input, word, skip_tabs) |
| 3314 | #endif |
| 3315 | static char *fetch_till_str(o_string *as_string, |
| 3316 | struct in_str *input, |
| 3317 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3318 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3319 | { |
| 3320 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3321 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3322 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3323 | int ch; |
| 3324 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3325 | goto jump_in; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3326 | while (1) { |
| 3327 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3328 | if (ch != EOF) |
| 3329 | nommu_addchr(as_string, ch); |
| 3330 | if ((ch == '\n' || ch == EOF) |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3331 | && ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') |
| 3332 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3333 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 3334 | heredoc.data[past_EOL] = '\0'; |
| 3335 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 3336 | return heredoc.data; |
| 3337 | } |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3338 | while (ch == '\n') { |
| 3339 | o_addchr(&heredoc, ch); |
| 3340 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3341 | jump_in: |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3342 | past_EOL = heredoc.length; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3343 | do { |
| 3344 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3345 | if (ch != EOF) |
| 3346 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3347 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3348 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3349 | } |
| 3350 | if (ch == EOF) { |
| 3351 | o_free_unsafe(&heredoc); |
| 3352 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3353 | } |
| 3354 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3355 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 3356 | if (prev == '\\' && ch == '\\') |
| 3357 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
| 3358 | prev = 0; /* not \ */ |
| 3359 | else |
| 3360 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3361 | } |
| 3362 | } |
| 3363 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3364 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 3365 | * and load them all. There should be exactly heredoc_cnt of them. |
| 3366 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3367 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 3368 | { |
| 3369 | struct pipe *pi = ctx->list_head; |
| 3370 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3371 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3372 | int i; |
| 3373 | struct command *cmd = pi->cmds; |
| 3374 | |
| 3375 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 3376 | pi->num_cmds, |
| 3377 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 3378 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3379 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3380 | |
| 3381 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 3382 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3383 | while (redir) { |
| 3384 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3385 | char *p; |
| 3386 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3387 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3388 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3389 | p = fetch_till_str(&ctx->as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3390 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3391 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3392 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3393 | return 1; |
| 3394 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3395 | free(redir->rd_filename); |
| 3396 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3397 | heredoc_cnt--; |
| 3398 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3399 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3400 | } |
| 3401 | cmd++; |
| 3402 | } |
| 3403 | pi = pi->next; |
| 3404 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3405 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3406 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3407 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3408 | bb_error_msg_and_die("heredoc BUG 2"); |
| 3409 | #endif |
| 3410 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
| 3413 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3414 | static int run_list(struct pipe *pi); |
| 3415 | #if BB_MMU |
| 3416 | #define parse_stream(pstring, input, end_trigger) \ |
| 3417 | parse_stream(input, end_trigger) |
| 3418 | #endif |
| 3419 | static struct pipe *parse_stream(char **pstring, |
| 3420 | struct in_str *input, |
| 3421 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3422 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3423 | |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 3424 | #if !ENABLE_HUSH_FUNCTIONS |
| 3425 | #define parse_group(dest, ctx, input, ch) \ |
| 3426 | parse_group(ctx, input, ch) |
| 3427 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3428 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3429 | struct in_str *input, int ch) |
| 3430 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3431 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 3432 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3433 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3434 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3435 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3436 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3437 | |
| 3438 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3439 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3440 | if (ch == '(' && !dest->has_quoted_part) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3441 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3442 | if (done_word(dest, ctx)) |
| 3443 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3444 | if (!command->argv) |
| 3445 | goto skip; /* (... */ |
| 3446 | if (command->argv[1]) { /* word word ... (... */ |
| 3447 | syntax_error_unexpected_ch('('); |
| 3448 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3449 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3450 | /* it is "word(..." or "word (..." */ |
| 3451 | do |
| 3452 | ch = i_getch(input); |
| 3453 | while (ch == ' ' || ch == '\t'); |
| 3454 | if (ch != ')') { |
| 3455 | syntax_error_unexpected_ch(ch); |
| 3456 | return 1; |
| 3457 | } |
| 3458 | nommu_addchr(&ctx->as_string, ch); |
| 3459 | do |
| 3460 | ch = i_getch(input); |
| 3461 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 3462 | if (ch != '{') { |
| 3463 | syntax_error_unexpected_ch(ch); |
| 3464 | return 1; |
| 3465 | } |
| 3466 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3467 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3468 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3469 | } |
| 3470 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3471 | |
| 3472 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3473 | if (command->argv /* word [word]{... */ |
| 3474 | || dest->length /* word{... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3475 | || dest->has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3476 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3477 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3478 | debug_printf_parse("parse_group return 1: " |
| 3479 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3480 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3481 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3482 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3483 | |
| 3484 | #if ENABLE_HUSH_FUNCTIONS |
| 3485 | skip: |
| 3486 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3487 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3488 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3489 | endch = ')'; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3490 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3491 | } else { |
| 3492 | /* bash does not allow "{echo...", requires whitespace */ |
| 3493 | ch = i_getch(input); |
| 3494 | if (ch != ' ' && ch != '\t' && ch != '\n') { |
| 3495 | syntax_error_unexpected_ch(ch); |
| 3496 | return 1; |
| 3497 | } |
| 3498 | nommu_addchr(&ctx->as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3499 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3500 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3501 | { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3502 | #if BB_MMU |
| 3503 | # define as_string NULL |
| 3504 | #else |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3505 | char *as_string = NULL; |
| 3506 | #endif |
| 3507 | pipe_list = parse_stream(&as_string, input, endch); |
| 3508 | #if !BB_MMU |
| 3509 | if (as_string) |
| 3510 | o_addstr(&ctx->as_string, as_string); |
| 3511 | #endif |
| 3512 | /* empty ()/{} or parse error? */ |
| 3513 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3514 | /* parse_stream already emitted error msg */ |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3515 | if (!BB_MMU) |
| 3516 | free(as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3517 | debug_printf_parse("parse_group return 1: " |
| 3518 | "parse_stream returned %p\n", pipe_list); |
| 3519 | return 1; |
| 3520 | } |
| 3521 | command->group = pipe_list; |
| 3522 | #if !BB_MMU |
| 3523 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 3524 | command->group_as_string = as_string; |
| 3525 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 3526 | command->group_as_string); |
| 3527 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3528 | #undef as_string |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3529 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3530 | debug_printf_parse("parse_group return 0\n"); |
| 3531 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3532 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3535 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3536 | /* Subroutines for copying $(...) and `...` things */ |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3537 | static void 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] | 3538 | /* '...' */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3539 | static void add_till_single_quote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3540 | { |
| 3541 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3542 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3543 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3544 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3545 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3546 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3547 | if (ch == '\'') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3548 | return; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3549 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3550 | } |
| 3551 | } |
| 3552 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3553 | static void add_till_double_quote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3554 | { |
| 3555 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3556 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3557 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3558 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3559 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3560 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3561 | if (ch == '"') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3562 | return; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3563 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3564 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3565 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3566 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3567 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3568 | if (ch == '`') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3569 | add_till_backquote(dest, input, /*in_dquote:*/ 1); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3570 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3571 | continue; |
| 3572 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 3573 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3574 | } |
| 3575 | } |
| 3576 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 3577 | * \` quoting. |
| 3578 | * "Within the backquoted style of command substitution, backslash |
| 3579 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 3580 | * The search for the matching backquote shall be satisfied by the first |
| 3581 | * backquote found without a preceding backslash; during this search, |
| 3582 | * if a non-escaped backquote is encountered within a shell comment, |
| 3583 | * a here-document, an embedded command substitution of the $(command) |
| 3584 | * form, or a quoted string, undefined results occur. A single-quoted |
| 3585 | * or double-quoted string that begins, but does not end, within the |
| 3586 | * "`...`" sequence produces undefined results." |
| 3587 | * Example Output |
| 3588 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 3589 | */ |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3590 | static void 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] | 3591 | { |
| 3592 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3593 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3594 | if (ch == '`') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3595 | return; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3596 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3597 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 3598 | ch = i_getch(input); |
| 3599 | if (ch != '`' |
| 3600 | && ch != '$' |
| 3601 | && ch != '\\' |
| 3602 | && (!in_dquote || ch != '"') |
| 3603 | ) { |
| 3604 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3605 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3606 | } |
| 3607 | if (ch == EOF) { |
| 3608 | syntax_error_unterm_ch('`'); |
| 3609 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3610 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3611 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3612 | } |
| 3613 | } |
| 3614 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 3615 | * quoting and nested ()s. |
| 3616 | * "With the $(command) style of command substitution, all characters |
| 3617 | * following the open parenthesis to the matching closing parenthesis |
| 3618 | * constitute the command. Any valid shell script can be used for command, |
| 3619 | * except a script consisting solely of redirections which produces |
| 3620 | * unspecified results." |
| 3621 | * Example Output |
| 3622 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 3623 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 3624 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3625 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3626 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3627 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3628 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 3629 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3630 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3631 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3632 | 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] | 3633 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3634 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3635 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3636 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3637 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3638 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3639 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 3640 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3641 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3642 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3643 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3644 | syntax_error_unterm_ch(end_ch); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3645 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3646 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3647 | if (ch == end_ch IF_HUSH_BASH_COMPAT( || ch == end_char2)) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3648 | if (!dbl) |
| 3649 | break; |
| 3650 | /* we look for closing )) of $((EXPR)) */ |
| 3651 | if (i_peek(input) == end_ch) { |
| 3652 | i_getch(input); /* eat second ')' */ |
| 3653 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3654 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3655 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3656 | o_addchr(dest, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3657 | if (ch == '(' || ch == '{') { |
| 3658 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3659 | add_till_closing_bracket(dest, input, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3660 | o_addchr(dest, ch); |
| 3661 | continue; |
| 3662 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3663 | if (ch == '\'') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3664 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3665 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3666 | continue; |
| 3667 | } |
| 3668 | if (ch == '"') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3669 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3670 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3671 | continue; |
| 3672 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3673 | if (ch == '`') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3674 | add_till_backquote(dest, input, /*in_dquote:*/ 0); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3675 | o_addchr(dest, ch); |
| 3676 | continue; |
| 3677 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3678 | if (ch == '\\') { |
| 3679 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3680 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3681 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3682 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3683 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3684 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3685 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3686 | continue; |
| 3687 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3688 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3689 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3690 | } |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3691 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3692 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3693 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3694 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3695 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 3696 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3697 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3698 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3699 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3700 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3701 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3702 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3703 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3704 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3705 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3706 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3707 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3708 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3709 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3710 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3711 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3712 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3713 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3714 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3715 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3716 | if (!isalnum(ch) && ch != '_') |
| 3717 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3718 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3719 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3720 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3721 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3722 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3723 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3724 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3725 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3726 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3727 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3728 | o_addchr(dest, ch | quote_mask); |
| 3729 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3730 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3731 | case '$': /* pid */ |
| 3732 | case '!': /* last bg pid */ |
| 3733 | case '?': /* last exit code */ |
| 3734 | case '#': /* number of args */ |
| 3735 | case '*': /* args */ |
| 3736 | case '@': /* args */ |
| 3737 | goto make_one_char_var; |
| 3738 | case '{': { |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 3739 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3740 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3741 | ch = i_getch(input); /* eat '{' */ |
| 3742 | nommu_addchr(as_string, ch); |
| 3743 | |
| 3744 | ch = i_getch(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3745 | /* It should be ${?}, or ${#var}, |
| 3746 | * or even ${?+subst} - operator acting on a special variable, |
| 3747 | * or the beginning of variable name. |
| 3748 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3749 | if (ch == EOF |
| 3750 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 3751 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3752 | bad_dollar_syntax: |
| 3753 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3754 | debug_printf_parse("parse_dollar return 1: unterminated ${name}\n"); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3755 | return 1; |
| 3756 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3757 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3758 | ch |= quote_mask; |
| 3759 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3760 | /* 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] | 3761 | * However, this regresses some of our testsuite cases |
| 3762 | * which check invalid constructs like ${%}. |
| 3763 | * Oh well... let's check that the var name part is fine... */ |
| 3764 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3765 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3766 | unsigned pos; |
| 3767 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3768 | o_addchr(dest, ch); |
| 3769 | debug_printf_parse(": '%c'\n", ch); |
| 3770 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3771 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3772 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3773 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3774 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3775 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3776 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3777 | unsigned end_ch; |
| 3778 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3779 | /* handle parameter expansions |
| 3780 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 3781 | */ |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3782 | if (!strchr(VAR_SUBST_OPS, ch)) /* ${var<bad_char>... */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3783 | goto bad_dollar_syntax; |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3784 | |
| 3785 | /* Eat everything until closing '}' (or ':') */ |
| 3786 | end_ch = '}'; |
| 3787 | if (ENABLE_HUSH_BASH_COMPAT |
| 3788 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3789 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3790 | ) { |
| 3791 | /* It's ${var:N[:M]} thing */ |
| 3792 | end_ch = '}' * 0x100 + ':'; |
| 3793 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3794 | if (ENABLE_HUSH_BASH_COMPAT |
| 3795 | && ch == '/' |
| 3796 | ) { |
| 3797 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 3798 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 3799 | i_getch(input); |
| 3800 | nommu_addchr(as_string, '/'); |
| 3801 | ch = '\\'; |
| 3802 | } |
| 3803 | end_ch = '}' * 0x100 + '/'; |
| 3804 | } |
| 3805 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3806 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3807 | if (!BB_MMU) |
| 3808 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3809 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3810 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3811 | #else |
| 3812 | #error Simple code to only allow ${var} is not implemented |
| 3813 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3814 | if (as_string) { |
| 3815 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3816 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3817 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3818 | |
| 3819 | if (ENABLE_HUSH_BASH_COMPAT && (end_ch & 0xff00)) { |
| 3820 | /* close the first block: */ |
| 3821 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3822 | /* while parsing N from ${var:N[:M]} |
| 3823 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3824 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3825 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3826 | end_ch = '}'; |
| 3827 | goto again; |
| 3828 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3829 | /* got '}' */ |
| 3830 | if (end_ch == '}' * 0x100 + ':') { |
| 3831 | /* it's ${var:N} - emulate :999999999 */ |
| 3832 | o_addstr(dest, "999999999"); |
| 3833 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3834 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3835 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3836 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3837 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3838 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3839 | break; |
| 3840 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3841 | #if ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3842 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3843 | unsigned pos; |
| 3844 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3845 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3846 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 3847 | # if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3848 | if (i_peek(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3849 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3850 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3851 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3852 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3853 | if (!BB_MMU) |
| 3854 | pos = dest->length; |
| 3855 | add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3856 | if (as_string) { |
| 3857 | o_addstr(as_string, dest->data + pos); |
| 3858 | o_addchr(as_string, ')'); |
| 3859 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 3860 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3861 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3862 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3863 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 3864 | # endif |
| 3865 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3866 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3867 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3868 | if (!BB_MMU) |
| 3869 | pos = dest->length; |
| 3870 | add_till_closing_bracket(dest, input, ')'); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3871 | if (as_string) { |
| 3872 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 3873 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 3874 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3875 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 3876 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3877 | break; |
| 3878 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 3879 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3880 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3881 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3882 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3883 | ch = i_peek(input); |
| 3884 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 3885 | ch = '_'; |
| 3886 | goto make_var; |
| 3887 | } |
| 3888 | /* else: it's $_ */ |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 3889 | /* TODO: $_ and $-: */ |
| 3890 | /* $_ Shell or shell script name; or last argument of last command |
| 3891 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 3892 | * 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] | 3893 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 3894 | default: |
| 3895 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3896 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3897 | debug_printf_parse("parse_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3898 | return 0; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3899 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3902 | #if BB_MMU |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3903 | # if ENABLE_HUSH_BASH_COMPAT |
| 3904 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 3905 | encode_string(dest, input, dquote_end, process_bkslash) |
| 3906 | # else |
| 3907 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 3908 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 3909 | encode_string(dest, input, dquote_end) |
| 3910 | # endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3911 | #define as_string NULL |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3912 | |
| 3913 | #else /* !MMU */ |
| 3914 | |
| 3915 | # if ENABLE_HUSH_BASH_COMPAT |
| 3916 | /* all parameters are needed, no macro tricks */ |
| 3917 | # else |
| 3918 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 3919 | encode_string(as_string, dest, input, dquote_end) |
| 3920 | # endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3921 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3922 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3923 | o_string *dest, |
| 3924 | struct in_str *input, |
Denys Vlasenko | 14e289b | 2010-09-10 10:15:18 +0200 | [diff] [blame] | 3925 | int dquote_end, |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3926 | int process_bkslash) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3927 | { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3928 | #if !ENABLE_HUSH_BASH_COMPAT |
| 3929 | const int process_bkslash = 1; |
| 3930 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 3931 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3932 | int next; |
| 3933 | |
| 3934 | again: |
| 3935 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3936 | if (ch != EOF) |
| 3937 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3938 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3939 | debug_printf_parse("encode_string return 0\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3940 | return 0; |
| 3941 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3942 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3943 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3944 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3945 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3946 | } |
| 3947 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3948 | if (ch != '\n') { |
| 3949 | next = i_peek(input); |
| 3950 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 3951 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3952 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3953 | if (process_bkslash && ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3954 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3955 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3956 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3957 | } |
| 3958 | /* bash: |
| 3959 | * "The backslash retains its special meaning [in "..."] |
| 3960 | * only when followed by one of the following characters: |
| 3961 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3962 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3963 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 3964 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3965 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3966 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 3967 | ch = i_getch(input); /* eat next */ |
| 3968 | if (ch == '\n') |
| 3969 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 3970 | } /* else: ch remains == '\\', and we double it below: */ |
| 3971 | 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] | 3972 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3973 | goto again; |
| 3974 | } |
| 3975 | if (ch == '$') { |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3976 | if (parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80) != 0) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3977 | debug_printf_parse("encode_string return 1: " |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3978 | "parse_dollar returned non-0\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3979 | return 1; |
| 3980 | } |
| 3981 | goto again; |
| 3982 | } |
| 3983 | #if ENABLE_HUSH_TICK |
| 3984 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3985 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3986 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3987 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3988 | add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"'); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3989 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3990 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 3991 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3992 | } |
| 3993 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 3994 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3995 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3996 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3997 | } |
| 3998 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3999 | /* |
| 4000 | * Scan input until EOF or end_trigger char. |
| 4001 | * Return a list of pipes to execute, or NULL on EOF |
| 4002 | * or if end_trigger character is met. |
| 4003 | * On syntax error, exit is shell is not interactive, |
| 4004 | * reset parsing machinery and start parsing anew, |
| 4005 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4006 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4007 | static struct pipe *parse_stream(char **pstring, |
| 4008 | struct in_str *input, |
| 4009 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4010 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4011 | struct parse_context ctx; |
| 4012 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4013 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4014 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4015 | /* 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] | 4016 | * found. When recursing, quote state is passed in via dest->o_expflags. |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4017 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4018 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 4019 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4020 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4021 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4022 | /* If very first arg is "" or '', dest.data may end up NULL. |
| 4023 | * Preventing this: */ |
| 4024 | o_addchr(&dest, '\0'); |
| 4025 | dest.length = 0; |
| 4026 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4027 | /* We used to separate words on $IFS here. This was wrong. |
| 4028 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4029 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4030 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4031 | |
| 4032 | reset: /* we come back here only on syntax errors in interactive shell */ |
| 4033 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4034 | #if ENABLE_HUSH_INTERACTIVE |
| 4035 | input->promptmode = 0; /* PS1 */ |
| 4036 | #endif |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4037 | if (MAYBE_ASSIGNMENT != 0) |
| 4038 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4039 | initialize_context(&ctx); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4040 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4041 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4042 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4043 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4044 | int ch; |
| 4045 | int next; |
| 4046 | int redir_fd; |
| 4047 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4048 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4049 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4050 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4051 | ch, ch, !!(dest.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4052 | if (ch == EOF) { |
| 4053 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4054 | |
| 4055 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4056 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4057 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4058 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4059 | /* end_trigger == '}' case errors out earlier, |
| 4060 | * checking only ')' */ |
| 4061 | if (end_trigger == ')') { |
| 4062 | syntax_error_unterm_ch('('); /* exits */ |
| 4063 | /* goto parse_error; */ |
| 4064 | } |
| 4065 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4066 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4067 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4068 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4069 | o_free(&dest); |
| 4070 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4071 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4072 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4073 | /* (this makes bare "&" cmd a no-op. |
| 4074 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4075 | if (pi->num_cmds == 0 |
| 4076 | IF_HAS_KEYWORDS( && pi->res_word == RES_NONE) |
| 4077 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4078 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4079 | pi = NULL; |
| 4080 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4081 | #if !BB_MMU |
| 4082 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 4083 | if (pstring) |
| 4084 | *pstring = ctx.as_string.data; |
| 4085 | else |
| 4086 | o_free_unsafe(&ctx.as_string); |
| 4087 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4088 | debug_leave(); |
| 4089 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4090 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4091 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4092 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4093 | |
| 4094 | next = '\0'; |
| 4095 | if (ch != '\n') |
| 4096 | next = i_peek(input); |
| 4097 | |
| 4098 | is_special = "{}<>;&|()#'" /* special outside of "str" */ |
| 4099 | "\\$\"" IF_HUSH_TICK("`"); /* always special */ |
| 4100 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 4101 | if (ctx.command->argv /* word [word]{... - non-special */ |
| 4102 | || dest.length /* word{... - non-special */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4103 | || dest.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4104 | || (next != ';' /* }; - special */ |
| 4105 | && next != ')' /* }) - special */ |
| 4106 | && next != '&' /* }& and }&& ... - special */ |
| 4107 | && next != '|' /* }|| ... - special */ |
| 4108 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 4109 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4110 | ) { |
| 4111 | /* They are not special, skip "{}" */ |
| 4112 | is_special += 2; |
| 4113 | } |
| 4114 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4115 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4116 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4117 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 4118 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4119 | o_addQchr(&dest, ch); |
| 4120 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 4121 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4122 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4123 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4124 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4125 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4126 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4127 | continue; |
| 4128 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4129 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4130 | if (is_blank) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4131 | if (done_word(&dest, &ctx)) { |
| 4132 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 4133 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4134 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4135 | /* Is this a case when newline is simply ignored? |
| 4136 | * Some examples: |
| 4137 | * "cmd | <newline> cmd ..." |
| 4138 | * "case ... in <newline> word) ..." |
| 4139 | */ |
| 4140 | if (IS_NULL_CMD(ctx.command) |
| 4141 | && dest.length == 0 && !dest.has_quoted_part |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4142 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4143 | /* This newline can be ignored. But... |
| 4144 | * without the below check, interactive shell |
| 4145 | * will ignore even lines with bare <newline>, |
| 4146 | * and show the continuation prompt: |
| 4147 | * ps1_prompt$ <enter> |
| 4148 | * ps2> _ <=== wrong prompt, should be ps1 |
| 4149 | */ |
| 4150 | struct pipe *pi = ctx.list_head; |
| 4151 | if (pi->num_cmds != 0) |
| 4152 | continue; |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4153 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4154 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4155 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4156 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 4157 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4158 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4159 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4160 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4161 | heredoc_cnt = 0; |
| 4162 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4163 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4164 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4165 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4166 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4167 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4168 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 4169 | |
| 4170 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 4171 | * } is an ordinary char in this case, even inside { cmd; } |
| 4172 | * Pathological example: { ""}; } should exec "}" cmd |
| 4173 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4174 | if (ch == '}') { |
| 4175 | if (!IS_NULL_CMD(ctx.command) /* cmd } */ |
| 4176 | || dest.length != 0 /* word} */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4177 | || dest.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4178 | ) { |
| 4179 | goto ordinary_char; |
| 4180 | } |
| 4181 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
| 4182 | goto skip_end_trigger; |
| 4183 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 4184 | } |
| 4185 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4186 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4187 | && (ch != ';' || heredoc_cnt == 0) |
| 4188 | #if ENABLE_HUSH_CASE |
| 4189 | && (ch != ')' |
| 4190 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4191 | || (!dest.has_quoted_part && strcmp(dest.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4192 | ) |
| 4193 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4194 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4195 | if (heredoc_cnt) { |
| 4196 | /* This is technically valid: |
| 4197 | * { cat <<HERE; }; echo Ok |
| 4198 | * heredoc |
| 4199 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4200 | * HERE |
| 4201 | * but we don't support this. |
| 4202 | * We require heredoc to be in enclosing {}/(), |
| 4203 | * if any. |
| 4204 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4205 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4206 | goto parse_error; |
| 4207 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4208 | if (done_word(&dest, &ctx)) { |
| 4209 | goto parse_error; |
| 4210 | } |
| 4211 | done_pipe(&ctx, PIPE_SEQ); |
| 4212 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4213 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4214 | if (!HAS_KEYWORDS |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4215 | 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] | 4216 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4217 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4218 | #if !BB_MMU |
| 4219 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 4220 | if (pstring) |
| 4221 | *pstring = ctx.as_string.data; |
| 4222 | else |
| 4223 | o_free_unsafe(&ctx.as_string); |
| 4224 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4225 | debug_leave(); |
| 4226 | debug_printf_parse("parse_stream return %p: " |
| 4227 | "end_trigger char found\n", |
| 4228 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4229 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4230 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4231 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4232 | skip_end_trigger: |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4233 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4234 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4235 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4236 | /* Catch <, > before deciding whether this word is |
| 4237 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 4238 | switch (ch) { |
| 4239 | case '>': |
| 4240 | redir_fd = redirect_opt_num(&dest); |
| 4241 | if (done_word(&dest, &ctx)) { |
| 4242 | goto parse_error; |
| 4243 | } |
| 4244 | redir_style = REDIRECT_OVERWRITE; |
| 4245 | if (next == '>') { |
| 4246 | redir_style = REDIRECT_APPEND; |
| 4247 | ch = i_getch(input); |
| 4248 | nommu_addchr(&ctx.as_string, ch); |
| 4249 | } |
| 4250 | #if 0 |
| 4251 | else if (next == '(') { |
| 4252 | syntax_error(">(process) not supported"); |
| 4253 | goto parse_error; |
| 4254 | } |
| 4255 | #endif |
| 4256 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 4257 | goto parse_error; |
| 4258 | continue; /* back to top of while (1) */ |
| 4259 | case '<': |
| 4260 | redir_fd = redirect_opt_num(&dest); |
| 4261 | if (done_word(&dest, &ctx)) { |
| 4262 | goto parse_error; |
| 4263 | } |
| 4264 | redir_style = REDIRECT_INPUT; |
| 4265 | if (next == '<') { |
| 4266 | redir_style = REDIRECT_HEREDOC; |
| 4267 | heredoc_cnt++; |
| 4268 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 4269 | ch = i_getch(input); |
| 4270 | nommu_addchr(&ctx.as_string, ch); |
| 4271 | } else if (next == '>') { |
| 4272 | redir_style = REDIRECT_IO; |
| 4273 | ch = i_getch(input); |
| 4274 | nommu_addchr(&ctx.as_string, ch); |
| 4275 | } |
| 4276 | #if 0 |
| 4277 | else if (next == '(') { |
| 4278 | syntax_error("<(process) not supported"); |
| 4279 | goto parse_error; |
| 4280 | } |
| 4281 | #endif |
| 4282 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 4283 | goto parse_error; |
| 4284 | continue; /* back to top of while (1) */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4285 | case '#': |
| 4286 | if (dest.length == 0 && !dest.has_quoted_part) { |
| 4287 | /* skip "#comment" */ |
| 4288 | while (1) { |
| 4289 | ch = i_peek(input); |
| 4290 | if (ch == EOF || ch == '\n') |
| 4291 | break; |
| 4292 | i_getch(input); |
| 4293 | /* note: we do not add it to &ctx.as_string */ |
| 4294 | } |
| 4295 | nommu_addchr(&ctx.as_string, '\n'); |
| 4296 | continue; /* back to top of while (1) */ |
| 4297 | } |
| 4298 | break; |
| 4299 | case '\\': |
| 4300 | if (next == '\n') { |
| 4301 | /* It's "\<newline>" */ |
| 4302 | #if !BB_MMU |
| 4303 | /* Remove trailing '\' from ctx.as_string */ |
| 4304 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
| 4305 | #endif |
| 4306 | ch = i_getch(input); /* eat it */ |
| 4307 | continue; /* back to top of while (1) */ |
| 4308 | } |
| 4309 | break; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
| 4312 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 4313 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 4314 | && !ctx.pending_redirect |
| 4315 | ) { |
| 4316 | /* ch is a special char and thus this word |
| 4317 | * cannot be an assignment */ |
| 4318 | dest.o_assignment = NOT_ASSIGNMENT; |
| 4319 | } |
| 4320 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 4321 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 4322 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4323 | switch (ch) { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4324 | case '#': /* non-comment #: "echo a#b" etc */ |
| 4325 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4326 | break; |
| 4327 | case '\\': |
| 4328 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4329 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4330 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4331 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4332 | ch = i_getch(input); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4333 | /* note: ch != '\n' (that case does not reach this place) */ |
| 4334 | o_addchr(&dest, '\\'); |
| 4335 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
| 4336 | o_addchr(&dest, ch); |
| 4337 | nommu_addchr(&ctx.as_string, ch); |
| 4338 | /* Example: echo Hello \2>file |
| 4339 | * we need to know that word 2 is quoted */ |
| 4340 | dest.has_quoted_part = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4341 | break; |
| 4342 | case '$': |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4343 | if (parse_dollar(&ctx.as_string, &dest, input, /*quote_mask:*/ 0) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4344 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4345 | "parse_dollar returned non-0\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4346 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4347 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4348 | break; |
| 4349 | case '\'': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4350 | dest.has_quoted_part = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4351 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4352 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4353 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4354 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4355 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4356 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4357 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4358 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4359 | break; |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4360 | o_addqchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4361 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4362 | break; |
| 4363 | case '"': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4364 | dest.has_quoted_part = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4365 | if (dest.o_assignment == NOT_ASSIGNMENT) |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4366 | dest.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4367 | if (encode_string(&ctx.as_string, &dest, input, '"', /*process_bkslash:*/ 1)) |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4368 | goto parse_error; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4369 | dest.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4370 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4371 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4372 | case '`': { |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4373 | unsigned pos; |
| 4374 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4375 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4376 | o_addchr(&dest, '`'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4377 | pos = dest.length; |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4378 | add_till_backquote(&dest, input, /*in_dquote:*/ 0); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4379 | # if !BB_MMU |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4380 | o_addstr(&ctx.as_string, dest.data + pos); |
| 4381 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4382 | # endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4383 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4384 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4385 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4386 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4387 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4388 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4389 | #if ENABLE_HUSH_CASE |
| 4390 | case_semi: |
| 4391 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4392 | if (done_word(&dest, &ctx)) { |
| 4393 | goto parse_error; |
| 4394 | } |
| 4395 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4396 | #if ENABLE_HUSH_CASE |
| 4397 | /* Eat multiple semicolons, detect |
| 4398 | * whether it means something special */ |
| 4399 | while (1) { |
| 4400 | ch = i_peek(input); |
| 4401 | if (ch != ';') |
| 4402 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4403 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4404 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4405 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4406 | ctx.ctx_dsemicolon = 1; |
| 4407 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4408 | break; |
| 4409 | } |
| 4410 | } |
| 4411 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4412 | new_cmd: |
| 4413 | /* We just finished a cmd. New one may start |
| 4414 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4415 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4416 | break; |
| 4417 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4418 | if (done_word(&dest, &ctx)) { |
| 4419 | goto parse_error; |
| 4420 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4421 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4422 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4423 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4424 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4425 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4426 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4427 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4428 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4429 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4430 | if (done_word(&dest, &ctx)) { |
| 4431 | goto parse_error; |
| 4432 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4433 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4434 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4435 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4436 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4437 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4438 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4439 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4440 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4441 | } else { |
| 4442 | /* we could pick up a file descriptor choice here |
| 4443 | * with redirect_opt_num(), but bash doesn't do it. |
| 4444 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4445 | done_command(&ctx); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4446 | #if !BB_MMU |
| 4447 | o_reset_to_empty_unquoted(&ctx.as_string); |
| 4448 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4449 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4450 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4451 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4452 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4453 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4454 | if (ctx.ctx_res_w == RES_MATCH |
| 4455 | && ctx.command->argv == NULL /* not (word|(... */ |
| 4456 | && dest.length == 0 /* not word(... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4457 | && dest.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4458 | ) { |
| 4459 | continue; |
| 4460 | } |
| 4461 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4462 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4463 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 4464 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4465 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4466 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4467 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4468 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4469 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4470 | goto case_semi; |
| 4471 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4472 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4473 | /* proper use of this character is caught by end_trigger: |
| 4474 | * if we see {, we call parse_group(..., end_trigger='}') |
| 4475 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4476 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4477 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4478 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4479 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4480 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4481 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4482 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4483 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4484 | parse_error: |
| 4485 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4486 | struct parse_context *pctx; |
| 4487 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4488 | |
| 4489 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 4490 | * Sample for finding leaks on syntax error recovery path. |
| 4491 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4492 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 4493 | * Samples to catch leaks at execution: |
| 4494 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 4495 | * 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] | 4496 | */ |
| 4497 | pctx = &ctx; |
| 4498 | do { |
| 4499 | /* Update pipe/command counts, |
| 4500 | * otherwise freeing may miss some */ |
| 4501 | done_pipe(pctx, PIPE_SEQ); |
| 4502 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 4503 | pctx->list_head, pctx); |
| 4504 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4505 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4506 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4507 | #if !BB_MMU |
| 4508 | o_free_unsafe(&pctx->as_string); |
| 4509 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4510 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4511 | if (pctx != &ctx) { |
| 4512 | free(pctx); |
| 4513 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4514 | IF_HAS_KEYWORDS(pctx = p2;) |
| 4515 | } while (HAS_KEYWORDS && pctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4516 | /* Free text, clear all dest fields */ |
| 4517 | o_free(&dest); |
| 4518 | /* If we are not in top-level parse, we return, |
| 4519 | * our caller will propagate error. |
| 4520 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4521 | if (end_trigger != ';') { |
| 4522 | #if !BB_MMU |
| 4523 | if (pstring) |
| 4524 | *pstring = NULL; |
| 4525 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4526 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4527 | return ERR_PTR; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4528 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4529 | /* Discard cached input, force prompt */ |
| 4530 | input->p = NULL; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4531 | IF_HUSH_INTERACTIVE(input->promptme = 1;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4532 | goto reset; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4533 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4536 | |
| 4537 | /*** Execution routines ***/ |
| 4538 | |
| 4539 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4540 | #if !ENABLE_HUSH_BASH_COMPAT |
| 4541 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4542 | #define expand_string_to_string(str, do_unbackslash) \ |
| 4543 | expand_string_to_string(str) |
| 4544 | #endif |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 4545 | static char *expand_string_to_string(const char *str, int do_unbackslash); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 4546 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4547 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 4548 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4549 | |
| 4550 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 4551 | * all variable references within and returns a pointer to |
| 4552 | * a list of expanded strings, possibly with larger number |
| 4553 | * of strings. (Think VAR="a b"; echo $VAR). |
| 4554 | * This new list is allocated as a single malloc block. |
| 4555 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4556 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4557 | * Caller can deallocate entire list by single free(list). */ |
| 4558 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 4559 | /* A horde of its helpers come first: */ |
| 4560 | |
| 4561 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 4562 | { |
| 4563 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 4564 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 4565 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 4566 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 4567 | if (c == '{' || c == '}') { |
| 4568 | /* { -> \{, } -> \} */ |
| 4569 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 4570 | /* And now we want to add { or } and continue: |
| 4571 | * o_addchr(o, c); |
| 4572 | * continue; |
| 4573 | * luckily, just falling throught achieves this. |
| 4574 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 4575 | } |
| 4576 | #endif |
| 4577 | o_addchr(o, c); |
| 4578 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 4579 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 4580 | o_addchr(o, '\\'); |
| 4581 | if (len) { |
| 4582 | len--; |
| 4583 | o_addchr(o, '\\'); |
| 4584 | o_addchr(o, *str++); |
| 4585 | } |
| 4586 | } |
| 4587 | } |
| 4588 | } |
| 4589 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4590 | /* Store given string, finalizing the word and starting new one whenever |
| 4591 | * we encounter IFS char(s). This is used for expanding variable values. |
| 4592 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 4593 | static int expand_on_ifs(o_string *output, int n, const char *str) |
| 4594 | { |
| 4595 | while (1) { |
| 4596 | int word_len = strcspn(str, G.ifs); |
| 4597 | if (word_len) { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 4598 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4599 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 4600 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4601 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 4602 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 4603 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4604 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4605 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4606 | /*/ Why can't we do it easier? */ |
| 4607 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 4608 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 4609 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4610 | str += word_len; |
| 4611 | } |
| 4612 | if (!*str) /* EOL - do not finalize word */ |
| 4613 | break; |
| 4614 | o_addchr(output, '\0'); |
| 4615 | debug_print_list("expand_on_ifs", output, n); |
| 4616 | n = o_save_ptr(output, n); |
| 4617 | str += strspn(str, G.ifs); /* skip ifs chars */ |
| 4618 | } |
| 4619 | debug_print_list("expand_on_ifs[1]", output, n); |
| 4620 | return n; |
| 4621 | } |
| 4622 | |
| 4623 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 4624 | * they are in double quotes, with the exception that they are not :). |
| 4625 | * Just the rules are similar: "expand only $var and `cmd`" |
| 4626 | * |
| 4627 | * Returns malloced string. |
| 4628 | * As an optimization, we return NULL if expansion is not needed. |
| 4629 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4630 | #if !ENABLE_HUSH_BASH_COMPAT |
| 4631 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4632 | #define encode_then_expand_string(str, process_bkslash, do_unbackslash) \ |
| 4633 | encode_then_expand_string(str) |
| 4634 | #endif |
| 4635 | 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] | 4636 | { |
| 4637 | char *exp_str; |
| 4638 | struct in_str input; |
| 4639 | o_string dest = NULL_O_STRING; |
| 4640 | |
| 4641 | if (!strchr(str, '$') |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4642 | && !strchr(str, '\\') |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4643 | #if ENABLE_HUSH_TICK |
| 4644 | && !strchr(str, '`') |
| 4645 | #endif |
| 4646 | ) { |
| 4647 | return NULL; |
| 4648 | } |
| 4649 | |
| 4650 | /* We need to expand. Example: |
| 4651 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 4652 | */ |
| 4653 | setup_string_in_str(&input, str); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4654 | encode_string(NULL, &dest, &input, EOF, process_bkslash); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4655 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 4656 | exp_str = expand_string_to_string(dest.data, /*unbackslash:*/ do_unbackslash); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4657 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 4658 | o_free_unsafe(&dest); |
| 4659 | return exp_str; |
| 4660 | } |
| 4661 | |
| 4662 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4663 | 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] | 4664 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 4665 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4666 | arith_t res; |
| 4667 | char *exp_str; |
| 4668 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 4669 | math_state.lookupvar = get_local_var_value; |
| 4670 | math_state.setvar = set_local_var_from_halves; |
| 4671 | //math_state.endofname = endofname; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4672 | exp_str = encode_then_expand_string(arg, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 4673 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4674 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4675 | if (errmsg_p) |
| 4676 | *errmsg_p = math_state.errmsg; |
| 4677 | if (math_state.errmsg) |
| 4678 | die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4679 | return res; |
| 4680 | } |
| 4681 | #endif |
| 4682 | |
| 4683 | #if ENABLE_HUSH_BASH_COMPAT |
| 4684 | /* ${var/[/]pattern[/repl]} helpers */ |
| 4685 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 4686 | { |
| 4687 | while (1) { |
| 4688 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 4689 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 4690 | if (end) { |
| 4691 | *size = end - val; |
| 4692 | return val; |
| 4693 | } |
| 4694 | if (*val == '\0') |
| 4695 | return NULL; |
| 4696 | /* Optimization: if "*pat" did not match the start of "string", |
| 4697 | * we know that "tring", "ring" etc will not match too: |
| 4698 | */ |
| 4699 | if (pattern[0] == '*') |
| 4700 | return NULL; |
| 4701 | val++; |
| 4702 | } |
| 4703 | } |
| 4704 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 4705 | { |
| 4706 | char *result = NULL; |
| 4707 | unsigned res_len = 0; |
| 4708 | unsigned repl_len = strlen(repl); |
| 4709 | |
| 4710 | while (1) { |
| 4711 | int size; |
| 4712 | char *s = strstr_pattern(val, pattern, &size); |
| 4713 | if (!s) |
| 4714 | break; |
| 4715 | |
| 4716 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
| 4717 | memcpy(result + res_len, val, s - val); |
| 4718 | res_len += s - val; |
| 4719 | strcpy(result + res_len, repl); |
| 4720 | res_len += repl_len; |
| 4721 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 4722 | |
| 4723 | val = s + size; |
| 4724 | if (exp_op == '/') |
| 4725 | break; |
| 4726 | } |
| 4727 | if (val[0] && result) { |
| 4728 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 4729 | strcpy(result + res_len, val); |
| 4730 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 4731 | } |
| 4732 | debug_printf_varexp("result:'%s'\n", result); |
| 4733 | return result; |
| 4734 | } |
| 4735 | #endif |
| 4736 | |
| 4737 | /* Helper: |
| 4738 | * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
| 4739 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 4740 | 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] | 4741 | { |
| 4742 | const char *val = NULL; |
| 4743 | char *to_be_freed = NULL; |
| 4744 | char *p = *pp; |
| 4745 | char *var; |
| 4746 | char first_char; |
| 4747 | char exp_op; |
| 4748 | char exp_save = exp_save; /* for compiler */ |
| 4749 | char *exp_saveptr; /* points to expansion operator */ |
| 4750 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 4751 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4752 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4753 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4754 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4755 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 4756 | arg0 = arg[0]; |
| 4757 | first_char = arg[0] = arg0 & 0x7f; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4758 | exp_op = 0; |
| 4759 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4760 | if (first_char == '#' /* ${#... */ |
| 4761 | && arg[1] && !exp_saveptr /* not ${#} and not ${#<op_char>...} */ |
| 4762 | ) { |
| 4763 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4764 | var++; |
| 4765 | exp_op = 'L'; |
| 4766 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4767 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4768 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
| 4769 | && strchr(NUMERIC_SPECVARS_STR, first_char) /* 1st char is special variable */ |
| 4770 | ) { |
| 4771 | /* ${?:0}, ${#[:]%0} etc */ |
| 4772 | exp_saveptr = var + 1; |
| 4773 | } else { |
| 4774 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 4775 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 4776 | } |
| 4777 | exp_op = exp_save = *exp_saveptr; |
| 4778 | if (exp_op) { |
| 4779 | exp_word = exp_saveptr + 1; |
| 4780 | if (exp_op == ':') { |
| 4781 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4782 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4783 | if (ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4784 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4785 | ) { |
| 4786 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 4787 | exp_op = ':'; |
| 4788 | exp_word--; |
| 4789 | } |
| 4790 | } |
| 4791 | *exp_saveptr = '\0'; |
| 4792 | } /* else: it's not an expansion op, but bare ${var} */ |
| 4793 | } |
| 4794 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4795 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4796 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4797 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4798 | int n = xatoi_positive(var); |
| 4799 | if (n < G.global_argc) |
| 4800 | val = G.global_argv[n]; |
| 4801 | /* else val remains NULL: $N with too big N */ |
| 4802 | } else { |
| 4803 | switch (var[0]) { |
| 4804 | case '$': /* pid */ |
| 4805 | val = utoa(G.root_pid); |
| 4806 | break; |
| 4807 | case '!': /* bg pid */ |
| 4808 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 4809 | break; |
| 4810 | case '?': /* exitcode */ |
| 4811 | val = utoa(G.last_exitcode); |
| 4812 | break; |
| 4813 | case '#': /* argc */ |
| 4814 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 4815 | break; |
| 4816 | default: |
| 4817 | val = get_local_var_value(var); |
| 4818 | } |
| 4819 | } |
| 4820 | |
| 4821 | /* Handle any expansions */ |
| 4822 | if (exp_op == 'L') { |
| 4823 | debug_printf_expand("expand: length(%s)=", val); |
| 4824 | val = utoa(val ? strlen(val) : 0); |
| 4825 | debug_printf_expand("%s\n", val); |
| 4826 | } else if (exp_op) { |
| 4827 | if (exp_op == '%' || exp_op == '#') { |
| 4828 | /* Standard-mandated substring removal ops: |
| 4829 | * ${parameter%word} - remove smallest suffix pattern |
| 4830 | * ${parameter%%word} - remove largest suffix pattern |
| 4831 | * ${parameter#word} - remove smallest prefix pattern |
| 4832 | * ${parameter##word} - remove largest prefix pattern |
| 4833 | * |
| 4834 | * Word is expanded to produce a glob pattern. |
| 4835 | * Then var's value is matched to it and matching part removed. |
| 4836 | */ |
| 4837 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4838 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4839 | char *exp_exp_word; |
| 4840 | char *loc; |
| 4841 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 4842 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4843 | exp_word++; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4844 | 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] | 4845 | if (exp_exp_word) |
| 4846 | exp_word = exp_exp_word; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4847 | /* HACK ALERT. We depend here on the fact that |
| 4848 | * G.global_argv and results of utoa and get_local_var_value |
| 4849 | * are actually in writable memory: |
| 4850 | * scan_and_match momentarily stores NULs there. */ |
| 4851 | t = (char*)val; |
| 4852 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4853 | //bb_error_msg("op:%c str:'%s' pat:'%s' res:'%s'", |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4854 | // exp_op, t, exp_word, loc); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4855 | free(exp_exp_word); |
| 4856 | if (loc) { /* match was found */ |
| 4857 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4858 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4859 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4860 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4861 | } |
| 4862 | } |
| 4863 | } |
| 4864 | #if ENABLE_HUSH_BASH_COMPAT |
| 4865 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4866 | /* It's ${var/[/]pattern[/repl]} thing. |
| 4867 | * Note that in encoded form it has TWO parts: |
| 4868 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4869 | * and if // is used, it is encoded as \: |
| 4870 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4871 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4872 | /* Empty variable always gives nothing: */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4873 | // "v=''; echo ${v/*/w}" prints "", not "w" |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4874 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4875 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4876 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4877 | * by the usual expansion rules: |
| 4878 | * >az; >bz; |
| 4879 | * v='a bz'; echo "${v/a*z/a*z}" prints "a*z" |
| 4880 | * v='a bz'; echo "${v/a*z/\z}" prints "\z" |
| 4881 | * v='a bz'; echo ${v/a*z/a*z} prints "az" |
| 4882 | * v='a bz'; echo ${v/a*z/\z} prints "z" |
| 4883 | * (note that a*z _pattern_ is never globbed!) |
| 4884 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4885 | char *pattern, *repl, *t; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4886 | pattern = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4887 | if (!pattern) |
| 4888 | pattern = xstrdup(exp_word); |
| 4889 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 4890 | *p++ = SPECIAL_VAR_SYMBOL; |
| 4891 | exp_word = p; |
| 4892 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 4893 | *p = '\0'; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4894 | 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] | 4895 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 4896 | /* HACK ALERT. We depend here on the fact that |
| 4897 | * G.global_argv and results of utoa and get_local_var_value |
| 4898 | * are actually in writable memory: |
| 4899 | * replace_pattern momentarily stores NULs there. */ |
| 4900 | t = (char*)val; |
| 4901 | to_be_freed = replace_pattern(t, |
| 4902 | pattern, |
| 4903 | (repl ? repl : exp_word), |
| 4904 | exp_op); |
| 4905 | if (to_be_freed) /* at least one replace happened */ |
| 4906 | val = to_be_freed; |
| 4907 | free(pattern); |
| 4908 | free(repl); |
| 4909 | } |
| 4910 | } |
| 4911 | #endif |
| 4912 | else if (exp_op == ':') { |
| 4913 | #if ENABLE_HUSH_BASH_COMPAT && ENABLE_SH_MATH_SUPPORT |
| 4914 | /* It's ${var:N[:M]} bashism. |
| 4915 | * Note that in encoded form it has TWO parts: |
| 4916 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 4917 | */ |
| 4918 | arith_t beg, len; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4919 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4920 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4921 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 4922 | if (errmsg) |
| 4923 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4924 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 4925 | *p++ = SPECIAL_VAR_SYMBOL; |
| 4926 | exp_word = p; |
| 4927 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 4928 | *p = '\0'; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4929 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 4930 | if (errmsg) |
| 4931 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4932 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4933 | if (len >= 0) { /* bash compat: len < 0 is illegal */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4934 | if (beg < 0) /* bash compat */ |
| 4935 | beg = 0; |
| 4936 | debug_printf_varexp("from val:'%s'\n", val); |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 4937 | if (len == 0 || !val || beg >= strlen(val)) { |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4938 | arith_err: |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 4939 | val = NULL; |
| 4940 | } else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4941 | /* Paranoia. What if user entered 9999999999999 |
| 4942 | * which fits in arith_t but not int? */ |
| 4943 | if (len >= INT_MAX) |
| 4944 | len = INT_MAX; |
| 4945 | val = to_be_freed = xstrndup(val + beg, len); |
| 4946 | } |
| 4947 | debug_printf_varexp("val:'%s'\n", val); |
| 4948 | } else |
| 4949 | #endif |
| 4950 | { |
| 4951 | die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 4952 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4953 | } |
| 4954 | } else { /* one of "-=+?" */ |
| 4955 | /* Standard-mandated substitution ops: |
| 4956 | * ${var?word} - indicate error if unset |
| 4957 | * If var is unset, word (or a message indicating it is unset |
| 4958 | * if word is null) is written to standard error |
| 4959 | * and the shell exits with a non-zero exit status. |
| 4960 | * Otherwise, the value of var is substituted. |
| 4961 | * ${var-word} - use default value |
| 4962 | * If var is unset, word is substituted. |
| 4963 | * ${var=word} - assign and use default value |
| 4964 | * If var is unset, word is assigned to var. |
| 4965 | * In all cases, final value of var is substituted. |
| 4966 | * ${var+word} - use alternative value |
| 4967 | * If var is unset, null is substituted. |
| 4968 | * Otherwise, word is substituted. |
| 4969 | * |
| 4970 | * Word is subjected to tilde expansion, parameter expansion, |
| 4971 | * command substitution, and arithmetic expansion. |
| 4972 | * If word is not needed, it is not expanded. |
| 4973 | * |
| 4974 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 4975 | * but also treat null var as if it is unset. |
| 4976 | */ |
| 4977 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 4978 | if (exp_op == '+') |
| 4979 | use_word = !use_word; |
| 4980 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 4981 | (exp_save == ':') ? "true" : "false", use_word); |
| 4982 | if (use_word) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4983 | 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] | 4984 | if (to_be_freed) |
| 4985 | exp_word = to_be_freed; |
| 4986 | if (exp_op == '?') { |
| 4987 | /* mimic bash message */ |
| 4988 | die_if_script("%s: %s", |
| 4989 | var, |
| 4990 | exp_word[0] ? exp_word : "parameter null or not set" |
| 4991 | ); |
| 4992 | //TODO: how interactive bash aborts expansion mid-command? |
| 4993 | } else { |
| 4994 | val = exp_word; |
| 4995 | } |
| 4996 | |
| 4997 | if (exp_op == '=') { |
| 4998 | /* ${var=[word]} or ${var:=[word]} */ |
| 4999 | if (isdigit(var[0]) || var[0] == '#') { |
| 5000 | /* mimic bash message */ |
| 5001 | die_if_script("$%s: cannot assign in this way", var); |
| 5002 | val = NULL; |
| 5003 | } else { |
| 5004 | char *new_var = xasprintf("%s=%s", var, val); |
| 5005 | set_local_var(new_var, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 5006 | } |
| 5007 | } |
| 5008 | } |
| 5009 | } /* one of "-=+?" */ |
| 5010 | |
| 5011 | *exp_saveptr = exp_save; |
| 5012 | } /* if (exp_op) */ |
| 5013 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5014 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5015 | |
| 5016 | *pp = p; |
| 5017 | *to_be_freed_pp = to_be_freed; |
| 5018 | return val; |
| 5019 | } |
| 5020 | |
| 5021 | /* Expand all variable references in given string, adding words to list[] |
| 5022 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 5023 | * to be filled). This routine is extremely tricky: has to deal with |
| 5024 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 5025 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5026 | 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] | 5027 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5028 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5029 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5030 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5031 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5032 | char *p; |
| 5033 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5034 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 5035 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5036 | debug_print_list("expand_vars_to_list", output, n); |
| 5037 | n = o_save_ptr(output, n); |
| 5038 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 5039 | |
| 5040 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 5041 | char first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5042 | char *to_be_freed = NULL; |
| 5043 | const char *val = NULL; |
| 5044 | #if ENABLE_HUSH_TICK |
| 5045 | o_string subst_result = NULL_O_STRING; |
| 5046 | #endif |
| 5047 | #if ENABLE_SH_MATH_SUPPORT |
| 5048 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 5049 | #endif |
| 5050 | o_addblock(output, arg, p - arg); |
| 5051 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 5052 | arg = ++p; |
| 5053 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5054 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5055 | /* Fetch special var name (if it is indeed one of them) |
| 5056 | * and quote bit, force the bit on if singleword expansion - |
| 5057 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5058 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5059 | |
| 5060 | /* Is this variable quoted and thus expansion can't be null? |
| 5061 | * "$@" is special. Even if quoted, it can still |
| 5062 | * expand to nothing (not even an empty string), |
| 5063 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5064 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5065 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5066 | |
| 5067 | switch (first_ch & 0x7f) { |
| 5068 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 5069 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5070 | case '@': { |
| 5071 | int i; |
| 5072 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5073 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5074 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5075 | 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] | 5076 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5077 | while (G.global_argv[i]) { |
| 5078 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 5079 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 5080 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 5081 | /* this argv[] is not empty and not last: |
| 5082 | * put terminating NUL, start new word */ |
| 5083 | o_addchr(output, '\0'); |
| 5084 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 5085 | n = o_save_ptr(output, n); |
| 5086 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 5087 | } |
| 5088 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5089 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5090 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5091 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5092 | if (first_ch == ('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5093 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5094 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5095 | while (1) { |
| 5096 | o_addQstr(output, G.global_argv[i]); |
| 5097 | if (++i >= G.global_argc) |
| 5098 | break; |
| 5099 | o_addchr(output, '\0'); |
| 5100 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 5101 | n = o_save_ptr(output, n); |
| 5102 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5103 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5104 | while (1) { |
| 5105 | o_addQstr(output, G.global_argv[i]); |
| 5106 | if (!G.global_argv[++i]) |
| 5107 | break; |
| 5108 | if (G.ifs[0]) |
| 5109 | o_addchr(output, G.ifs[0]); |
| 5110 | } |
| 5111 | } |
| 5112 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5113 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5114 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 5115 | /* "Empty variable", used to make "" etc to not disappear */ |
| 5116 | arg++; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5117 | cant_be_null = 0x80; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5118 | break; |
| 5119 | #if ENABLE_HUSH_TICK |
| 5120 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5121 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5122 | arg++; |
| 5123 | /* Can't just stuff it into output o_string, |
| 5124 | * expanded result may need to be globbed |
| 5125 | * and $IFS-splitted */ |
| 5126 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 5127 | G.last_exitcode = process_command_subs(&subst_result, arg); |
| 5128 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
| 5129 | val = subst_result.data; |
| 5130 | goto store_val; |
| 5131 | #endif |
| 5132 | #if ENABLE_SH_MATH_SUPPORT |
| 5133 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
| 5134 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5135 | |
| 5136 | arg++; /* skip '+' */ |
| 5137 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 5138 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5139 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 5140 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 5141 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5142 | val = arith_buf; |
| 5143 | break; |
| 5144 | } |
| 5145 | #endif |
| 5146 | default: |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5147 | val = expand_one_var(&to_be_freed, arg, &p); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5148 | IF_HUSH_TICK(store_val:) |
| 5149 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5150 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 5151 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5152 | if (val && val[0]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5153 | n = expand_on_ifs(output, n, val); |
| 5154 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5155 | } |
| 5156 | } else { /* quoted $VAR, val will be appended below */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5157 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 5158 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5159 | } |
| 5160 | break; |
| 5161 | |
| 5162 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 5163 | |
| 5164 | if (val && val[0]) { |
| 5165 | o_addQstr(output, val); |
| 5166 | } |
| 5167 | free(to_be_freed); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5168 | |
| 5169 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 5170 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5171 | if (*p != SPECIAL_VAR_SYMBOL) |
| 5172 | *p = SPECIAL_VAR_SYMBOL; |
| 5173 | |
| 5174 | #if ENABLE_HUSH_TICK |
| 5175 | o_free(&subst_result); |
| 5176 | #endif |
| 5177 | arg = ++p; |
| 5178 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 5179 | |
| 5180 | if (arg[0]) { |
| 5181 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 5182 | /* this part is literal, and it was already pre-quoted |
| 5183 | * if needed (much earlier), do not use o_addQstr here! */ |
| 5184 | o_addstr_with_NUL(output, arg); |
| 5185 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 5186 | } 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] | 5187 | && !(cant_be_null & 0x80) /* and all vars were not quoted. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5188 | ) { |
| 5189 | n--; |
| 5190 | /* allow to reuse list[n] later without re-growth */ |
| 5191 | output->has_empty_slot = 1; |
| 5192 | } else { |
| 5193 | o_addchr(output, '\0'); |
| 5194 | } |
| 5195 | |
| 5196 | return n; |
| 5197 | } |
| 5198 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5199 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5200 | { |
| 5201 | int n; |
| 5202 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5203 | o_string output = NULL_O_STRING; |
| 5204 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5205 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5206 | |
| 5207 | n = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5208 | while (*argv) { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5209 | n = expand_vars_to_list(&output, n, *argv); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5210 | argv++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5211 | } |
| 5212 | debug_print_list("expand_variables", &output, n); |
| 5213 | |
| 5214 | /* output.data (malloced in one block) gets returned in "list" */ |
| 5215 | list = o_finalize_list(&output, n); |
| 5216 | debug_print_strings("expand_variables[1]", list); |
| 5217 | return list; |
| 5218 | } |
| 5219 | |
| 5220 | static char **expand_strvec_to_strvec(char **argv) |
| 5221 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5222 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5223 | } |
| 5224 | |
| 5225 | #if ENABLE_HUSH_BASH_COMPAT |
| 5226 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 5227 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5228 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5229 | } |
| 5230 | #endif |
| 5231 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5232 | /* Used for expansion of right hand of assignments, |
| 5233 | * $((...)), heredocs, variable espansion parts. |
| 5234 | * |
| 5235 | * NB: should NOT do globbing! |
| 5236 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 5237 | */ |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5238 | static char *expand_string_to_string(const char *str, int do_unbackslash) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5239 | { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5240 | #if !ENABLE_HUSH_BASH_COMPAT |
| 5241 | const int do_unbackslash = 1; |
| 5242 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5243 | char *argv[2], **list; |
| 5244 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5245 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5246 | /* This is generally an optimization, but it also |
| 5247 | * handles "", which otherwise trips over !list[0] check below. |
| 5248 | * (is this ever happens that we actually get str="" here?) |
| 5249 | */ |
| 5250 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 5251 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5252 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5253 | return xstrdup(str); |
| 5254 | } |
| 5255 | |
| 5256 | argv[0] = (char*)str; |
| 5257 | argv[1] = NULL; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5258 | list = expand_variables(argv, do_unbackslash |
| 5259 | ? EXP_FLAG_ESC_GLOB_CHARS | EXP_FLAG_SINGLEWORD |
| 5260 | : EXP_FLAG_SINGLEWORD |
| 5261 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5262 | if (HUSH_DEBUG) |
| 5263 | if (!list[0] || list[1]) |
| 5264 | bb_error_msg_and_die("BUG in varexp2"); |
| 5265 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 5266 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5267 | if (do_unbackslash) |
| 5268 | unbackslash((char*)list); |
| 5269 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5270 | return (char*)list; |
| 5271 | } |
| 5272 | |
| 5273 | /* Used for "eval" builtin */ |
| 5274 | static char* expand_strvec_to_string(char **argv) |
| 5275 | { |
| 5276 | char **list; |
| 5277 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5278 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5279 | /* Convert all NULs to spaces */ |
| 5280 | if (list[0]) { |
| 5281 | int n = 1; |
| 5282 | while (list[n]) { |
| 5283 | if (HUSH_DEBUG) |
| 5284 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 5285 | bb_error_msg_and_die("BUG in varexp3"); |
| 5286 | /* bash uses ' ' regardless of $IFS contents */ |
| 5287 | list[n][-1] = ' '; |
| 5288 | n++; |
| 5289 | } |
| 5290 | } |
| 5291 | overlapping_strcpy((char*)list, list[0]); |
| 5292 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 5293 | return (char*)list; |
| 5294 | } |
| 5295 | |
| 5296 | static char **expand_assignments(char **argv, int count) |
| 5297 | { |
| 5298 | int i; |
| 5299 | char **p; |
| 5300 | |
| 5301 | G.expanded_assignments = p = NULL; |
| 5302 | /* Expand assignments into one string each */ |
| 5303 | for (i = 0; i < count; i++) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5304 | 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] | 5305 | } |
| 5306 | G.expanded_assignments = NULL; |
| 5307 | return p; |
| 5308 | } |
| 5309 | |
| 5310 | |
| 5311 | #if BB_MMU |
| 5312 | /* never called */ |
| 5313 | void re_execute_shell(char ***to_free, const char *s, |
| 5314 | char *g_argv0, char **g_argv, |
| 5315 | char **builtin_argv) NORETURN; |
| 5316 | |
| 5317 | static void reset_traps_to_defaults(void) |
| 5318 | { |
| 5319 | /* This function is always called in a child shell |
| 5320 | * after fork (not vfork, NOMMU doesn't use this function). |
| 5321 | */ |
| 5322 | unsigned sig; |
| 5323 | unsigned mask; |
| 5324 | |
| 5325 | /* Child shells are not interactive. |
| 5326 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 5327 | * Testcase: (while :; do :; done) + ^Z should background. |
| 5328 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 5329 | */ |
| 5330 | if (!G.traps && !(G.non_DFL_mask & SPECIAL_INTERACTIVE_SIGS)) |
| 5331 | return; /* already no traps and no SPECIAL_INTERACTIVE_SIGS */ |
| 5332 | |
| 5333 | /* Switching off SPECIAL_INTERACTIVE_SIGS. |
| 5334 | * Stupid. It can be done with *single* &= op, but we can't use |
| 5335 | * the fact that G.blocked_set is implemented as a bitmask |
| 5336 | * in libc... */ |
| 5337 | mask = (SPECIAL_INTERACTIVE_SIGS >> 1); |
| 5338 | sig = 1; |
| 5339 | while (1) { |
| 5340 | if (mask & 1) { |
| 5341 | /* Careful. Only if no trap or trap is not "" */ |
| 5342 | if (!G.traps || !G.traps[sig] || G.traps[sig][0]) |
| 5343 | sigdelset(&G.blocked_set, sig); |
| 5344 | } |
| 5345 | mask >>= 1; |
| 5346 | if (!mask) |
| 5347 | break; |
| 5348 | sig++; |
| 5349 | } |
| 5350 | /* Our homegrown sig mask is saner to work with :) */ |
| 5351 | G.non_DFL_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
| 5352 | |
| 5353 | /* Resetting all traps to default except empty ones */ |
| 5354 | mask = G.non_DFL_mask; |
| 5355 | if (G.traps) for (sig = 0; sig < NSIG; sig++, mask >>= 1) { |
| 5356 | if (!G.traps[sig] || !G.traps[sig][0]) |
| 5357 | continue; |
| 5358 | free(G.traps[sig]); |
| 5359 | G.traps[sig] = NULL; |
| 5360 | /* There is no signal for 0 (EXIT) */ |
| 5361 | if (sig == 0) |
| 5362 | continue; |
| 5363 | /* There was a trap handler, we just removed it. |
| 5364 | * But if sig still has non-DFL handling, |
| 5365 | * we should not unblock the sig. */ |
| 5366 | if (mask & 1) |
| 5367 | continue; |
| 5368 | sigdelset(&G.blocked_set, sig); |
| 5369 | } |
| 5370 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 5371 | } |
| 5372 | |
| 5373 | #else /* !BB_MMU */ |
| 5374 | |
| 5375 | static void re_execute_shell(char ***to_free, const char *s, |
| 5376 | char *g_argv0, char **g_argv, |
| 5377 | char **builtin_argv) NORETURN; |
| 5378 | static void re_execute_shell(char ***to_free, const char *s, |
| 5379 | char *g_argv0, char **g_argv, |
| 5380 | char **builtin_argv) |
| 5381 | { |
| 5382 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 5383 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 5384 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 5385 | char *heredoc_argv[4]; |
| 5386 | struct variable *cur; |
| 5387 | # if ENABLE_HUSH_FUNCTIONS |
| 5388 | struct function *funcp; |
| 5389 | # endif |
| 5390 | char **argv, **pp; |
| 5391 | unsigned cnt; |
| 5392 | unsigned long long empty_trap_mask; |
| 5393 | |
| 5394 | if (!g_argv0) { /* heredoc */ |
| 5395 | argv = heredoc_argv; |
| 5396 | argv[0] = (char *) G.argv0_for_re_execing; |
| 5397 | argv[1] = (char *) "-<"; |
| 5398 | argv[2] = (char *) s; |
| 5399 | argv[3] = NULL; |
| 5400 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 5401 | goto do_exec; |
| 5402 | } |
| 5403 | |
| 5404 | cnt = 0; |
| 5405 | pp = builtin_argv; |
| 5406 | if (pp) while (*pp++) |
| 5407 | cnt++; |
| 5408 | |
| 5409 | empty_trap_mask = 0; |
| 5410 | if (G.traps) { |
| 5411 | int sig; |
| 5412 | for (sig = 1; sig < NSIG; sig++) { |
| 5413 | if (G.traps[sig] && !G.traps[sig][0]) |
| 5414 | empty_trap_mask |= 1LL << sig; |
| 5415 | } |
| 5416 | } |
| 5417 | |
| 5418 | sprintf(param_buf, NOMMU_HACK_FMT |
| 5419 | , (unsigned) G.root_pid |
| 5420 | , (unsigned) G.root_ppid |
| 5421 | , (unsigned) G.last_bg_pid |
| 5422 | , (unsigned) G.last_exitcode |
| 5423 | , cnt |
| 5424 | , empty_trap_mask |
| 5425 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 5426 | ); |
| 5427 | # undef NOMMU_HACK_FMT |
| 5428 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 5429 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 5430 | */ |
| 5431 | cnt += 6; |
| 5432 | for (cur = G.top_var; cur; cur = cur->next) { |
| 5433 | if (!cur->flg_export || cur->flg_read_only) |
| 5434 | cnt += 2; |
| 5435 | } |
| 5436 | # if ENABLE_HUSH_FUNCTIONS |
| 5437 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 5438 | cnt += 3; |
| 5439 | # endif |
| 5440 | pp = g_argv; |
| 5441 | while (*pp++) |
| 5442 | cnt++; |
| 5443 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 5444 | *pp++ = (char *) G.argv0_for_re_execing; |
| 5445 | *pp++ = param_buf; |
| 5446 | for (cur = G.top_var; cur; cur = cur->next) { |
| 5447 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 5448 | continue; |
| 5449 | if (cur->flg_read_only) { |
| 5450 | *pp++ = (char *) "-R"; |
| 5451 | *pp++ = cur->varstr; |
| 5452 | } else if (!cur->flg_export) { |
| 5453 | *pp++ = (char *) "-V"; |
| 5454 | *pp++ = cur->varstr; |
| 5455 | } |
| 5456 | } |
| 5457 | # if ENABLE_HUSH_FUNCTIONS |
| 5458 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 5459 | *pp++ = (char *) "-F"; |
| 5460 | *pp++ = funcp->name; |
| 5461 | *pp++ = funcp->body_as_string; |
| 5462 | } |
| 5463 | # endif |
| 5464 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 5465 | * |
| 5466 | * However, POSIX says that subshells reset signals with traps |
| 5467 | * to SIG_DFL. |
| 5468 | * I tested bash-3.2 and it not only does that with true subshells |
| 5469 | * of the form ( list ), but with any forked children shells. |
| 5470 | * I set trap "echo W" WINCH; and then tried: |
| 5471 | * |
| 5472 | * { echo 1; sleep 20; echo 2; } & |
| 5473 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 5474 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 5475 | * |
| 5476 | * In all these cases sending SIGWINCH to the child shell |
| 5477 | * did not run the trap. If I add trap "echo V" WINCH; |
| 5478 | * _inside_ group (just before echo 1), it works. |
| 5479 | * |
| 5480 | * I conclude it means we don't need to pass active traps here. |
| 5481 | * Even if we would use signal handlers instead of signal masking |
| 5482 | * in order to implement trap handling, |
| 5483 | * exec syscall below resets signals to SIG_DFL for us. |
| 5484 | */ |
| 5485 | *pp++ = (char *) "-c"; |
| 5486 | *pp++ = (char *) s; |
| 5487 | if (builtin_argv) { |
| 5488 | while (*++builtin_argv) |
| 5489 | *pp++ = *builtin_argv; |
| 5490 | *pp++ = (char *) ""; |
| 5491 | } |
| 5492 | *pp++ = g_argv0; |
| 5493 | while (*g_argv) |
| 5494 | *pp++ = *g_argv++; |
| 5495 | /* *pp = NULL; - is already there */ |
| 5496 | pp = environ; |
| 5497 | |
| 5498 | do_exec: |
| 5499 | debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s); |
| 5500 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 5501 | execve(bb_busybox_exec_path, argv, pp); |
| 5502 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 5503 | if (argv[0][0] == '/') |
| 5504 | execve(argv[0], argv, pp); |
| 5505 | xfunc_error_retval = 127; |
| 5506 | bb_error_msg_and_die("can't re-execute the shell"); |
| 5507 | } |
| 5508 | #endif /* !BB_MMU */ |
| 5509 | |
| 5510 | |
| 5511 | static int run_and_free_list(struct pipe *pi); |
| 5512 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5513 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5514 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 5515 | * end_trigger controls how often we stop parsing |
| 5516 | * NUL: parse all, execute, return |
| 5517 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 5518 | */ |
| 5519 | 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] | 5520 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5521 | /* Why we need empty flag? |
| 5522 | * An obscure corner case "false; ``; echo $?": |
| 5523 | * empty command in `` should still set $? to 0. |
| 5524 | * But we can't just set $? to 0 at the start, |
| 5525 | * this breaks "false; echo `echo $?`" case. |
| 5526 | */ |
| 5527 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5528 | while (1) { |
| 5529 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5530 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5531 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5532 | if (!pipe_list) { /* EOF */ |
| 5533 | if (empty) |
| 5534 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5535 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5536 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5537 | debug_print_tree(pipe_list, 0); |
| 5538 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 5539 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5540 | empty = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5541 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5542 | } |
| 5543 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5544 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5545 | { |
| 5546 | struct in_str input; |
| 5547 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5548 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5549 | } |
| 5550 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5551 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5552 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5553 | struct in_str input; |
| 5554 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5555 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5556 | } |
| 5557 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5558 | #if ENABLE_HUSH_TICK |
| 5559 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
| 5560 | { |
| 5561 | pid_t pid; |
| 5562 | int channel[2]; |
| 5563 | # if !BB_MMU |
| 5564 | char **to_free = NULL; |
| 5565 | # endif |
| 5566 | |
| 5567 | xpipe(channel); |
| 5568 | pid = BB_MMU ? xfork() : xvfork(); |
| 5569 | if (pid == 0) { /* child */ |
| 5570 | disable_restore_tty_pgrp_on_exit(); |
| 5571 | /* Process substitution is not considered to be usual |
| 5572 | * 'command execution'. |
| 5573 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 5574 | */ |
| 5575 | bb_signals(0 |
| 5576 | + (1 << SIGTSTP) |
| 5577 | + (1 << SIGTTIN) |
| 5578 | + (1 << SIGTTOU) |
| 5579 | , SIG_IGN); |
| 5580 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 5581 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 5582 | xmove_fd(channel[1], 1); |
| 5583 | /* Prevent it from trying to handle ctrl-z etc */ |
| 5584 | IF_HUSH_JOB(G.run_list_level = 1;) |
| 5585 | /* Awful hack for `trap` or $(trap). |
| 5586 | * |
| 5587 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 5588 | * contains an example where "trap" is executed in a subshell: |
| 5589 | * |
| 5590 | * save_traps=$(trap) |
| 5591 | * ... |
| 5592 | * eval "$save_traps" |
| 5593 | * |
| 5594 | * Standard does not say that "trap" in subshell shall print |
| 5595 | * parent shell's traps. It only says that its output |
| 5596 | * must have suitable form, but then, in the above example |
| 5597 | * (which is not supposed to be normative), it implies that. |
| 5598 | * |
| 5599 | * bash (and probably other shell) does implement it |
| 5600 | * (traps are reset to defaults, but "trap" still shows them), |
| 5601 | * but as a result, "trap" logic is hopelessly messed up: |
| 5602 | * |
| 5603 | * # trap |
| 5604 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 5605 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 5606 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 5607 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 5608 | * trap -- 'echo Ho' SIGWINCH |
| 5609 | * # echo `(trap)` <--- in subshell in subshell - output |
| 5610 | * trap -- 'echo Ho' SIGWINCH |
| 5611 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 5612 | * trap -- 'echo Ho' SIGWINCH |
| 5613 | * |
| 5614 | * The rules when to forget and when to not forget traps |
| 5615 | * get really complex and nonsensical. |
| 5616 | * |
| 5617 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 5618 | */ |
| 5619 | s = skip_whitespace(s); |
| 5620 | if (strncmp(s, "trap", 4) == 0 |
| 5621 | && skip_whitespace(s + 4)[0] == '\0' |
| 5622 | ) { |
| 5623 | static const char *const argv[] = { NULL, NULL }; |
| 5624 | builtin_trap((char**)argv); |
| 5625 | exit(0); /* not _exit() - we need to fflush */ |
| 5626 | } |
| 5627 | # if BB_MMU |
| 5628 | reset_traps_to_defaults(); |
| 5629 | parse_and_run_string(s); |
| 5630 | _exit(G.last_exitcode); |
| 5631 | # else |
| 5632 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 5633 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 5634 | * huge=`cat BIG` # was blocking here forever |
| 5635 | * echo OK |
| 5636 | */ |
| 5637 | re_execute_shell(&to_free, |
| 5638 | s, |
| 5639 | G.global_argv[0], |
| 5640 | G.global_argv + 1, |
| 5641 | NULL); |
| 5642 | # endif |
| 5643 | } |
| 5644 | |
| 5645 | /* parent */ |
| 5646 | *pid_p = pid; |
| 5647 | # if ENABLE_HUSH_FAST |
| 5648 | G.count_SIGCHLD++; |
| 5649 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 5650 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 5651 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 5652 | # endif |
| 5653 | enable_restore_tty_pgrp_on_exit(); |
| 5654 | # if !BB_MMU |
| 5655 | free(to_free); |
| 5656 | # endif |
| 5657 | close(channel[1]); |
| 5658 | close_on_exec_on(channel[0]); |
| 5659 | return xfdopen_for_read(channel[0]); |
| 5660 | } |
| 5661 | |
| 5662 | /* Return code is exit status of the process that is run. */ |
| 5663 | static int process_command_subs(o_string *dest, const char *s) |
| 5664 | { |
| 5665 | FILE *fp; |
| 5666 | struct in_str pipe_str; |
| 5667 | pid_t pid; |
| 5668 | int status, ch, eol_cnt; |
| 5669 | |
| 5670 | fp = generate_stream_from_string(s, &pid); |
| 5671 | |
| 5672 | /* Now send results of command back into original context */ |
| 5673 | setup_file_in_str(&pipe_str, fp); |
| 5674 | eol_cnt = 0; |
| 5675 | while ((ch = i_getch(&pipe_str)) != EOF) { |
| 5676 | if (ch == '\n') { |
| 5677 | eol_cnt++; |
| 5678 | continue; |
| 5679 | } |
| 5680 | while (eol_cnt) { |
| 5681 | o_addchr(dest, '\n'); |
| 5682 | eol_cnt--; |
| 5683 | } |
| 5684 | o_addQchr(dest, ch); |
| 5685 | } |
| 5686 | |
| 5687 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
| 5688 | fclose(fp); |
| 5689 | /* We need to extract exitcode. Test case |
| 5690 | * "true; echo `sleep 1; false` $?" |
| 5691 | * should print 1 */ |
| 5692 | safe_waitpid(pid, &status, 0); |
| 5693 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 5694 | return WEXITSTATUS(status); |
| 5695 | } |
| 5696 | #endif /* ENABLE_HUSH_TICK */ |
| 5697 | |
| 5698 | |
| 5699 | static void setup_heredoc(struct redir_struct *redir) |
| 5700 | { |
| 5701 | struct fd_pair pair; |
| 5702 | pid_t pid; |
| 5703 | int len, written; |
| 5704 | /* the _body_ of heredoc (misleading field name) */ |
| 5705 | const char *heredoc = redir->rd_filename; |
| 5706 | char *expanded; |
| 5707 | #if !BB_MMU |
| 5708 | char **to_free; |
| 5709 | #endif |
| 5710 | |
| 5711 | expanded = NULL; |
| 5712 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5713 | expanded = encode_then_expand_string(heredoc, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5714 | if (expanded) |
| 5715 | heredoc = expanded; |
| 5716 | } |
| 5717 | len = strlen(heredoc); |
| 5718 | |
| 5719 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 5720 | xpiped_pair(pair); |
| 5721 | xmove_fd(pair.rd, redir->rd_fd); |
| 5722 | |
| 5723 | /* Try writing without forking. Newer kernels have |
| 5724 | * dynamically growing pipes. Must use non-blocking write! */ |
| 5725 | ndelay_on(pair.wr); |
| 5726 | while (1) { |
| 5727 | written = write(pair.wr, heredoc, len); |
| 5728 | if (written <= 0) |
| 5729 | break; |
| 5730 | len -= written; |
| 5731 | if (len == 0) { |
| 5732 | close(pair.wr); |
| 5733 | free(expanded); |
| 5734 | return; |
| 5735 | } |
| 5736 | heredoc += written; |
| 5737 | } |
| 5738 | ndelay_off(pair.wr); |
| 5739 | |
| 5740 | /* Okay, pipe buffer was not big enough */ |
| 5741 | /* Note: we must not create a stray child (bastard? :) |
| 5742 | * for the unsuspecting parent process. Child creates a grandchild |
| 5743 | * and exits before parent execs the process which consumes heredoc |
| 5744 | * (that exec happens after we return from this function) */ |
| 5745 | #if !BB_MMU |
| 5746 | to_free = NULL; |
| 5747 | #endif |
| 5748 | pid = xvfork(); |
| 5749 | if (pid == 0) { |
| 5750 | /* child */ |
| 5751 | disable_restore_tty_pgrp_on_exit(); |
| 5752 | pid = BB_MMU ? xfork() : xvfork(); |
| 5753 | if (pid != 0) |
| 5754 | _exit(0); |
| 5755 | /* grandchild */ |
| 5756 | close(redir->rd_fd); /* read side of the pipe */ |
| 5757 | #if BB_MMU |
| 5758 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 5759 | _exit(0); |
| 5760 | #else |
| 5761 | /* Delegate blocking writes to another process */ |
| 5762 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 5763 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 5764 | #endif |
| 5765 | } |
| 5766 | /* parent */ |
| 5767 | #if ENABLE_HUSH_FAST |
| 5768 | G.count_SIGCHLD++; |
| 5769 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 5770 | #endif |
| 5771 | enable_restore_tty_pgrp_on_exit(); |
| 5772 | #if !BB_MMU |
| 5773 | free(to_free); |
| 5774 | #endif |
| 5775 | close(pair.wr); |
| 5776 | free(expanded); |
| 5777 | wait(NULL); /* wait till child has died */ |
| 5778 | } |
| 5779 | |
| 5780 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 5781 | * and stderr if they are redirected. */ |
| 5782 | static int setup_redirects(struct command *prog, int squirrel[]) |
| 5783 | { |
| 5784 | int openfd, mode; |
| 5785 | struct redir_struct *redir; |
| 5786 | |
| 5787 | for (redir = prog->redirects; redir; redir = redir->next) { |
| 5788 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
| 5789 | /* rd_fd<<HERE case */ |
| 5790 | if (squirrel && redir->rd_fd < 3 |
| 5791 | && squirrel[redir->rd_fd] < 0 |
| 5792 | ) { |
| 5793 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
| 5794 | } |
| 5795 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 5796 | * of the heredoc */ |
| 5797 | debug_printf_parse("set heredoc '%s'\n", |
| 5798 | redir->rd_filename); |
| 5799 | setup_heredoc(redir); |
| 5800 | continue; |
| 5801 | } |
| 5802 | |
| 5803 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
| 5804 | /* rd_fd<*>file case (<*> is <,>,>>,<>) */ |
| 5805 | char *p; |
| 5806 | if (redir->rd_filename == NULL) { |
| 5807 | /* Something went wrong in the parse. |
| 5808 | * Pretend it didn't happen */ |
| 5809 | bb_error_msg("bug in redirect parse"); |
| 5810 | continue; |
| 5811 | } |
| 5812 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5813 | p = expand_string_to_string(redir->rd_filename, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5814 | openfd = open_or_warn(p, mode); |
| 5815 | free(p); |
| 5816 | if (openfd < 0) { |
| 5817 | /* this could get lost if stderr has been redirected, but |
| 5818 | * bash and ash both lose it as well (though zsh doesn't!) */ |
| 5819 | //what the above comment tries to say? |
| 5820 | return 1; |
| 5821 | } |
| 5822 | } else { |
| 5823 | /* rd_fd<*>rd_dup or rd_fd<*>- cases */ |
| 5824 | openfd = redir->rd_dup; |
| 5825 | } |
| 5826 | |
| 5827 | if (openfd != redir->rd_fd) { |
| 5828 | if (squirrel && redir->rd_fd < 3 |
| 5829 | && squirrel[redir->rd_fd] < 0 |
| 5830 | ) { |
| 5831 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
| 5832 | } |
| 5833 | if (openfd == REDIRFD_CLOSE) { |
| 5834 | /* "n>-" means "close me" */ |
| 5835 | close(redir->rd_fd); |
| 5836 | } else { |
| 5837 | xdup2(openfd, redir->rd_fd); |
| 5838 | if (redir->rd_dup == REDIRFD_TO_FILE) |
| 5839 | close(openfd); |
| 5840 | } |
| 5841 | } |
| 5842 | } |
| 5843 | return 0; |
| 5844 | } |
| 5845 | |
| 5846 | static void restore_redirects(int squirrel[]) |
| 5847 | { |
| 5848 | int i, fd; |
| 5849 | for (i = 0; i < 3; i++) { |
| 5850 | fd = squirrel[i]; |
| 5851 | if (fd != -1) { |
| 5852 | /* We simply die on error */ |
| 5853 | xmove_fd(fd, i); |
| 5854 | } |
| 5855 | } |
| 5856 | } |
| 5857 | |
| 5858 | static char *find_in_path(const char *arg) |
| 5859 | { |
| 5860 | char *ret = NULL; |
| 5861 | const char *PATH = get_local_var_value("PATH"); |
| 5862 | |
| 5863 | if (!PATH) |
| 5864 | return NULL; |
| 5865 | |
| 5866 | while (1) { |
| 5867 | const char *end = strchrnul(PATH, ':'); |
| 5868 | int sz = end - PATH; /* must be int! */ |
| 5869 | |
| 5870 | free(ret); |
| 5871 | if (sz != 0) { |
| 5872 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 5873 | } else { |
| 5874 | /* We have xxx::yyyy in $PATH, |
| 5875 | * it means "use current dir" */ |
| 5876 | ret = xstrdup(arg); |
| 5877 | } |
| 5878 | if (access(ret, F_OK) == 0) |
| 5879 | break; |
| 5880 | |
| 5881 | if (*end == '\0') { |
| 5882 | free(ret); |
| 5883 | return NULL; |
| 5884 | } |
| 5885 | PATH = end + 1; |
| 5886 | } |
| 5887 | |
| 5888 | return ret; |
| 5889 | } |
| 5890 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5891 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5892 | const struct built_in_command *x, |
| 5893 | const struct built_in_command *end) |
| 5894 | { |
| 5895 | while (x != end) { |
| 5896 | if (strcmp(name, x->b_cmd) != 0) { |
| 5897 | x++; |
| 5898 | continue; |
| 5899 | } |
| 5900 | debug_printf_exec("found builtin '%s'\n", name); |
| 5901 | return x; |
| 5902 | } |
| 5903 | return NULL; |
| 5904 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5905 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5906 | { |
| 5907 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 5908 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5909 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5910 | { |
| 5911 | const struct built_in_command *x = find_builtin1(name); |
| 5912 | if (x) |
| 5913 | return x; |
| 5914 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 5915 | } |
| 5916 | |
| 5917 | #if ENABLE_HUSH_FUNCTIONS |
| 5918 | static struct function **find_function_slot(const char *name) |
| 5919 | { |
| 5920 | struct function **funcpp = &G.top_func; |
| 5921 | while (*funcpp) { |
| 5922 | if (strcmp(name, (*funcpp)->name) == 0) { |
| 5923 | break; |
| 5924 | } |
| 5925 | funcpp = &(*funcpp)->next; |
| 5926 | } |
| 5927 | return funcpp; |
| 5928 | } |
| 5929 | |
| 5930 | static const struct function *find_function(const char *name) |
| 5931 | { |
| 5932 | const struct function *funcp = *find_function_slot(name); |
| 5933 | if (funcp) |
| 5934 | debug_printf_exec("found function '%s'\n", name); |
| 5935 | return funcp; |
| 5936 | } |
| 5937 | |
| 5938 | /* Note: takes ownership on name ptr */ |
| 5939 | static struct function *new_function(char *name) |
| 5940 | { |
| 5941 | struct function **funcpp = find_function_slot(name); |
| 5942 | struct function *funcp = *funcpp; |
| 5943 | |
| 5944 | if (funcp != NULL) { |
| 5945 | struct command *cmd = funcp->parent_cmd; |
| 5946 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 5947 | if (!cmd) { |
| 5948 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 5949 | free(funcp->name); |
| 5950 | /* Note: if !funcp->body, do not free body_as_string! |
| 5951 | * This is a special case of "-F name body" function: |
| 5952 | * body_as_string was not malloced! */ |
| 5953 | if (funcp->body) { |
| 5954 | free_pipe_list(funcp->body); |
| 5955 | # if !BB_MMU |
| 5956 | free(funcp->body_as_string); |
| 5957 | # endif |
| 5958 | } |
| 5959 | } else { |
| 5960 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 5961 | cmd->argv[0] = funcp->name; |
| 5962 | cmd->group = funcp->body; |
| 5963 | # if !BB_MMU |
| 5964 | cmd->group_as_string = funcp->body_as_string; |
| 5965 | # endif |
| 5966 | } |
| 5967 | } else { |
| 5968 | debug_printf_exec("remembering new function '%s'\n", name); |
| 5969 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 5970 | /*funcp->next = NULL;*/ |
| 5971 | } |
| 5972 | |
| 5973 | funcp->name = name; |
| 5974 | return funcp; |
| 5975 | } |
| 5976 | |
| 5977 | static void unset_func(const char *name) |
| 5978 | { |
| 5979 | struct function **funcpp = find_function_slot(name); |
| 5980 | struct function *funcp = *funcpp; |
| 5981 | |
| 5982 | if (funcp != NULL) { |
| 5983 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 5984 | *funcpp = funcp->next; |
| 5985 | /* funcp is unlinked now, deleting it. |
| 5986 | * Note: if !funcp->body, the function was created by |
| 5987 | * "-F name body", do not free ->body_as_string |
| 5988 | * and ->name as they were not malloced. */ |
| 5989 | if (funcp->body) { |
| 5990 | free_pipe_list(funcp->body); |
| 5991 | free(funcp->name); |
| 5992 | # if !BB_MMU |
| 5993 | free(funcp->body_as_string); |
| 5994 | # endif |
| 5995 | } |
| 5996 | free(funcp); |
| 5997 | } |
| 5998 | } |
| 5999 | |
| 6000 | # if BB_MMU |
| 6001 | #define exec_function(to_free, funcp, argv) \ |
| 6002 | exec_function(funcp, argv) |
| 6003 | # endif |
| 6004 | static void exec_function(char ***to_free, |
| 6005 | const struct function *funcp, |
| 6006 | char **argv) NORETURN; |
| 6007 | static void exec_function(char ***to_free, |
| 6008 | const struct function *funcp, |
| 6009 | char **argv) |
| 6010 | { |
| 6011 | # if BB_MMU |
| 6012 | int n = 1; |
| 6013 | |
| 6014 | argv[0] = G.global_argv[0]; |
| 6015 | G.global_argv = argv; |
| 6016 | while (*++argv) |
| 6017 | n++; |
| 6018 | G.global_argc = n; |
| 6019 | /* On MMU, funcp->body is always non-NULL */ |
| 6020 | n = run_list(funcp->body); |
| 6021 | fflush_all(); |
| 6022 | _exit(n); |
| 6023 | # else |
| 6024 | re_execute_shell(to_free, |
| 6025 | funcp->body_as_string, |
| 6026 | G.global_argv[0], |
| 6027 | argv + 1, |
| 6028 | NULL); |
| 6029 | # endif |
| 6030 | } |
| 6031 | |
| 6032 | static int run_function(const struct function *funcp, char **argv) |
| 6033 | { |
| 6034 | int rc; |
| 6035 | save_arg_t sv; |
| 6036 | smallint sv_flg; |
| 6037 | |
| 6038 | save_and_replace_G_args(&sv, argv); |
| 6039 | |
| 6040 | /* "we are in function, ok to use return" */ |
| 6041 | sv_flg = G.flag_return_in_progress; |
| 6042 | G.flag_return_in_progress = -1; |
| 6043 | # if ENABLE_HUSH_LOCAL |
| 6044 | G.func_nest_level++; |
| 6045 | # endif |
| 6046 | |
| 6047 | /* On MMU, funcp->body is always non-NULL */ |
| 6048 | # if !BB_MMU |
| 6049 | if (!funcp->body) { |
| 6050 | /* Function defined by -F */ |
| 6051 | parse_and_run_string(funcp->body_as_string); |
| 6052 | rc = G.last_exitcode; |
| 6053 | } else |
| 6054 | # endif |
| 6055 | { |
| 6056 | rc = run_list(funcp->body); |
| 6057 | } |
| 6058 | |
| 6059 | # if ENABLE_HUSH_LOCAL |
| 6060 | { |
| 6061 | struct variable *var; |
| 6062 | struct variable **var_pp; |
| 6063 | |
| 6064 | var_pp = &G.top_var; |
| 6065 | while ((var = *var_pp) != NULL) { |
| 6066 | if (var->func_nest_level < G.func_nest_level) { |
| 6067 | var_pp = &var->next; |
| 6068 | continue; |
| 6069 | } |
| 6070 | /* Unexport */ |
| 6071 | if (var->flg_export) |
| 6072 | bb_unsetenv(var->varstr); |
| 6073 | /* Remove from global list */ |
| 6074 | *var_pp = var->next; |
| 6075 | /* Free */ |
| 6076 | if (!var->max_len) |
| 6077 | free(var->varstr); |
| 6078 | free(var); |
| 6079 | } |
| 6080 | G.func_nest_level--; |
| 6081 | } |
| 6082 | # endif |
| 6083 | G.flag_return_in_progress = sv_flg; |
| 6084 | |
| 6085 | restore_G_args(&sv, argv); |
| 6086 | |
| 6087 | return rc; |
| 6088 | } |
| 6089 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 6090 | |
| 6091 | |
| 6092 | #if BB_MMU |
| 6093 | #define exec_builtin(to_free, x, argv) \ |
| 6094 | exec_builtin(x, argv) |
| 6095 | #else |
| 6096 | #define exec_builtin(to_free, x, argv) \ |
| 6097 | exec_builtin(to_free, argv) |
| 6098 | #endif |
| 6099 | static void exec_builtin(char ***to_free, |
| 6100 | const struct built_in_command *x, |
| 6101 | char **argv) NORETURN; |
| 6102 | static void exec_builtin(char ***to_free, |
| 6103 | const struct built_in_command *x, |
| 6104 | char **argv) |
| 6105 | { |
| 6106 | #if BB_MMU |
| 6107 | int rcode = x->b_function(argv); |
| 6108 | fflush_all(); |
| 6109 | _exit(rcode); |
| 6110 | #else |
| 6111 | /* On NOMMU, we must never block! |
| 6112 | * Example: { sleep 99 | read line; } & echo Ok |
| 6113 | */ |
| 6114 | re_execute_shell(to_free, |
| 6115 | argv[0], |
| 6116 | G.global_argv[0], |
| 6117 | G.global_argv + 1, |
| 6118 | argv); |
| 6119 | #endif |
| 6120 | } |
| 6121 | |
| 6122 | |
| 6123 | static void execvp_or_die(char **argv) NORETURN; |
| 6124 | static void execvp_or_die(char **argv) |
| 6125 | { |
| 6126 | debug_printf_exec("execing '%s'\n", argv[0]); |
| 6127 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 6128 | execvp(argv[0], argv); |
| 6129 | bb_perror_msg("can't execute '%s'", argv[0]); |
| 6130 | _exit(127); /* bash compat */ |
| 6131 | } |
| 6132 | |
| 6133 | #if ENABLE_HUSH_MODE_X |
| 6134 | static void dump_cmd_in_x_mode(char **argv) |
| 6135 | { |
| 6136 | if (G_x_mode && argv) { |
| 6137 | /* We want to output the line in one write op */ |
| 6138 | char *buf, *p; |
| 6139 | int len; |
| 6140 | int n; |
| 6141 | |
| 6142 | len = 3; |
| 6143 | n = 0; |
| 6144 | while (argv[n]) |
| 6145 | len += strlen(argv[n++]) + 1; |
| 6146 | buf = xmalloc(len); |
| 6147 | buf[0] = '+'; |
| 6148 | p = buf + 1; |
| 6149 | n = 0; |
| 6150 | while (argv[n]) |
| 6151 | p += sprintf(p, " %s", argv[n++]); |
| 6152 | *p++ = '\n'; |
| 6153 | *p = '\0'; |
| 6154 | fputs(buf, stderr); |
| 6155 | free(buf); |
| 6156 | } |
| 6157 | } |
| 6158 | #else |
| 6159 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 6160 | #endif |
| 6161 | |
| 6162 | #if BB_MMU |
| 6163 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 6164 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 6165 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 6166 | pseudo_exec(command, argv_expanded) |
| 6167 | #endif |
| 6168 | |
| 6169 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 6170 | * Never returns. |
| 6171 | * Don't exit() here. If you don't exec, use _exit instead. |
| 6172 | * The at_exit handlers apparently confuse the calling process, |
| 6173 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
| 6174 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 6175 | char **argv, int assignment_cnt, |
| 6176 | char **argv_expanded) NORETURN; |
| 6177 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 6178 | char **argv, int assignment_cnt, |
| 6179 | char **argv_expanded) |
| 6180 | { |
| 6181 | char **new_env; |
| 6182 | |
| 6183 | new_env = expand_assignments(argv, assignment_cnt); |
| 6184 | dump_cmd_in_x_mode(new_env); |
| 6185 | |
| 6186 | if (!argv[assignment_cnt]) { |
| 6187 | /* Case when we are here: ... | var=val | ... |
| 6188 | * (note that we do not exit early, i.e., do not optimize out |
| 6189 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 6190 | */ |
| 6191 | free_strings(new_env); |
| 6192 | _exit(EXIT_SUCCESS); |
| 6193 | } |
| 6194 | |
| 6195 | #if BB_MMU |
| 6196 | set_vars_and_save_old(new_env); |
| 6197 | free(new_env); /* optional */ |
| 6198 | /* we can also destroy set_vars_and_save_old's return value, |
| 6199 | * to save memory */ |
| 6200 | #else |
| 6201 | nommu_save->new_env = new_env; |
| 6202 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
| 6203 | #endif |
| 6204 | |
| 6205 | if (argv_expanded) { |
| 6206 | argv = argv_expanded; |
| 6207 | } else { |
| 6208 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 6209 | #if !BB_MMU |
| 6210 | nommu_save->argv = argv; |
| 6211 | #endif |
| 6212 | } |
| 6213 | dump_cmd_in_x_mode(argv); |
| 6214 | |
| 6215 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 6216 | if (strchr(argv[0], '/') != NULL) |
| 6217 | goto skip; |
| 6218 | #endif |
| 6219 | |
| 6220 | /* Check if the command matches any of the builtins. |
| 6221 | * Depending on context, this might be redundant. But it's |
| 6222 | * easier to waste a few CPU cycles than it is to figure out |
| 6223 | * if this is one of those cases. |
| 6224 | */ |
| 6225 | { |
| 6226 | /* On NOMMU, it is more expensive to re-execute shell |
| 6227 | * just in order to run echo or test builtin. |
| 6228 | * It's better to skip it here and run corresponding |
| 6229 | * non-builtin later. */ |
| 6230 | const struct built_in_command *x; |
| 6231 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 6232 | if (x) { |
| 6233 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
| 6234 | } |
| 6235 | } |
| 6236 | #if ENABLE_HUSH_FUNCTIONS |
| 6237 | /* Check if the command matches any functions */ |
| 6238 | { |
| 6239 | const struct function *funcp = find_function(argv[0]); |
| 6240 | if (funcp) { |
| 6241 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
| 6242 | } |
| 6243 | } |
| 6244 | #endif |
| 6245 | |
| 6246 | #if ENABLE_FEATURE_SH_STANDALONE |
| 6247 | /* Check if the command matches any busybox applets */ |
| 6248 | { |
| 6249 | int a = find_applet_by_name(argv[0]); |
| 6250 | if (a >= 0) { |
| 6251 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 6252 | if (APPLET_IS_NOEXEC(a)) { |
| 6253 | debug_printf_exec("running applet '%s'\n", argv[0]); |
| 6254 | run_applet_no_and_exit(a, argv); |
| 6255 | } |
| 6256 | # endif |
| 6257 | /* Re-exec ourselves */ |
| 6258 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
| 6259 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 6260 | execv(bb_busybox_exec_path, argv); |
| 6261 | /* If they called chroot or otherwise made the binary no longer |
| 6262 | * executable, fall through */ |
| 6263 | } |
| 6264 | } |
| 6265 | #endif |
| 6266 | |
| 6267 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 6268 | skip: |
| 6269 | #endif |
| 6270 | execvp_or_die(argv); |
| 6271 | } |
| 6272 | |
| 6273 | /* Called after [v]fork() in run_pipe |
| 6274 | */ |
| 6275 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 6276 | struct command *command, |
| 6277 | char **argv_expanded) NORETURN; |
| 6278 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 6279 | struct command *command, |
| 6280 | char **argv_expanded) |
| 6281 | { |
| 6282 | if (command->argv) { |
| 6283 | pseudo_exec_argv(nommu_save, command->argv, |
| 6284 | command->assignment_cnt, argv_expanded); |
| 6285 | } |
| 6286 | |
| 6287 | if (command->group) { |
| 6288 | /* Cases when we are here: |
| 6289 | * ( list ) |
| 6290 | * { list } & |
| 6291 | * ... | ( list ) | ... |
| 6292 | * ... | { list } | ... |
| 6293 | */ |
| 6294 | #if BB_MMU |
| 6295 | int rcode; |
| 6296 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 6297 | reset_traps_to_defaults(); |
| 6298 | rcode = run_list(command->group); |
| 6299 | /* OK to leak memory by not calling free_pipe_list, |
| 6300 | * since this process is about to exit */ |
| 6301 | _exit(rcode); |
| 6302 | #else |
| 6303 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 6304 | command->group_as_string, |
| 6305 | G.global_argv[0], |
| 6306 | G.global_argv + 1, |
| 6307 | NULL); |
| 6308 | #endif |
| 6309 | } |
| 6310 | |
| 6311 | /* Case when we are here: ... | >file */ |
| 6312 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 6313 | _exit(EXIT_SUCCESS); |
| 6314 | } |
| 6315 | |
| 6316 | #if ENABLE_HUSH_JOB |
| 6317 | static const char *get_cmdtext(struct pipe *pi) |
| 6318 | { |
| 6319 | char **argv; |
| 6320 | char *p; |
| 6321 | int len; |
| 6322 | |
| 6323 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 6324 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 6325 | * On subsequent bg argv is trashed, but we won't use it */ |
| 6326 | if (pi->cmdtext) |
| 6327 | return pi->cmdtext; |
| 6328 | argv = pi->cmds[0].argv; |
| 6329 | if (!argv || !argv[0]) { |
| 6330 | pi->cmdtext = xzalloc(1); |
| 6331 | return pi->cmdtext; |
| 6332 | } |
| 6333 | |
| 6334 | len = 0; |
| 6335 | do { |
| 6336 | len += strlen(*argv) + 1; |
| 6337 | } while (*++argv); |
| 6338 | p = xmalloc(len); |
| 6339 | pi->cmdtext = p; |
| 6340 | argv = pi->cmds[0].argv; |
| 6341 | do { |
| 6342 | len = strlen(*argv); |
| 6343 | memcpy(p, *argv, len); |
| 6344 | p += len; |
| 6345 | *p++ = ' '; |
| 6346 | } while (*++argv); |
| 6347 | p[-1] = '\0'; |
| 6348 | return pi->cmdtext; |
| 6349 | } |
| 6350 | |
| 6351 | static void insert_bg_job(struct pipe *pi) |
| 6352 | { |
| 6353 | struct pipe *job, **jobp; |
| 6354 | int i; |
| 6355 | |
| 6356 | /* Linear search for the ID of the job to use */ |
| 6357 | pi->jobid = 1; |
| 6358 | for (job = G.job_list; job; job = job->next) |
| 6359 | if (job->jobid >= pi->jobid) |
| 6360 | pi->jobid = job->jobid + 1; |
| 6361 | |
| 6362 | /* Add job to the list of running jobs */ |
| 6363 | jobp = &G.job_list; |
| 6364 | while ((job = *jobp) != NULL) |
| 6365 | jobp = &job->next; |
| 6366 | job = *jobp = xmalloc(sizeof(*job)); |
| 6367 | |
| 6368 | *job = *pi; /* physical copy */ |
| 6369 | job->next = NULL; |
| 6370 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 6371 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 6372 | for (i = 0; i < pi->num_cmds; i++) { |
| 6373 | job->cmds[i].pid = pi->cmds[i].pid; |
| 6374 | /* all other fields are not used and stay zero */ |
| 6375 | } |
| 6376 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 6377 | |
| 6378 | if (G_interactive_fd) |
| 6379 | printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext); |
| 6380 | G.last_jobid = job->jobid; |
| 6381 | } |
| 6382 | |
| 6383 | static void remove_bg_job(struct pipe *pi) |
| 6384 | { |
| 6385 | struct pipe *prev_pipe; |
| 6386 | |
| 6387 | if (pi == G.job_list) { |
| 6388 | G.job_list = pi->next; |
| 6389 | } else { |
| 6390 | prev_pipe = G.job_list; |
| 6391 | while (prev_pipe->next != pi) |
| 6392 | prev_pipe = prev_pipe->next; |
| 6393 | prev_pipe->next = pi->next; |
| 6394 | } |
| 6395 | if (G.job_list) |
| 6396 | G.last_jobid = G.job_list->jobid; |
| 6397 | else |
| 6398 | G.last_jobid = 0; |
| 6399 | } |
| 6400 | |
| 6401 | /* Remove a backgrounded job */ |
| 6402 | static void delete_finished_bg_job(struct pipe *pi) |
| 6403 | { |
| 6404 | remove_bg_job(pi); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6405 | free_pipe(pi); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6406 | } |
| 6407 | #endif /* JOB */ |
| 6408 | |
| 6409 | /* Check to see if any processes have exited -- if they |
| 6410 | * have, figure out why and see if a job has completed */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 6411 | static int checkjobs(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6412 | { |
| 6413 | int attributes; |
| 6414 | int status; |
| 6415 | #if ENABLE_HUSH_JOB |
| 6416 | struct pipe *pi; |
| 6417 | #endif |
| 6418 | pid_t childpid; |
| 6419 | int rcode = 0; |
| 6420 | |
| 6421 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 6422 | |
| 6423 | attributes = WUNTRACED; |
| 6424 | if (fg_pipe == NULL) |
| 6425 | attributes |= WNOHANG; |
| 6426 | |
| 6427 | errno = 0; |
| 6428 | #if ENABLE_HUSH_FAST |
| 6429 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 6430 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 6431 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 6432 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 6433 | /* Avoid doing waitpid syscall if possible */ |
| 6434 | if (!G.we_have_children) { |
| 6435 | errno = ECHILD; |
| 6436 | return -1; |
| 6437 | } |
| 6438 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 6439 | /* We have children, but they did not exit |
| 6440 | * or stop yet (we saw no SIGCHLD) */ |
| 6441 | return 0; |
| 6442 | } |
| 6443 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 6444 | } |
| 6445 | #endif |
| 6446 | |
| 6447 | /* Do we do this right? |
| 6448 | * bash-3.00# sleep 20 | false |
| 6449 | * <ctrl-Z pressed> |
| 6450 | * [3]+ Stopped sleep 20 | false |
| 6451 | * bash-3.00# echo $? |
| 6452 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 6453 | * [hush 1.14.0: yes we do it right] |
| 6454 | */ |
| 6455 | wait_more: |
| 6456 | while (1) { |
| 6457 | int i; |
| 6458 | int dead; |
| 6459 | |
| 6460 | #if ENABLE_HUSH_FAST |
| 6461 | i = G.count_SIGCHLD; |
| 6462 | #endif |
| 6463 | childpid = waitpid(-1, &status, attributes); |
| 6464 | if (childpid <= 0) { |
| 6465 | if (childpid && errno != ECHILD) |
| 6466 | bb_perror_msg("waitpid"); |
| 6467 | #if ENABLE_HUSH_FAST |
| 6468 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 6469 | G.we_have_children = (childpid == 0); |
| 6470 | G.handled_SIGCHLD = i; |
| 6471 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6472 | } |
| 6473 | #endif |
| 6474 | break; |
| 6475 | } |
| 6476 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 6477 | |
| 6478 | #if DEBUG_JOBS |
| 6479 | if (WIFSTOPPED(status)) |
| 6480 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 6481 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 6482 | if (WIFSIGNALED(status)) |
| 6483 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 6484 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 6485 | if (WIFEXITED(status)) |
| 6486 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 6487 | childpid, WEXITSTATUS(status)); |
| 6488 | #endif |
| 6489 | /* Were we asked to wait for fg pipe? */ |
| 6490 | if (fg_pipe) { |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 6491 | i = fg_pipe->num_cmds; |
| 6492 | while (--i >= 0) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6493 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 6494 | if (fg_pipe->cmds[i].pid != childpid) |
| 6495 | continue; |
| 6496 | if (dead) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6497 | int ex; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6498 | fg_pipe->cmds[i].pid = 0; |
| 6499 | fg_pipe->alive_cmds--; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6500 | ex = WEXITSTATUS(status); |
| 6501 | /* bash prints killer signal's name for *last* |
| 6502 | * process in pipe (prints just newline for SIGINT). |
| 6503 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 6504 | */ |
| 6505 | if (WIFSIGNALED(status)) { |
| 6506 | int sig = WTERMSIG(status); |
| 6507 | if (i == fg_pipe->num_cmds-1) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6508 | printf("%s\n", sig == SIGINT ? "" : get_signame(sig)); |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6509 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 6510 | * Maybe we need to use sig | 128? */ |
| 6511 | ex = sig + 128; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6512 | } |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6513 | fg_pipe->cmds[i].cmd_exitcode = ex; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6514 | } else { |
| 6515 | fg_pipe->cmds[i].is_stopped = 1; |
| 6516 | fg_pipe->stopped_cmds++; |
| 6517 | } |
| 6518 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 6519 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 6520 | if (fg_pipe->alive_cmds == fg_pipe->stopped_cmds) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6521 | /* All processes in fg pipe have exited or stopped */ |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6522 | i = fg_pipe->num_cmds; |
| 6523 | while (--i >= 0) { |
| 6524 | rcode = fg_pipe->cmds[i].cmd_exitcode; |
| 6525 | /* usually last process gives overall exitstatus, |
| 6526 | * but with "set -o pipefail", last *failed* process does */ |
| 6527 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 6528 | break; |
| 6529 | } |
| 6530 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6531 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 6532 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 6533 | * and "killall -STOP cat" */ |
| 6534 | if (G_interactive_fd) { |
| 6535 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 6536 | if (fg_pipe->alive_cmds != 0) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6537 | insert_bg_job(fg_pipe); |
| 6538 | #endif |
| 6539 | return rcode; |
| 6540 | } |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 6541 | if (fg_pipe->alive_cmds == 0) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6542 | return rcode; |
| 6543 | } |
| 6544 | /* There are still running processes in the fg pipe */ |
| 6545 | goto wait_more; /* do waitpid again */ |
| 6546 | } |
| 6547 | /* it wasnt fg_pipe, look for process in bg pipes */ |
| 6548 | } |
| 6549 | |
| 6550 | #if ENABLE_HUSH_JOB |
| 6551 | /* We asked to wait for bg or orphaned children */ |
| 6552 | /* No need to remember exitcode in this case */ |
| 6553 | for (pi = G.job_list; pi; pi = pi->next) { |
| 6554 | for (i = 0; i < pi->num_cmds; i++) { |
| 6555 | if (pi->cmds[i].pid == childpid) |
| 6556 | goto found_pi_and_prognum; |
| 6557 | } |
| 6558 | } |
| 6559 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 6560 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 6561 | continue; /* do waitpid again */ |
| 6562 | |
| 6563 | found_pi_and_prognum: |
| 6564 | if (dead) { |
| 6565 | /* child exited */ |
| 6566 | pi->cmds[i].pid = 0; |
| 6567 | pi->alive_cmds--; |
| 6568 | if (!pi->alive_cmds) { |
| 6569 | if (G_interactive_fd) |
| 6570 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 6571 | "Done", pi->cmdtext); |
| 6572 | delete_finished_bg_job(pi); |
| 6573 | } |
| 6574 | } else { |
| 6575 | /* child stopped */ |
| 6576 | pi->cmds[i].is_stopped = 1; |
| 6577 | pi->stopped_cmds++; |
| 6578 | } |
| 6579 | #endif |
| 6580 | } /* while (waitpid succeeds)... */ |
| 6581 | |
| 6582 | return rcode; |
| 6583 | } |
| 6584 | |
| 6585 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6586 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6587 | { |
| 6588 | pid_t p; |
| 6589 | int rcode = checkjobs(fg_pipe); |
| 6590 | if (G_saved_tty_pgrp) { |
| 6591 | /* Job finished, move the shell to the foreground */ |
| 6592 | p = getpgrp(); /* our process group id */ |
| 6593 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 6594 | tcsetpgrp(G_interactive_fd, p); |
| 6595 | } |
| 6596 | return rcode; |
| 6597 | } |
| 6598 | #endif |
| 6599 | |
| 6600 | /* Start all the jobs, but don't wait for anything to finish. |
| 6601 | * See checkjobs(). |
| 6602 | * |
| 6603 | * Return code is normally -1, when the caller has to wait for children |
| 6604 | * to finish to determine the exit status of the pipe. If the pipe |
| 6605 | * is a simple builtin command, however, the action is done by the |
| 6606 | * time run_pipe returns, and the exit code is provided as the |
| 6607 | * return value. |
| 6608 | * |
| 6609 | * Returns -1 only if started some children. IOW: we have to |
| 6610 | * mask out retvals of builtins etc with 0xff! |
| 6611 | * |
| 6612 | * The only case when we do not need to [v]fork is when the pipe |
| 6613 | * is single, non-backgrounded, non-subshell command. Examples: |
| 6614 | * cmd ; ... { list } ; ... |
| 6615 | * cmd && ... { list } && ... |
| 6616 | * cmd || ... { list } || ... |
| 6617 | * If it is, then we can run cmd as a builtin, NOFORK [do we do this?], |
| 6618 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 6619 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 6620 | * |
| 6621 | * Cases when we must fork: |
| 6622 | * non-single: cmd | cmd |
| 6623 | * backgrounded: cmd & { list } & |
| 6624 | * subshell: ( list ) [&] |
| 6625 | */ |
| 6626 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 6627 | #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] | 6628 | redirect_and_varexp_helper(new_env_p, old_vars_p, command, squirrel) |
| 6629 | #endif |
| 6630 | static int redirect_and_varexp_helper(char ***new_env_p, |
| 6631 | struct variable **old_vars_p, |
| 6632 | struct command *command, |
| 6633 | int squirrel[3], |
| 6634 | char **argv_expanded) |
| 6635 | { |
| 6636 | /* setup_redirects acts on file descriptors, not FILEs. |
| 6637 | * This is perfect for work that comes after exec(). |
| 6638 | * Is it really safe for inline use? Experimentally, |
| 6639 | * things seem to work. */ |
| 6640 | int rcode = setup_redirects(command, squirrel); |
| 6641 | if (rcode == 0) { |
| 6642 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 6643 | *new_env_p = new_env; |
| 6644 | dump_cmd_in_x_mode(new_env); |
| 6645 | dump_cmd_in_x_mode(argv_expanded); |
| 6646 | if (old_vars_p) |
| 6647 | *old_vars_p = set_vars_and_save_old(new_env); |
| 6648 | } |
| 6649 | return rcode; |
| 6650 | } |
| 6651 | static NOINLINE int run_pipe(struct pipe *pi) |
| 6652 | { |
| 6653 | static const char *const null_ptr = NULL; |
| 6654 | |
| 6655 | int cmd_no; |
| 6656 | int next_infd; |
| 6657 | struct command *command; |
| 6658 | char **argv_expanded; |
| 6659 | char **argv; |
| 6660 | /* it is not always needed, but we aim to smaller code */ |
| 6661 | int squirrel[] = { -1, -1, -1 }; |
| 6662 | int rcode; |
| 6663 | |
| 6664 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 6665 | debug_enter(); |
| 6666 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6667 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 6668 | * Result should be 3 lines: q w e, qwe, q w e |
| 6669 | */ |
| 6670 | G.ifs = get_local_var_value("IFS"); |
| 6671 | if (!G.ifs) |
| 6672 | G.ifs = defifs; |
| 6673 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6674 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 6675 | pi->stopped_cmds = 0; |
| 6676 | command = &pi->cmds[0]; |
| 6677 | argv_expanded = NULL; |
| 6678 | |
| 6679 | if (pi->num_cmds != 1 |
| 6680 | || pi->followup == PIPE_BG |
| 6681 | || command->cmd_type == CMD_SUBSHELL |
| 6682 | ) { |
| 6683 | goto must_fork; |
| 6684 | } |
| 6685 | |
| 6686 | pi->alive_cmds = 1; |
| 6687 | |
| 6688 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 6689 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 6690 | |
| 6691 | if (command->group) { |
| 6692 | #if ENABLE_HUSH_FUNCTIONS |
| 6693 | if (command->cmd_type == CMD_FUNCDEF) { |
| 6694 | /* "executing" func () { list } */ |
| 6695 | struct function *funcp; |
| 6696 | |
| 6697 | funcp = new_function(command->argv[0]); |
| 6698 | /* funcp->name is already set to argv[0] */ |
| 6699 | funcp->body = command->group; |
| 6700 | # if !BB_MMU |
| 6701 | funcp->body_as_string = command->group_as_string; |
| 6702 | command->group_as_string = NULL; |
| 6703 | # endif |
| 6704 | command->group = NULL; |
| 6705 | command->argv[0] = NULL; |
| 6706 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 6707 | funcp->parent_cmd = command; |
| 6708 | command->child_func = funcp; |
| 6709 | |
| 6710 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 6711 | debug_leave(); |
| 6712 | return EXIT_SUCCESS; |
| 6713 | } |
| 6714 | #endif |
| 6715 | /* { list } */ |
| 6716 | debug_printf("non-subshell group\n"); |
| 6717 | rcode = 1; /* exitcode if redir failed */ |
| 6718 | if (setup_redirects(command, squirrel) == 0) { |
| 6719 | debug_printf_exec(": run_list\n"); |
| 6720 | rcode = run_list(command->group) & 0xff; |
| 6721 | } |
| 6722 | restore_redirects(squirrel); |
| 6723 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 6724 | debug_leave(); |
| 6725 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 6726 | return rcode; |
| 6727 | } |
| 6728 | |
| 6729 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 6730 | { |
| 6731 | const struct built_in_command *x; |
| 6732 | #if ENABLE_HUSH_FUNCTIONS |
| 6733 | const struct function *funcp; |
| 6734 | #else |
| 6735 | enum { funcp = 0 }; |
| 6736 | #endif |
| 6737 | char **new_env = NULL; |
| 6738 | struct variable *old_vars = NULL; |
| 6739 | |
| 6740 | if (argv[command->assignment_cnt] == NULL) { |
| 6741 | /* Assignments, but no command */ |
| 6742 | /* Ensure redirects take effect (that is, create files). |
| 6743 | * Try "a=t >file" */ |
| 6744 | #if 0 /* A few cases in testsuite fail with this code. FIXME */ |
| 6745 | rcode = redirect_and_varexp_helper(&new_env, /*old_vars:*/ NULL, command, squirrel, /*argv_expanded:*/ NULL); |
| 6746 | /* Set shell variables */ |
| 6747 | if (new_env) { |
| 6748 | argv = new_env; |
| 6749 | while (*argv) { |
| 6750 | set_local_var(*argv, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 6751 | /* Do we need to flag set_local_var() errors? |
| 6752 | * "assignment to readonly var" and "putenv error" |
| 6753 | */ |
| 6754 | argv++; |
| 6755 | } |
| 6756 | } |
| 6757 | /* Redirect error sets $? to 1. Otherwise, |
| 6758 | * if evaluating assignment value set $?, retain it. |
| 6759 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 6760 | if (rcode == 0) |
| 6761 | rcode = G.last_exitcode; |
| 6762 | /* Exit, _skipping_ variable restoring code: */ |
| 6763 | goto clean_up_and_ret0; |
| 6764 | |
| 6765 | #else /* Older, bigger, but more correct code */ |
| 6766 | |
| 6767 | rcode = setup_redirects(command, squirrel); |
| 6768 | restore_redirects(squirrel); |
| 6769 | /* Set shell variables */ |
| 6770 | if (G_x_mode) |
| 6771 | bb_putchar_stderr('+'); |
| 6772 | while (*argv) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6773 | char *p = expand_string_to_string(*argv, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6774 | if (G_x_mode) |
| 6775 | fprintf(stderr, " %s", p); |
| 6776 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 6777 | *argv, p); |
| 6778 | set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 6779 | /* Do we need to flag set_local_var() errors? |
| 6780 | * "assignment to readonly var" and "putenv error" |
| 6781 | */ |
| 6782 | argv++; |
| 6783 | } |
| 6784 | if (G_x_mode) |
| 6785 | bb_putchar_stderr('\n'); |
| 6786 | /* Redirect error sets $? to 1. Otherwise, |
| 6787 | * if evaluating assignment value set $?, retain it. |
| 6788 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 6789 | if (rcode == 0) |
| 6790 | rcode = G.last_exitcode; |
| 6791 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 6792 | debug_leave(); |
| 6793 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 6794 | return rcode; |
| 6795 | #endif |
| 6796 | } |
| 6797 | |
| 6798 | /* Expand the rest into (possibly) many strings each */ |
| 6799 | if (0) {} |
| 6800 | #if ENABLE_HUSH_BASH_COMPAT |
| 6801 | else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) { |
| 6802 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
| 6803 | } |
| 6804 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6805 | else { |
| 6806 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
| 6807 | } |
| 6808 | |
| 6809 | /* if someone gives us an empty string: `cmd with empty output` */ |
| 6810 | if (!argv_expanded[0]) { |
| 6811 | free(argv_expanded); |
| 6812 | debug_leave(); |
| 6813 | return G.last_exitcode; |
| 6814 | } |
| 6815 | |
| 6816 | x = find_builtin(argv_expanded[0]); |
| 6817 | #if ENABLE_HUSH_FUNCTIONS |
| 6818 | funcp = NULL; |
| 6819 | if (!x) |
| 6820 | funcp = find_function(argv_expanded[0]); |
| 6821 | #endif |
| 6822 | if (x || funcp) { |
| 6823 | if (!funcp) { |
| 6824 | if (x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 6825 | debug_printf("exec with redirects only\n"); |
| 6826 | rcode = setup_redirects(command, NULL); |
| 6827 | goto clean_up_and_ret1; |
| 6828 | } |
| 6829 | } |
| 6830 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
| 6831 | if (rcode == 0) { |
| 6832 | if (!funcp) { |
| 6833 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 6834 | x->b_cmd, argv_expanded[1]); |
| 6835 | rcode = x->b_function(argv_expanded) & 0xff; |
| 6836 | fflush_all(); |
| 6837 | } |
| 6838 | #if ENABLE_HUSH_FUNCTIONS |
| 6839 | else { |
| 6840 | # if ENABLE_HUSH_LOCAL |
| 6841 | struct variable **sv; |
| 6842 | sv = G.shadowed_vars_pp; |
| 6843 | G.shadowed_vars_pp = &old_vars; |
| 6844 | # endif |
| 6845 | debug_printf_exec(": function '%s' '%s'...\n", |
| 6846 | funcp->name, argv_expanded[1]); |
| 6847 | rcode = run_function(funcp, argv_expanded) & 0xff; |
| 6848 | # if ENABLE_HUSH_LOCAL |
| 6849 | G.shadowed_vars_pp = sv; |
| 6850 | # endif |
| 6851 | } |
| 6852 | #endif |
| 6853 | } |
| 6854 | clean_up_and_ret: |
| 6855 | unset_vars(new_env); |
| 6856 | add_vars(old_vars); |
| 6857 | /* clean_up_and_ret0: */ |
| 6858 | restore_redirects(squirrel); |
| 6859 | clean_up_and_ret1: |
| 6860 | free(argv_expanded); |
| 6861 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 6862 | debug_leave(); |
| 6863 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 6864 | return rcode; |
| 6865 | } |
| 6866 | |
| 6867 | if (ENABLE_FEATURE_SH_STANDALONE) { |
| 6868 | int n = find_applet_by_name(argv_expanded[0]); |
| 6869 | if (n >= 0 && APPLET_IS_NOFORK(n)) { |
| 6870 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
| 6871 | if (rcode == 0) { |
| 6872 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 6873 | argv_expanded[0], argv_expanded[1]); |
| 6874 | rcode = run_nofork_applet(n, argv_expanded); |
| 6875 | } |
| 6876 | goto clean_up_and_ret; |
| 6877 | } |
| 6878 | } |
| 6879 | /* It is neither builtin nor applet. We must fork. */ |
| 6880 | } |
| 6881 | |
| 6882 | must_fork: |
| 6883 | /* NB: argv_expanded may already be created, and that |
| 6884 | * might include `cmd` runs! Do not rerun it! We *must* |
| 6885 | * use argv_expanded if it's non-NULL */ |
| 6886 | |
| 6887 | /* Going to fork a child per each pipe member */ |
| 6888 | pi->alive_cmds = 0; |
| 6889 | next_infd = 0; |
| 6890 | |
| 6891 | cmd_no = 0; |
| 6892 | while (cmd_no < pi->num_cmds) { |
| 6893 | struct fd_pair pipefds; |
| 6894 | #if !BB_MMU |
| 6895 | volatile nommu_save_t nommu_save; |
| 6896 | nommu_save.new_env = NULL; |
| 6897 | nommu_save.old_vars = NULL; |
| 6898 | nommu_save.argv = NULL; |
| 6899 | nommu_save.argv_from_re_execing = NULL; |
| 6900 | #endif |
| 6901 | command = &pi->cmds[cmd_no]; |
| 6902 | cmd_no++; |
| 6903 | if (command->argv) { |
| 6904 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 6905 | command->argv[0], command->argv[1]); |
| 6906 | } else { |
| 6907 | debug_printf_exec(": pipe member with no argv\n"); |
| 6908 | } |
| 6909 | |
| 6910 | /* pipes are inserted between pairs of commands */ |
| 6911 | pipefds.rd = 0; |
| 6912 | pipefds.wr = 1; |
| 6913 | if (cmd_no < pi->num_cmds) |
| 6914 | xpiped_pair(pipefds); |
| 6915 | |
| 6916 | command->pid = BB_MMU ? fork() : vfork(); |
| 6917 | if (!command->pid) { /* child */ |
| 6918 | #if ENABLE_HUSH_JOB |
| 6919 | disable_restore_tty_pgrp_on_exit(); |
| 6920 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 6921 | |
| 6922 | /* Every child adds itself to new process group |
| 6923 | * with pgid == pid_of_first_child_in_pipe */ |
| 6924 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 6925 | pid_t pgrp; |
| 6926 | pgrp = pi->pgrp; |
| 6927 | if (pgrp < 0) /* true for 1st process only */ |
| 6928 | pgrp = getpid(); |
| 6929 | if (setpgid(0, pgrp) == 0 |
| 6930 | && pi->followup != PIPE_BG |
| 6931 | && G_saved_tty_pgrp /* we have ctty */ |
| 6932 | ) { |
| 6933 | /* We do it in *every* child, not just first, |
| 6934 | * to avoid races */ |
| 6935 | tcsetpgrp(G_interactive_fd, pgrp); |
| 6936 | } |
| 6937 | } |
| 6938 | #endif |
| 6939 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 6940 | /* 1st cmd in backgrounded pipe |
| 6941 | * should have its stdin /dev/null'ed */ |
| 6942 | close(0); |
| 6943 | if (open(bb_dev_null, O_RDONLY)) |
| 6944 | xopen("/", O_RDONLY); |
| 6945 | } else { |
| 6946 | xmove_fd(next_infd, 0); |
| 6947 | } |
| 6948 | xmove_fd(pipefds.wr, 1); |
| 6949 | if (pipefds.rd > 1) |
| 6950 | close(pipefds.rd); |
| 6951 | /* Like bash, explicit redirects override pipes, |
| 6952 | * and the pipe fd is available for dup'ing. */ |
| 6953 | if (setup_redirects(command, NULL)) |
| 6954 | _exit(1); |
| 6955 | |
| 6956 | /* Restore default handlers just prior to exec */ |
| 6957 | /*signal(SIGCHLD, SIG_DFL); - so far we don't have any handlers */ |
| 6958 | |
| 6959 | /* Stores to nommu_save list of env vars putenv'ed |
| 6960 | * (NOMMU, on MMU we don't need that) */ |
| 6961 | /* cast away volatility... */ |
| 6962 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 6963 | /* pseudo_exec() does not return */ |
| 6964 | } |
| 6965 | |
| 6966 | /* parent or error */ |
| 6967 | #if ENABLE_HUSH_FAST |
| 6968 | G.count_SIGCHLD++; |
| 6969 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6970 | #endif |
| 6971 | enable_restore_tty_pgrp_on_exit(); |
| 6972 | #if !BB_MMU |
| 6973 | /* Clean up after vforked child */ |
| 6974 | free(nommu_save.argv); |
| 6975 | free(nommu_save.argv_from_re_execing); |
| 6976 | unset_vars(nommu_save.new_env); |
| 6977 | add_vars(nommu_save.old_vars); |
| 6978 | #endif |
| 6979 | free(argv_expanded); |
| 6980 | argv_expanded = NULL; |
| 6981 | if (command->pid < 0) { /* [v]fork failed */ |
| 6982 | /* Clearly indicate, was it fork or vfork */ |
| 6983 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
| 6984 | } else { |
| 6985 | pi->alive_cmds++; |
| 6986 | #if ENABLE_HUSH_JOB |
| 6987 | /* Second and next children need to know pid of first one */ |
| 6988 | if (pi->pgrp < 0) |
| 6989 | pi->pgrp = command->pid; |
| 6990 | #endif |
| 6991 | } |
| 6992 | |
| 6993 | if (cmd_no > 1) |
| 6994 | close(next_infd); |
| 6995 | if (cmd_no < pi->num_cmds) |
| 6996 | close(pipefds.wr); |
| 6997 | /* Pass read (output) pipe end to next iteration */ |
| 6998 | next_infd = pipefds.rd; |
| 6999 | } |
| 7000 | |
| 7001 | if (!pi->alive_cmds) { |
| 7002 | debug_leave(); |
| 7003 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 7004 | return 1; |
| 7005 | } |
| 7006 | |
| 7007 | debug_leave(); |
| 7008 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 7009 | return -1; |
| 7010 | } |
| 7011 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7012 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 7013 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 7014 | static int run_list(struct pipe *pi) |
| 7015 | { |
| 7016 | #if ENABLE_HUSH_CASE |
| 7017 | char *case_word = NULL; |
| 7018 | #endif |
| 7019 | #if ENABLE_HUSH_LOOPS |
| 7020 | struct pipe *loop_top = NULL; |
| 7021 | char **for_lcur = NULL; |
| 7022 | char **for_list = NULL; |
| 7023 | #endif |
| 7024 | smallint last_followup; |
| 7025 | smalluint rcode; |
| 7026 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 7027 | smalluint cond_code = 0; |
| 7028 | #else |
| 7029 | enum { cond_code = 0 }; |
| 7030 | #endif |
| 7031 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 7032 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7033 | smallint last_rword; /* ditto */ |
| 7034 | #endif |
| 7035 | |
| 7036 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 7037 | debug_enter(); |
| 7038 | |
| 7039 | #if ENABLE_HUSH_LOOPS |
| 7040 | /* Check syntax for "for" */ |
Denys Vlasenko | 0d6a4ec | 2010-12-18 01:34:49 +0100 | [diff] [blame] | 7041 | { |
| 7042 | struct pipe *cpipe; |
| 7043 | for (cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 7044 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 7045 | continue; |
| 7046 | /* current word is FOR or IN (BOLD in comments below) */ |
| 7047 | if (cpipe->next == NULL) { |
| 7048 | syntax_error("malformed for"); |
| 7049 | debug_leave(); |
| 7050 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 7051 | return 1; |
| 7052 | } |
| 7053 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 7054 | if (cpipe->next->res_word == RES_DO) |
| 7055 | continue; |
| 7056 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 7057 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 7058 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 7059 | ) { |
| 7060 | syntax_error("malformed for"); |
| 7061 | debug_leave(); |
| 7062 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 7063 | return 1; |
| 7064 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7065 | } |
| 7066 | } |
| 7067 | #endif |
| 7068 | |
| 7069 | /* Past this point, all code paths should jump to ret: label |
| 7070 | * in order to return, no direct "return" statements please. |
| 7071 | * This helps to ensure that no memory is leaked. */ |
| 7072 | |
| 7073 | #if ENABLE_HUSH_JOB |
| 7074 | G.run_list_level++; |
| 7075 | #endif |
| 7076 | |
| 7077 | #if HAS_KEYWORDS |
| 7078 | rword = RES_NONE; |
| 7079 | last_rword = RES_XXXX; |
| 7080 | #endif |
| 7081 | last_followup = PIPE_SEQ; |
| 7082 | rcode = G.last_exitcode; |
| 7083 | |
| 7084 | /* Go through list of pipes, (maybe) executing them. */ |
| 7085 | for (; pi; pi = IF_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
| 7086 | if (G.flag_SIGINT) |
| 7087 | break; |
| 7088 | |
| 7089 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 7090 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 7091 | rword, cond_code, last_rword); |
| 7092 | #if ENABLE_HUSH_LOOPS |
| 7093 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 7094 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 7095 | ) { |
| 7096 | /* start of a loop: remember where loop starts */ |
| 7097 | loop_top = pi; |
| 7098 | G.depth_of_loop++; |
| 7099 | } |
| 7100 | #endif |
| 7101 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 7102 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 7103 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 7104 | || (rcode != 0 && last_followup == PIPE_AND) |
| 7105 | ) { |
| 7106 | /* It is "<true> || CMD" or "<false> && CMD" |
| 7107 | * and we should not execute CMD */ |
| 7108 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 7109 | last_followup = pi->followup; |
| 7110 | continue; |
| 7111 | } |
| 7112 | } |
| 7113 | last_followup = pi->followup; |
| 7114 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 7115 | #if ENABLE_HUSH_IF |
| 7116 | if (cond_code) { |
| 7117 | if (rword == RES_THEN) { |
| 7118 | /* if false; then ... fi has exitcode 0! */ |
| 7119 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7120 | /* "if <false> THEN cmd": skip cmd */ |
| 7121 | continue; |
| 7122 | } |
| 7123 | } else { |
| 7124 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 7125 | /* "if <true> then ... ELSE/ELIF cmd": |
| 7126 | * skip cmd and all following ones */ |
| 7127 | break; |
| 7128 | } |
| 7129 | } |
| 7130 | #endif |
| 7131 | #if ENABLE_HUSH_LOOPS |
| 7132 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 7133 | if (!for_lcur) { |
| 7134 | /* first loop through for */ |
| 7135 | |
| 7136 | static const char encoded_dollar_at[] ALIGN1 = { |
| 7137 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 7138 | }; /* encoded representation of "$@" */ |
| 7139 | static const char *const encoded_dollar_at_argv[] = { |
| 7140 | encoded_dollar_at, NULL |
| 7141 | }; /* argv list with one element: "$@" */ |
| 7142 | char **vals; |
| 7143 | |
| 7144 | vals = (char**)encoded_dollar_at_argv; |
| 7145 | if (pi->next->res_word == RES_IN) { |
| 7146 | /* if no variable values after "in" we skip "for" */ |
| 7147 | if (!pi->next->cmds[0].argv) { |
| 7148 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7149 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 7150 | break; |
| 7151 | } |
| 7152 | vals = pi->next->cmds[0].argv; |
| 7153 | } /* else: "for var; do..." -> assume "$@" list */ |
| 7154 | /* create list of variable values */ |
| 7155 | debug_print_strings("for_list made from", vals); |
| 7156 | for_list = expand_strvec_to_strvec(vals); |
| 7157 | for_lcur = for_list; |
| 7158 | debug_print_strings("for_list", for_list); |
| 7159 | } |
| 7160 | if (!*for_lcur) { |
| 7161 | /* "for" loop is over, clean up */ |
| 7162 | free(for_list); |
| 7163 | for_list = NULL; |
| 7164 | for_lcur = NULL; |
| 7165 | break; |
| 7166 | } |
| 7167 | /* Insert next value from for_lcur */ |
| 7168 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
| 7169 | set_local_var(xasprintf("%s=%s", pi->cmds[0].argv[0], *for_lcur++), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7170 | continue; |
| 7171 | } |
| 7172 | if (rword == RES_IN) { |
| 7173 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 7174 | } |
| 7175 | if (rword == RES_DONE) { |
| 7176 | continue; /* "done" has no cmds too */ |
| 7177 | } |
| 7178 | #endif |
| 7179 | #if ENABLE_HUSH_CASE |
| 7180 | if (rword == RES_CASE) { |
| 7181 | case_word = expand_strvec_to_string(pi->cmds->argv); |
| 7182 | continue; |
| 7183 | } |
| 7184 | if (rword == RES_MATCH) { |
| 7185 | char **argv; |
| 7186 | |
| 7187 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 7188 | break; |
| 7189 | /* all prev words didn't match, does this one match? */ |
| 7190 | argv = pi->cmds->argv; |
| 7191 | while (*argv) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7192 | char *pattern = expand_string_to_string(*argv, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7193 | /* TODO: which FNM_xxx flags to use? */ |
| 7194 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 7195 | free(pattern); |
| 7196 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 7197 | free(case_word); /* make future "word)" stop */ |
| 7198 | case_word = NULL; |
| 7199 | break; |
| 7200 | } |
| 7201 | argv++; |
| 7202 | } |
| 7203 | continue; |
| 7204 | } |
| 7205 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
| 7206 | if (cond_code != 0) |
| 7207 | continue; /* not matched yet, skip this pipe */ |
| 7208 | } |
| 7209 | #endif |
| 7210 | /* Just pressing <enter> in shell should check for jobs. |
| 7211 | * OTOH, in non-interactive shell this is useless |
| 7212 | * and only leads to extra job checks */ |
| 7213 | if (pi->num_cmds == 0) { |
| 7214 | if (G_interactive_fd) |
| 7215 | goto check_jobs_and_continue; |
| 7216 | continue; |
| 7217 | } |
| 7218 | |
| 7219 | /* After analyzing all keywords and conditions, we decided |
| 7220 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 7221 | * after run_pipe to collect any background children, |
| 7222 | * even if list execution is to be stopped. */ |
| 7223 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
| 7224 | { |
| 7225 | int r; |
| 7226 | #if ENABLE_HUSH_LOOPS |
| 7227 | G.flag_break_continue = 0; |
| 7228 | #endif |
| 7229 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 7230 | if (r != -1) { |
| 7231 | /* We ran a builtin, function, or group. |
| 7232 | * rcode is already known |
| 7233 | * and we don't need to wait for anything. */ |
| 7234 | G.last_exitcode = rcode; |
| 7235 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
| 7236 | check_and_run_traps(0); |
| 7237 | #if ENABLE_HUSH_LOOPS |
| 7238 | /* Was it "break" or "continue"? */ |
| 7239 | if (G.flag_break_continue) { |
| 7240 | smallint fbc = G.flag_break_continue; |
| 7241 | /* We might fall into outer *loop*, |
| 7242 | * don't want to break it too */ |
| 7243 | if (loop_top) { |
| 7244 | G.depth_break_continue--; |
| 7245 | if (G.depth_break_continue == 0) |
| 7246 | G.flag_break_continue = 0; |
| 7247 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 7248 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
| 7249 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
| 7250 | goto check_jobs_and_break; |
| 7251 | /* "continue": simulate end of loop */ |
| 7252 | rword = RES_DONE; |
| 7253 | continue; |
| 7254 | } |
| 7255 | #endif |
| 7256 | #if ENABLE_HUSH_FUNCTIONS |
| 7257 | if (G.flag_return_in_progress == 1) { |
| 7258 | /* same as "goto check_jobs_and_break" */ |
| 7259 | checkjobs(NULL); |
| 7260 | break; |
| 7261 | } |
| 7262 | #endif |
| 7263 | } else if (pi->followup == PIPE_BG) { |
| 7264 | /* What does bash do with attempts to background builtins? */ |
| 7265 | /* even bash 3.2 doesn't do that well with nested bg: |
| 7266 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 7267 | * I'm NOT treating inner &'s as jobs */ |
| 7268 | check_and_run_traps(0); |
| 7269 | #if ENABLE_HUSH_JOB |
| 7270 | if (G.run_list_level == 1) |
| 7271 | insert_bg_job(pi); |
| 7272 | #endif |
| 7273 | /* Last command's pid goes to $! */ |
| 7274 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
| 7275 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7276 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
| 7277 | } else { |
| 7278 | #if ENABLE_HUSH_JOB |
| 7279 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 7280 | /* Waits for completion, then fg's main shell */ |
| 7281 | rcode = checkjobs_and_fg_shell(pi); |
| 7282 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
| 7283 | check_and_run_traps(0); |
| 7284 | } else |
| 7285 | #endif |
| 7286 | { /* This one just waits for completion */ |
| 7287 | rcode = checkjobs(pi); |
| 7288 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
| 7289 | check_and_run_traps(0); |
| 7290 | } |
| 7291 | G.last_exitcode = rcode; |
| 7292 | } |
| 7293 | } |
| 7294 | |
| 7295 | /* Analyze how result affects subsequent commands */ |
| 7296 | #if ENABLE_HUSH_IF |
| 7297 | if (rword == RES_IF || rword == RES_ELIF) |
| 7298 | cond_code = rcode; |
| 7299 | #endif |
| 7300 | #if ENABLE_HUSH_LOOPS |
| 7301 | /* Beware of "while false; true; do ..."! */ |
| 7302 | if (pi->next && pi->next->res_word == RES_DO) { |
| 7303 | if (rword == RES_WHILE) { |
| 7304 | if (rcode) { |
| 7305 | /* "while false; do...done" - exitcode 0 */ |
| 7306 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7307 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
| 7308 | goto check_jobs_and_break; |
| 7309 | } |
| 7310 | } |
| 7311 | if (rword == RES_UNTIL) { |
| 7312 | if (!rcode) { |
| 7313 | debug_printf_exec(": until expr is true: breaking\n"); |
| 7314 | check_jobs_and_break: |
| 7315 | checkjobs(NULL); |
| 7316 | break; |
| 7317 | } |
| 7318 | } |
| 7319 | } |
| 7320 | #endif |
| 7321 | |
| 7322 | check_jobs_and_continue: |
| 7323 | checkjobs(NULL); |
| 7324 | } /* for (pi) */ |
| 7325 | |
| 7326 | #if ENABLE_HUSH_JOB |
| 7327 | G.run_list_level--; |
| 7328 | #endif |
| 7329 | #if ENABLE_HUSH_LOOPS |
| 7330 | if (loop_top) |
| 7331 | G.depth_of_loop--; |
| 7332 | free(for_list); |
| 7333 | #endif |
| 7334 | #if ENABLE_HUSH_CASE |
| 7335 | free(case_word); |
| 7336 | #endif |
| 7337 | debug_leave(); |
| 7338 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 7339 | return rcode; |
| 7340 | } |
| 7341 | |
| 7342 | /* Select which version we will use */ |
| 7343 | static int run_and_free_list(struct pipe *pi) |
| 7344 | { |
| 7345 | int rcode = 0; |
| 7346 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 7347 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7348 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 7349 | rcode = run_list(pi); |
| 7350 | } |
| 7351 | /* free_pipe_list has the side effect of clearing memory. |
| 7352 | * In the long run that function can be merged with run_list, |
| 7353 | * but doing that now would hobble the debugging effort. */ |
| 7354 | free_pipe_list(pi); |
| 7355 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 7356 | return rcode; |
| 7357 | } |
| 7358 | |
| 7359 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7360 | /* Called a few times only (or even once if "sh -c") */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7361 | static void init_sigmasks(void) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 7362 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7363 | unsigned sig; |
| 7364 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7365 | sigset_t old_blocked_set; |
| 7366 | |
| 7367 | if (!G.inherited_set_is_saved) { |
| 7368 | sigprocmask(SIG_SETMASK, NULL, &G.blocked_set); |
| 7369 | G.inherited_set = G.blocked_set; |
| 7370 | } |
| 7371 | old_blocked_set = G.blocked_set; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 7372 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7373 | mask = (1 << SIGQUIT); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7374 | if (G_interactive_fd) { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 7375 | mask = (1 << SIGQUIT) | SPECIAL_INTERACTIVE_SIGS; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7376 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7377 | mask |= SPECIAL_JOB_SIGS; |
| 7378 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7379 | G.non_DFL_mask = mask; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 7380 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7381 | sig = 0; |
| 7382 | while (mask) { |
| 7383 | if (mask & 1) |
| 7384 | sigaddset(&G.blocked_set, sig); |
| 7385 | mask >>= 1; |
| 7386 | sig++; |
| 7387 | } |
| 7388 | sigdelset(&G.blocked_set, SIGCHLD); |
| 7389 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7390 | if (memcmp(&old_blocked_set, &G.blocked_set, sizeof(old_blocked_set)) != 0) |
| 7391 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7392 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7393 | /* POSIX allows shell to re-enable SIGCHLD |
| 7394 | * even if it was SIG_IGN on entry */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 7395 | #if ENABLE_HUSH_FAST |
| 7396 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7397 | if (!G.inherited_set_is_saved) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 7398 | signal(SIGCHLD, SIGCHLD_handler); |
| 7399 | #else |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7400 | if (!G.inherited_set_is_saved) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 7401 | signal(SIGCHLD, SIG_DFL); |
| 7402 | #endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7403 | |
| 7404 | G.inherited_set_is_saved = 1; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7405 | } |
| 7406 | |
| 7407 | #if ENABLE_HUSH_JOB |
| 7408 | /* helper */ |
| 7409 | static void maybe_set_to_sigexit(int sig) |
| 7410 | { |
| 7411 | void (*handler)(int); |
| 7412 | /* non_DFL_mask'ed signals are, well, masked, |
| 7413 | * no need to set handler for them. |
| 7414 | */ |
| 7415 | if (!((G.non_DFL_mask >> sig) & 1)) { |
| 7416 | handler = signal(sig, sigexit); |
| 7417 | if (handler == SIG_IGN) /* oops... restore back to IGN! */ |
| 7418 | signal(sig, handler); |
| 7419 | } |
| 7420 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7421 | /* Set handlers to restore tty pgrp and exit */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7422 | static void set_fatal_handlers(void) |
| 7423 | { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 7424 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7425 | if (HUSH_DEBUG) { |
| 7426 | maybe_set_to_sigexit(SIGILL ); |
| 7427 | maybe_set_to_sigexit(SIGFPE ); |
| 7428 | maybe_set_to_sigexit(SIGBUS ); |
| 7429 | maybe_set_to_sigexit(SIGSEGV); |
| 7430 | maybe_set_to_sigexit(SIGTRAP); |
| 7431 | } /* else: hush is perfect. what SEGV? */ |
| 7432 | maybe_set_to_sigexit(SIGABRT); |
| 7433 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 7434 | maybe_set_to_sigexit(SIGPIPE); |
| 7435 | maybe_set_to_sigexit(SIGALRM); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 7436 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are masked. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7437 | * if we aren't interactive... but in this case |
| 7438 | * we never want to restore pgrp on exit, and this fn is not called */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 7439 | /*maybe_set_to_sigexit(SIGHUP );*/ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7440 | /*maybe_set_to_sigexit(SIGTERM);*/ |
| 7441 | /*maybe_set_to_sigexit(SIGINT );*/ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 7442 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 7443 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 7444 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7445 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7446 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7447 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7448 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7449 | case 'n': |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 7450 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7451 | break; |
| 7452 | case 'x': |
| 7453 | IF_HUSH_MODE_X(G_x_mode = state;) |
| 7454 | break; |
| 7455 | case 'o': |
| 7456 | if (!o_opt) { |
| 7457 | /* "set -+o" without parameter. |
| 7458 | * in bash, set -o produces this output: |
| 7459 | * pipefail off |
| 7460 | * and set +o: |
| 7461 | * set +o pipefail |
| 7462 | * We always use the second form. |
| 7463 | */ |
| 7464 | const char *p = o_opt_strings; |
| 7465 | idx = 0; |
| 7466 | while (*p) { |
| 7467 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 7468 | idx++; |
| 7469 | p += strlen(p) + 1; |
| 7470 | } |
| 7471 | break; |
| 7472 | } |
| 7473 | idx = index_in_strings(o_opt_strings, o_opt); |
| 7474 | if (idx >= 0) { |
| 7475 | G.o_opt[idx] = state; |
| 7476 | break; |
| 7477 | } |
| 7478 | default: |
| 7479 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7480 | } |
| 7481 | return EXIT_SUCCESS; |
| 7482 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7483 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 7484 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 7485 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7486 | { |
| 7487 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7488 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7489 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7490 | struct variable *cur_var; |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 7491 | struct variable *shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 7492 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 7493 | INIT_G(); |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 7494 | if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7495 | G.last_exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7496 | #if !BB_MMU |
| 7497 | G.argv0_for_re_execing = argv[0]; |
| 7498 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7499 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 7500 | shell_ver = xzalloc(sizeof(*shell_ver)); |
| 7501 | shell_ver->flg_export = 1; |
| 7502 | shell_ver->flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 7503 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 7504 | * therefore we xstrdup: */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 7505 | shell_ver->varstr = xstrdup(hush_version_str); |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 7506 | /* Create shell local variables from the values |
| 7507 | * currently living in the environment */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 7508 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7509 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 7510 | G.top_var = shell_ver; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7511 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7512 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7513 | if (e) while (*e) { |
| 7514 | char *value = strchr(*e, '='); |
| 7515 | if (value) { /* paranoia */ |
| 7516 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 7517 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 7518 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7519 | cur_var->max_len = strlen(*e); |
| 7520 | cur_var->flg_export = 1; |
| 7521 | } |
| 7522 | e++; |
| 7523 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 7524 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 7525 | debug_printf_env("putenv '%s'\n", shell_ver->varstr); |
| 7526 | putenv(shell_ver->varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7527 | |
| 7528 | /* Export PWD */ |
| 7529 | set_pwd_var(/*exp:*/ 1); |
| 7530 | /* bash also exports SHLVL and _, |
| 7531 | * and sets (but doesn't export) the following variables: |
| 7532 | * BASH=/bin/bash |
| 7533 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 7534 | * BASH_VERSION='3.2.0(1)-release' |
| 7535 | * HOSTTYPE=i386 |
| 7536 | * MACHTYPE=i386-pc-linux-gnu |
| 7537 | * OSTYPE=linux-gnu |
| 7538 | * HOSTNAME=<xxxxxxxxxx> |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7539 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7540 | * EUID=<NNNNN> |
| 7541 | * UID=<NNNNN> |
| 7542 | * GROUPS=() |
| 7543 | * LINES=<NNN> |
| 7544 | * COLUMNS=<NNN> |
| 7545 | * BASH_ARGC=() |
| 7546 | * BASH_ARGV=() |
| 7547 | * BASH_LINENO=() |
| 7548 | * BASH_SOURCE=() |
| 7549 | * DIRSTACK=() |
| 7550 | * PIPESTATUS=([0]="0") |
| 7551 | * HISTFILE=/<xxx>/.bash_history |
| 7552 | * HISTFILESIZE=500 |
| 7553 | * HISTSIZE=500 |
| 7554 | * MAILCHECK=60 |
| 7555 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 7556 | * SHELL=/bin/bash |
| 7557 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 7558 | * TERM=dumb |
| 7559 | * OPTERR=1 |
| 7560 | * OPTIND=1 |
| 7561 | * IFS=$' \t\n' |
| 7562 | * PS1='\s-\v\$ ' |
| 7563 | * PS2='> ' |
| 7564 | * PS4='+ ' |
| 7565 | */ |
| 7566 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 7567 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7568 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 7569 | # if defined MAX_HISTORY && MAX_HISTORY > 0 && ENABLE_HUSH_SAVEHISTORY |
| 7570 | { |
| 7571 | const char *hp = get_local_var_value("HISTFILE"); |
| 7572 | if (!hp) { |
| 7573 | hp = get_local_var_value("HOME"); |
| 7574 | if (hp) { |
| 7575 | G.line_input_state->hist_file = concat_path_file(hp, ".hush_history"); |
| 7576 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 7577 | } |
| 7578 | } |
| 7579 | } |
| 7580 | # endif |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 7581 | #endif |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 7582 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7583 | G.global_argc = argc; |
| 7584 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 7585 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 7586 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 7587 | |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 7588 | if (setjmp(die_jmp)) { |
| 7589 | /* xfunc has failed! die die die */ |
| 7590 | /* no EXIT traps, this is an escape hatch! */ |
| 7591 | G.exiting = 1; |
| 7592 | hush_exit(xfunc_error_retval); |
| 7593 | } |
| 7594 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7595 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7596 | * init_sigmasks() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7597 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7598 | * If we later decide that we are interactive, we run init_sigmasks() |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7599 | * in order to intercept (more) signals. |
| 7600 | */ |
| 7601 | |
| 7602 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 7603 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7604 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7605 | while (1) { |
Denys Vlasenko | a67a962 | 2009-08-20 03:38:58 +0200 | [diff] [blame] | 7606 | opt = getopt(argc, argv, "+c:xins" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7607 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 7608 | "<:$:R:V:" |
| 7609 | # if ENABLE_HUSH_FUNCTIONS |
| 7610 | "F:" |
| 7611 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7612 | #endif |
| 7613 | ); |
| 7614 | if (opt <= 0) |
| 7615 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7616 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7617 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7618 | /* Possibilities: |
| 7619 | * sh ... -c 'script' |
| 7620 | * sh ... -c 'script' ARG0 [ARG1...] |
| 7621 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7622 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7623 | * "" needs to be replaced with NULL |
| 7624 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7625 | * Note: the form without ARG0 never happens: |
| 7626 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7627 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7628 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7629 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7630 | G.root_ppid = getppid(); |
| 7631 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7632 | G.global_argv = argv + optind; |
| 7633 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7634 | if (builtin_argc) { |
| 7635 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 7636 | const struct built_in_command *x; |
| 7637 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7638 | init_sigmasks(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7639 | x = find_builtin(optarg); |
| 7640 | if (x) { /* paranoia */ |
| 7641 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 7642 | G.global_argv += builtin_argc; |
| 7643 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7644 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7645 | } |
| 7646 | goto final_return; |
| 7647 | } |
| 7648 | if (!G.global_argv[0]) { |
| 7649 | /* -c 'script' (no params): prevent empty $0 */ |
| 7650 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 7651 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 7652 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7653 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7654 | init_sigmasks(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7655 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7656 | goto final_return; |
| 7657 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 7658 | /* Well, we cannot just declare interactiveness, |
| 7659 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7660 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7661 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 7662 | case 's': |
| 7663 | /* "-s" means "read from stdin", but this is how we always |
| 7664 | * operate, so simply do nothing here. */ |
| 7665 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7666 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 7667 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 7668 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 7669 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7670 | case '$': { |
| 7671 | unsigned long long empty_trap_mask; |
| 7672 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7673 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 7674 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7675 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 7676 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7677 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 7678 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7679 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7680 | optarg++; |
| 7681 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7682 | optarg++; |
| 7683 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 7684 | if (empty_trap_mask != 0) { |
| 7685 | int sig; |
| 7686 | init_sigmasks(); |
| 7687 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 7688 | for (sig = 1; sig < NSIG; sig++) { |
| 7689 | if (empty_trap_mask & (1LL << sig)) { |
| 7690 | G.traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 7691 | sigaddset(&G.blocked_set, sig); |
| 7692 | } |
| 7693 | } |
| 7694 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7695 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7696 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7697 | optarg++; |
| 7698 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7699 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7700 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7701 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7702 | case 'R': |
| 7703 | case 'V': |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7704 | set_local_var(xstrdup(optarg), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ opt == 'R'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7705 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 7706 | # if ENABLE_HUSH_FUNCTIONS |
| 7707 | case 'F': { |
| 7708 | struct function *funcp = new_function(optarg); |
| 7709 | /* funcp->name is already set to optarg */ |
| 7710 | /* funcp->body is set to NULL. It's a special case. */ |
| 7711 | funcp->body_as_string = argv[optind]; |
| 7712 | optind++; |
| 7713 | break; |
| 7714 | } |
| 7715 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7716 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7717 | case 'n': |
| 7718 | case 'x': |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7719 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7720 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7721 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7722 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7723 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 7724 | " or: sh -c command [args]...\n\n"); |
| 7725 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7726 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7727 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7728 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7729 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7730 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7731 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7732 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7733 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7734 | G.root_ppid = getppid(); |
| 7735 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7736 | |
| 7737 | /* If we are login shell... */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7738 | if (argv[0] && argv[0][0] == '-') { |
| 7739 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7740 | debug_printf("sourcing /etc/profile\n"); |
| 7741 | input = fopen_for_read("/etc/profile"); |
| 7742 | if (input != NULL) { |
| 7743 | close_on_exec_on(fileno(input)); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7744 | init_sigmasks(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7745 | parse_and_run_file(input); |
| 7746 | fclose(input); |
| 7747 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7748 | /* bash: after sourcing /etc/profile, |
| 7749 | * tries to source (in the given order): |
| 7750 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 7751 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7752 | * bash also sources ~/.bash_logout on exit. |
| 7753 | * If called as sh, skips .bash_XXX files. |
| 7754 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7755 | } |
| 7756 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7757 | if (argv[optind]) { |
| 7758 | FILE *input; |
| 7759 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7760 | * "bash <script>" (which is never interactive (unless -i?)) |
| 7761 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7762 | * If called as sh, does the same but with $ENV. |
| 7763 | */ |
| 7764 | debug_printf("running script '%s'\n", argv[optind]); |
| 7765 | G.global_argv = argv + optind; |
| 7766 | G.global_argc = argc - optind; |
| 7767 | input = xfopen_for_read(argv[optind]); |
| 7768 | close_on_exec_on(fileno(input)); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7769 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7770 | parse_and_run_file(input); |
| 7771 | #if ENABLE_FEATURE_CLEAN_UP |
| 7772 | fclose(input); |
| 7773 | #endif |
| 7774 | goto final_return; |
| 7775 | } |
| 7776 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7777 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7778 | * NB: don't forget to (re)run init_sigmasks() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7779 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7780 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 7781 | /* A shell is interactive if the '-i' flag was given, |
| 7782 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 7783 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7784 | * no arguments remaining or the -s flag given |
| 7785 | * standard input is a terminal |
| 7786 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7787 | * Refer to Posix.2, the description of the 'sh' utility. |
| 7788 | */ |
| 7789 | #if ENABLE_HUSH_JOB |
| 7790 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7791 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 7792 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 7793 | if (G_saved_tty_pgrp < 0) |
| 7794 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7795 | |
| 7796 | /* try to dup stdin to high fd#, >= 255 */ |
| 7797 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 7798 | if (G_interactive_fd < 0) { |
| 7799 | /* try to dup to any fd */ |
| 7800 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7801 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7802 | /* give up */ |
| 7803 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7804 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 7805 | } |
| 7806 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7807 | // TODO: track & disallow any attempts of user |
| 7808 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7809 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7810 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7811 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7812 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7813 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7814 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7815 | /* If we were run as 'hush &', sleep until we are |
| 7816 | * in the foreground (tty pgrp == our pgrp). |
| 7817 | * If we get started under a job aware app (like bash), |
| 7818 | * make sure we are now in charge so we don't fight over |
| 7819 | * who gets the foreground */ |
| 7820 | while (1) { |
| 7821 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7822 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 7823 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7824 | break; |
| 7825 | /* send TTIN to ourself (should stop us) */ |
| 7826 | kill(- shell_pgrp, SIGTTIN); |
| 7827 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7828 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7829 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7830 | /* Block some signals */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7831 | init_sigmasks(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7832 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7833 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7834 | /* Set other signals to restore saved_tty_pgrp */ |
| 7835 | set_fatal_handlers(); |
| 7836 | /* Put ourselves in our own process group |
| 7837 | * (bash, too, does this only if ctty is available) */ |
| 7838 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 7839 | /* Grab control of the terminal */ |
| 7840 | tcsetpgrp(G_interactive_fd, getpid()); |
| 7841 | } |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 7842 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 7843 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 7844 | enable_restore_tty_pgrp_on_exit(); /* sets die_sleep = -1 */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7845 | } else { |
| 7846 | init_sigmasks(); |
| 7847 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7848 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7849 | /* No job control compiled in, only prompt/line editing */ |
| 7850 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7851 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 7852 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7853 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7854 | G_interactive_fd = dup(STDIN_FILENO); |
| 7855 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7856 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7857 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7858 | } |
| 7859 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7860 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7861 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7862 | } |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7863 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7864 | #else |
| 7865 | /* We have interactiveness code disabled */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7866 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7867 | #endif |
| 7868 | /* bash: |
| 7869 | * if interactive but not a login shell, sources ~/.bashrc |
| 7870 | * (--norc turns this off, --rcfile <file> overrides) |
| 7871 | */ |
| 7872 | |
| 7873 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 7874 | /* note: ash and hush share this string */ |
| 7875 | printf("\n\n%s %s\n" |
| 7876 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 7877 | "\n", |
| 7878 | bb_banner, |
| 7879 | "hush - the humble shell" |
| 7880 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 7881 | } |
| 7882 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7883 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7884 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7885 | final_return: |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7886 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7887 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 7888 | |
| 7889 | |
Denys Vlasenko | 1cc4b13 | 2009-08-21 00:05:51 +0200 | [diff] [blame] | 7890 | #if ENABLE_MSH |
| 7891 | int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 7892 | int msh_main(int argc, char **argv) |
| 7893 | { |
| 7894 | //bb_error_msg("msh is deprecated, please use hush instead"); |
| 7895 | return hush_main(argc, argv); |
| 7896 | } |
| 7897 | #endif |
| 7898 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7899 | |
| 7900 | /* |
| 7901 | * Built-ins |
| 7902 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7903 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7904 | { |
| 7905 | return 0; |
| 7906 | } |
| 7907 | |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 7908 | 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] | 7909 | { |
| 7910 | int argc = 0; |
| 7911 | while (*argv) { |
| 7912 | argc++; |
| 7913 | argv++; |
| 7914 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 7915 | return applet_main_func(argc, argv - argc); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 7916 | } |
| 7917 | |
| 7918 | static int FAST_FUNC builtin_test(char **argv) |
| 7919 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7920 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7921 | } |
| 7922 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7923 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7924 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7925 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7926 | } |
| 7927 | |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 7928 | #if ENABLE_PRINTF |
| 7929 | static int FAST_FUNC builtin_printf(char **argv) |
| 7930 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7931 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 7932 | } |
| 7933 | #endif |
| 7934 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7935 | static char **skip_dash_dash(char **argv) |
| 7936 | { |
| 7937 | argv++; |
| 7938 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 7939 | argv++; |
| 7940 | return argv; |
| 7941 | } |
| 7942 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7943 | static int FAST_FUNC builtin_eval(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7944 | { |
| 7945 | int rcode = EXIT_SUCCESS; |
| 7946 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7947 | argv = skip_dash_dash(argv); |
| 7948 | if (*argv) { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7949 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7950 | /* bash: |
| 7951 | * eval "echo Hi; done" ("done" is syntax error): |
| 7952 | * "echo Hi" will not execute too. |
| 7953 | */ |
| 7954 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7955 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7956 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7957 | } |
| 7958 | return rcode; |
| 7959 | } |
| 7960 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7961 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7962 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7963 | const char *newdir; |
| 7964 | |
| 7965 | argv = skip_dash_dash(argv); |
| 7966 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7967 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7968 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7969 | * bash says "bash: cd: HOME not set" and does nothing |
| 7970 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7971 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 7972 | const char *home = get_local_var_value("HOME"); |
| 7973 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7974 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7975 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7976 | /* Mimic bash message exactly */ |
| 7977 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7978 | return EXIT_FAILURE; |
| 7979 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7980 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 7981 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 7982 | * set it again, but do not export. bash does the same. |
| 7983 | */ |
| 7984 | set_pwd_var(/*exp:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7985 | return EXIT_SUCCESS; |
| 7986 | } |
| 7987 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7988 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7989 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7990 | argv = skip_dash_dash(argv); |
| 7991 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7992 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 7993 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 7994 | /* Careful: we can end up here after [v]fork. Do not restore |
| 7995 | * tty pgrp then, only top-level shell process does that */ |
| 7996 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 7997 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 7998 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 7999 | /* TODO: if exec fails, bash does NOT exit! We do. |
| 8000 | * We'll need to undo sigprocmask (it's inside execvp_or_die) |
| 8001 | * and tcsetpgrp, and this is inherently racy. |
| 8002 | */ |
| 8003 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8004 | } |
| 8005 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8006 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8007 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 8008 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8009 | |
| 8010 | /* interactive bash: |
| 8011 | * # trap "echo EEE" EXIT |
| 8012 | * # exit |
| 8013 | * exit |
| 8014 | * There are stopped jobs. |
| 8015 | * (if there are _stopped_ jobs, running ones don't count) |
| 8016 | * # exit |
| 8017 | * exit |
| 8018 | # EEE (then bash exits) |
| 8019 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 8020 | * 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] | 8021 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 8022 | |
| 8023 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8024 | argv = skip_dash_dash(argv); |
| 8025 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8026 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8027 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 8028 | xfunc_error_retval = 255; |
| 8029 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8030 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8031 | } |
| 8032 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8033 | static void print_escaped(const char *s) |
| 8034 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8035 | if (*s == '\'') |
| 8036 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8037 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8038 | const char *p = strchrnul(s, '\''); |
| 8039 | /* print 'xxxx', possibly just '' */ |
| 8040 | printf("'%.*s'", (int)(p - s), s); |
| 8041 | if (*p == '\0') |
| 8042 | break; |
| 8043 | s = p; |
| 8044 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8045 | /* s points to '; print "'''...'''" */ |
| 8046 | putchar('"'); |
| 8047 | do putchar('\''); while (*++s == '\''); |
| 8048 | putchar('"'); |
| 8049 | } while (*s); |
| 8050 | } |
| 8051 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8052 | #if !ENABLE_HUSH_LOCAL |
| 8053 | #define helper_export_local(argv, exp, lvl) \ |
| 8054 | helper_export_local(argv, exp) |
| 8055 | #endif |
| 8056 | static void helper_export_local(char **argv, int exp, int lvl) |
| 8057 | { |
| 8058 | do { |
| 8059 | char *name = *argv; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8060 | char *name_end = strchrnul(name, '='); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8061 | |
| 8062 | /* So far we do not check that name is valid (TODO?) */ |
| 8063 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8064 | if (*name_end == '\0') { |
| 8065 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8066 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8067 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 8068 | var = vpp ? *vpp : NULL; |
| 8069 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8070 | if (exp == -1) { /* unexporting? */ |
| 8071 | /* export -n NAME (without =VALUE) */ |
| 8072 | if (var) { |
| 8073 | var->flg_export = 0; |
| 8074 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 8075 | unsetenv(name); |
| 8076 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 8077 | continue; |
| 8078 | } |
| 8079 | if (exp == 1) { /* exporting? */ |
| 8080 | /* export NAME (without =VALUE) */ |
| 8081 | if (var) { |
| 8082 | var->flg_export = 1; |
| 8083 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 8084 | putenv(var->varstr); |
| 8085 | continue; |
| 8086 | } |
| 8087 | } |
| 8088 | /* Exporting non-existing variable. |
| 8089 | * bash does not put it in environment, |
| 8090 | * but remembers that it is exported, |
| 8091 | * and does put it in env when it is set later. |
| 8092 | * We just set it to "" and export. */ |
| 8093 | /* Or, it's "local NAME" (without =VALUE). |
| 8094 | * bash sets the value to "". */ |
| 8095 | name = xasprintf("%s=", name); |
| 8096 | } else { |
| 8097 | /* (Un)exporting/making local NAME=VALUE */ |
| 8098 | name = xstrdup(name); |
| 8099 | } |
| 8100 | set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0); |
| 8101 | } while (*++argv); |
| 8102 | } |
| 8103 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8104 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8105 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 8106 | unsigned opt_unexport; |
| 8107 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 8108 | #if ENABLE_HUSH_EXPORT_N |
| 8109 | /* "!": do not abort on errors */ |
| 8110 | opt_unexport = getopt32(argv, "!n"); |
| 8111 | if (opt_unexport == (uint32_t)-1) |
| 8112 | return EXIT_FAILURE; |
| 8113 | argv += optind; |
| 8114 | #else |
| 8115 | opt_unexport = 0; |
| 8116 | argv++; |
| 8117 | #endif |
| 8118 | |
| 8119 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8120 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8121 | if (e) { |
| 8122 | while (*e) { |
| 8123 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8124 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8125 | #else |
| 8126 | /* ash emits: export VAR='VAL' |
| 8127 | * bash: declare -x VAR="VAL" |
| 8128 | * we follow ash example */ |
| 8129 | const char *s = *e++; |
| 8130 | const char *p = strchr(s, '='); |
| 8131 | |
| 8132 | if (!p) /* wtf? take next variable */ |
| 8133 | continue; |
| 8134 | /* export var= */ |
| 8135 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8136 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8137 | putchar('\n'); |
| 8138 | #endif |
| 8139 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8140 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8141 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8142 | return EXIT_SUCCESS; |
| 8143 | } |
| 8144 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8145 | helper_export_local(argv, (opt_unexport ? -1 : 1), 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8146 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8147 | return EXIT_SUCCESS; |
| 8148 | } |
| 8149 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8150 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8151 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8152 | { |
| 8153 | if (G.func_nest_level == 0) { |
| 8154 | bb_error_msg("%s: not in a function", argv[0]); |
| 8155 | return EXIT_FAILURE; /* bash compat */ |
| 8156 | } |
| 8157 | helper_export_local(argv, 0, G.func_nest_level); |
| 8158 | return EXIT_SUCCESS; |
| 8159 | } |
| 8160 | #endif |
| 8161 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8162 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8163 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8164 | int sig; |
| 8165 | char *new_cmd; |
| 8166 | |
| 8167 | if (!G.traps) |
| 8168 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 8169 | |
| 8170 | argv++; |
| 8171 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 8172 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8173 | /* No args: print all trapped */ |
| 8174 | for (i = 0; i < NSIG; ++i) { |
| 8175 | if (G.traps[i]) { |
| 8176 | printf("trap -- "); |
| 8177 | print_escaped(G.traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 8178 | /* note: bash adds "SIG", but only if invoked |
| 8179 | * as "bash". If called as "sh", or if set -o posix, |
| 8180 | * then it prints short signal names. |
| 8181 | * We are printing short names: */ |
| 8182 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8183 | } |
| 8184 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8185 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8186 | return EXIT_SUCCESS; |
| 8187 | } |
| 8188 | |
| 8189 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8190 | /* If first arg is a number: reset all specified signals */ |
| 8191 | sig = bb_strtou(*argv, NULL, 10); |
| 8192 | if (errno == 0) { |
| 8193 | int ret; |
| 8194 | process_sig_list: |
| 8195 | ret = EXIT_SUCCESS; |
| 8196 | while (*argv) { |
| 8197 | sig = get_signum(*argv++); |
| 8198 | if (sig < 0 || sig >= NSIG) { |
| 8199 | ret = EXIT_FAILURE; |
| 8200 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 8201 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8202 | continue; |
| 8203 | } |
| 8204 | |
| 8205 | free(G.traps[sig]); |
| 8206 | G.traps[sig] = xstrdup(new_cmd); |
| 8207 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8208 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8209 | get_signame(sig), sig, G.traps[sig]); |
| 8210 | |
| 8211 | /* There is no signal for 0 (EXIT) */ |
| 8212 | if (sig == 0) |
| 8213 | continue; |
| 8214 | |
| 8215 | if (new_cmd) { |
| 8216 | sigaddset(&G.blocked_set, sig); |
| 8217 | } else { |
| 8218 | /* There was a trap handler, we are removing it |
| 8219 | * (if sig has non-DFL handling, |
| 8220 | * we don't need to do anything) */ |
| 8221 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 8222 | continue; |
| 8223 | sigdelset(&G.blocked_set, sig); |
| 8224 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8225 | } |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 8226 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8227 | return ret; |
| 8228 | } |
| 8229 | |
| 8230 | if (!argv[1]) { /* no second arg */ |
| 8231 | bb_error_msg("trap: invalid arguments"); |
| 8232 | return EXIT_FAILURE; |
| 8233 | } |
| 8234 | |
| 8235 | /* First arg is "-": reset all specified to default */ |
| 8236 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 8237 | /* Everything else: set arg as signal handler |
| 8238 | * (includes "" case, which ignores signal) */ |
| 8239 | if (argv[0][0] == '-') { |
| 8240 | if (argv[0][1] == '\0') { /* "-" */ |
| 8241 | /* new_cmd remains NULL: "reset these sigs" */ |
| 8242 | goto reset_traps; |
| 8243 | } |
| 8244 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 8245 | argv++; |
| 8246 | } |
| 8247 | /* else: "-something", no special meaning */ |
| 8248 | } |
| 8249 | new_cmd = *argv; |
| 8250 | reset_traps: |
| 8251 | argv++; |
| 8252 | goto process_sig_list; |
| 8253 | } |
| 8254 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8255 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8256 | static int FAST_FUNC builtin_type(char **argv) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8257 | { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8258 | int ret = EXIT_SUCCESS; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8259 | |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8260 | while (*++argv) { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8261 | const char *type; |
Denys Vlasenko | 171932d | 2009-05-28 17:07:22 +0200 | [diff] [blame] | 8262 | char *path = NULL; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8263 | |
| 8264 | if (0) {} /* make conditional compile easier below */ |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8265 | /*else if (find_alias(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8266 | type = "an alias";*/ |
| 8267 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8268 | else if (find_function(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8269 | type = "a function"; |
| 8270 | #endif |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8271 | else if (find_builtin(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8272 | type = "a shell builtin"; |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8273 | else if ((path = find_in_path(*argv)) != NULL) |
| 8274 | type = path; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 8275 | else { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8276 | bb_error_msg("type: %s: not found", *argv); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8277 | ret = EXIT_FAILURE; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 8278 | continue; |
| 8279 | } |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8280 | |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 8281 | printf("%s is %s\n", *argv, type); |
| 8282 | free(path); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8283 | } |
| 8284 | |
| 8285 | return ret; |
| 8286 | } |
| 8287 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8288 | #if ENABLE_HUSH_JOB |
| 8289 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8290 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8291 | { |
| 8292 | int i, jobnum; |
| 8293 | struct pipe *pi; |
| 8294 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8295 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8296 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8297 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8298 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 8299 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8300 | for (pi = G.job_list; pi; pi = pi->next) { |
| 8301 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8302 | goto found; |
| 8303 | } |
| 8304 | } |
| 8305 | bb_error_msg("%s: no current job", argv[0]); |
| 8306 | return EXIT_FAILURE; |
| 8307 | } |
| 8308 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 8309 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 8310 | return EXIT_FAILURE; |
| 8311 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8312 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8313 | if (pi->jobid == jobnum) { |
| 8314 | goto found; |
| 8315 | } |
| 8316 | } |
| 8317 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 8318 | return EXIT_FAILURE; |
| 8319 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 8320 | /* TODO: bash prints a string representation |
| 8321 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8322 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8323 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8324 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8325 | } |
| 8326 | |
| 8327 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8328 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 8329 | for (i = 0; i < pi->num_cmds; i++) { |
| 8330 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 8331 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8332 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8333 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8334 | |
| 8335 | i = kill(- pi->pgrp, SIGCONT); |
| 8336 | if (i < 0) { |
| 8337 | if (errno == ESRCH) { |
| 8338 | delete_finished_bg_job(pi); |
| 8339 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8340 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8341 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8342 | } |
| 8343 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8344 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8345 | remove_bg_job(pi); |
| 8346 | return checkjobs_and_fg_shell(pi); |
| 8347 | } |
| 8348 | return EXIT_SUCCESS; |
| 8349 | } |
| 8350 | #endif |
| 8351 | |
| 8352 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8353 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8354 | { |
| 8355 | const struct built_in_command *x; |
| 8356 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8357 | printf( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8358 | "Built-in commands:\n" |
| 8359 | "------------------\n"); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8360 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8361 | if (x->b_descr) |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8362 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8363 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8364 | bb_putchar('\n'); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8365 | return EXIT_SUCCESS; |
| 8366 | } |
| 8367 | #endif |
| 8368 | |
| 8369 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8370 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8371 | { |
| 8372 | struct pipe *job; |
| 8373 | const char *status_string; |
| 8374 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8375 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8376 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8377 | status_string = "Stopped"; |
| 8378 | else |
| 8379 | status_string = "Running"; |
| 8380 | |
| 8381 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 8382 | } |
| 8383 | return EXIT_SUCCESS; |
| 8384 | } |
| 8385 | #endif |
| 8386 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8387 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8388 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8389 | { |
| 8390 | void *p; |
| 8391 | unsigned long l; |
| 8392 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8393 | # ifdef M_TRIM_THRESHOLD |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 8394 | /* Optional. Reduces probability of false positives */ |
| 8395 | malloc_trim(0); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8396 | # endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8397 | /* Crude attempt to find where "free memory" starts, |
| 8398 | * sans fragmentation. */ |
| 8399 | p = malloc(240); |
| 8400 | l = (unsigned long)p; |
| 8401 | free(p); |
| 8402 | p = malloc(3400); |
| 8403 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 8404 | free(p); |
| 8405 | |
| 8406 | if (!G.memleak_value) |
| 8407 | G.memleak_value = l; |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 8408 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8409 | l -= G.memleak_value; |
| 8410 | if ((long)l < 0) |
| 8411 | l = 0; |
| 8412 | l /= 1024; |
| 8413 | if (l > 127) |
| 8414 | l = 127; |
| 8415 | |
| 8416 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 8417 | return l; |
| 8418 | } |
| 8419 | #endif |
| 8420 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8421 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8422 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8423 | puts(get_cwd(0)); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8424 | return EXIT_SUCCESS; |
| 8425 | } |
| 8426 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8427 | static int FAST_FUNC builtin_read(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8428 | { |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8429 | const char *r; |
| 8430 | char *opt_n = NULL; |
| 8431 | char *opt_p = NULL; |
| 8432 | char *opt_t = NULL; |
| 8433 | char *opt_u = NULL; |
| 8434 | int read_flags; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 8435 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8436 | /* "!": do not abort on errors. |
| 8437 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 8438 | */ |
| 8439 | read_flags = getopt32(argv, "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u); |
| 8440 | if (read_flags == (uint32_t)-1) |
| 8441 | return EXIT_FAILURE; |
| 8442 | argv += optind; |
| 8443 | |
| 8444 | r = shell_builtin_read(set_local_var_from_halves, |
| 8445 | argv, |
| 8446 | get_local_var_value("IFS"), /* can be NULL */ |
| 8447 | read_flags, |
| 8448 | opt_n, |
| 8449 | opt_p, |
| 8450 | opt_t, |
| 8451 | opt_u |
| 8452 | ); |
| 8453 | |
| 8454 | if ((uintptr_t)r > 1) { |
| 8455 | bb_error_msg("%s", r); |
| 8456 | r = (char*)(uintptr_t)1; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 8457 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8458 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8459 | return (uintptr_t)r; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8460 | } |
| 8461 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8462 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 8463 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8464 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8465 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 8466 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8467 | * set -- [argument...] |
| 8468 | * set -o |
| 8469 | * set +o |
| 8470 | * Implementations shall support the options in both their hyphen and |
| 8471 | * plus-sign forms. These options can also be specified as options to sh. |
| 8472 | * Examples: |
| 8473 | * Write out all variables and their values: set |
| 8474 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 8475 | * Turn on the -x and -v options: set -xv |
| 8476 | * Unset all positional parameters: set -- |
| 8477 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 8478 | * Set the positional parameters to the expansion of x, even if x expands |
| 8479 | * with a leading '-' or '+': set -- $x |
| 8480 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8481 | * So far, we only support "set -- [argument...]" and some of the short names. |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8482 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8483 | static int FAST_FUNC builtin_set(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8484 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8485 | int n; |
| 8486 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8487 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8488 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8489 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8490 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8491 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8492 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8493 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8494 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8495 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8496 | do { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8497 | if (strcmp(arg, "--") == 0) { |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8498 | ++argv; |
| 8499 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8500 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 8501 | if (arg[0] != '+' && arg[0] != '-') |
| 8502 | break; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8503 | for (n = 1; arg[n]; ++n) { |
| 8504 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 8505 | goto error; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8506 | if (arg[n] == 'o' && argv[1]) |
| 8507 | argv++; |
| 8508 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8509 | } while ((arg = *++argv) != NULL); |
| 8510 | /* Now argv[0] is 1st argument */ |
| 8511 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8512 | if (arg == NULL) |
| 8513 | return EXIT_SUCCESS; |
| 8514 | set_argv: |
| 8515 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8516 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 8517 | g_argv = G.global_argv; |
| 8518 | if (G.global_args_malloced) { |
| 8519 | pp = g_argv; |
| 8520 | while (*++pp) |
| 8521 | free(*pp); |
| 8522 | g_argv[1] = NULL; |
| 8523 | } else { |
| 8524 | G.global_args_malloced = 1; |
| 8525 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 8526 | pp[0] = g_argv[0]; /* retain $0 */ |
| 8527 | g_argv = pp; |
| 8528 | } |
| 8529 | /* This realloc's G.global_argv */ |
| 8530 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 8531 | |
| 8532 | n = 1; |
| 8533 | while (*++pp) |
| 8534 | n++; |
| 8535 | G.global_argc = n; |
| 8536 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8537 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8538 | |
| 8539 | /* Nothing known, so abort */ |
| 8540 | error: |
| 8541 | bb_error_msg("set: %s: invalid option", arg); |
| 8542 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8543 | } |
| 8544 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8545 | static int FAST_FUNC builtin_shift(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8546 | { |
| 8547 | int n = 1; |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8548 | argv = skip_dash_dash(argv); |
| 8549 | if (argv[0]) { |
| 8550 | n = atoi(argv[0]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8551 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8552 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 8553 | if (G.global_args_malloced) { |
| 8554 | int m = 1; |
| 8555 | while (m <= n) |
| 8556 | free(G.global_argv[m++]); |
| 8557 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8558 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 8559 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 8560 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8561 | return EXIT_SUCCESS; |
| 8562 | } |
| 8563 | return EXIT_FAILURE; |
| 8564 | } |
| 8565 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8566 | static int FAST_FUNC builtin_source(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8567 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8568 | char *arg_path, *filename; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8569 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8570 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8571 | #if ENABLE_HUSH_FUNCTIONS |
| 8572 | smallint sv_flg; |
| 8573 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8574 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8575 | argv = skip_dash_dash(argv); |
| 8576 | filename = argv[0]; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8577 | if (!filename) { |
| 8578 | /* bash says: "bash: .: filename argument required" */ |
| 8579 | return 2; /* bash compat */ |
| 8580 | } |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8581 | arg_path = NULL; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8582 | if (!strchr(filename, '/')) { |
| 8583 | arg_path = find_in_path(filename); |
| 8584 | if (arg_path) |
| 8585 | filename = arg_path; |
| 8586 | } |
| 8587 | input = fopen_or_warn(filename, "r"); |
| 8588 | free(arg_path); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8589 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8590 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8591 | return EXIT_FAILURE; |
| 8592 | } |
| 8593 | close_on_exec_on(fileno(input)); |
| 8594 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8595 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8596 | sv_flg = G.flag_return_in_progress; |
| 8597 | /* "we are inside sourced file, ok to use return" */ |
| 8598 | G.flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8599 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8600 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8601 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8602 | parse_and_run_file(input); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8603 | fclose(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8604 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8605 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8606 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8607 | G.flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8608 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8609 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8610 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8611 | } |
| 8612 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8613 | static int FAST_FUNC builtin_umask(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8614 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8615 | int rc; |
| 8616 | mode_t mask; |
| 8617 | |
| 8618 | mask = umask(0); |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8619 | argv = skip_dash_dash(argv); |
| 8620 | if (argv[0]) { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8621 | mode_t old_mask = mask; |
| 8622 | |
| 8623 | mask ^= 0777; |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8624 | rc = bb_parse_mode(argv[0], &mask); |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8625 | mask ^= 0777; |
| 8626 | if (rc == 0) { |
| 8627 | mask = old_mask; |
| 8628 | /* bash messages: |
| 8629 | * bash: umask: 'q': invalid symbolic mode operator |
| 8630 | * bash: umask: 999: octal number out of range |
| 8631 | */ |
Denys Vlasenko | 44c86ce | 2010-05-20 04:22:55 +0200 | [diff] [blame] | 8632 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8633 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8634 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8635 | rc = 1; |
| 8636 | /* Mimic bash */ |
| 8637 | printf("%04o\n", (unsigned) mask); |
| 8638 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8639 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8640 | umask(mask); |
| 8641 | |
| 8642 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8643 | } |
| 8644 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8645 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8646 | static int FAST_FUNC builtin_unset(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8647 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8648 | int ret; |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8649 | unsigned opts; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8650 | |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8651 | /* "!": do not abort on errors */ |
| 8652 | /* "+": stop at 1st non-option */ |
| 8653 | opts = getopt32(argv, "!+vf"); |
| 8654 | if (opts == (unsigned)-1) |
| 8655 | return EXIT_FAILURE; |
| 8656 | if (opts == 3) { |
| 8657 | bb_error_msg("unset: -v and -f are exclusive"); |
| 8658 | return EXIT_FAILURE; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8659 | } |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8660 | argv += optind; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8661 | |
| 8662 | ret = EXIT_SUCCESS; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8663 | while (*argv) { |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8664 | if (!(opts & 2)) { /* not -f */ |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8665 | if (unset_local_var(*argv)) { |
| 8666 | /* unset <nonexistent_var> doesn't fail. |
| 8667 | * Error is when one tries to unset RO var. |
| 8668 | * Message was printed by unset_local_var. */ |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8669 | ret = EXIT_FAILURE; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8670 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8671 | } |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8672 | #if ENABLE_HUSH_FUNCTIONS |
| 8673 | else { |
| 8674 | unset_func(*argv); |
| 8675 | } |
| 8676 | #endif |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8677 | argv++; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8678 | } |
| 8679 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8680 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8681 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8682 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8683 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8684 | { |
| 8685 | int ret = EXIT_SUCCESS; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8686 | int status, sig; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8687 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8688 | argv = skip_dash_dash(argv); |
| 8689 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8690 | /* Don't care about wait results */ |
| 8691 | /* Note 1: must wait until there are no more children */ |
| 8692 | /* Note 2: must be interruptible */ |
| 8693 | /* Examples: |
| 8694 | * $ sleep 3 & sleep 6 & wait |
| 8695 | * [1] 30934 sleep 3 |
| 8696 | * [2] 30935 sleep 6 |
| 8697 | * [1] Done sleep 3 |
| 8698 | * [2] Done sleep 6 |
| 8699 | * $ sleep 3 & sleep 6 & wait |
| 8700 | * [1] 30936 sleep 3 |
| 8701 | * [2] 30937 sleep 6 |
| 8702 | * [1] Done sleep 3 |
| 8703 | * ^C <-- after ~4 sec from keyboard |
| 8704 | * $ |
| 8705 | */ |
| 8706 | sigaddset(&G.blocked_set, SIGCHLD); |
| 8707 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 8708 | while (1) { |
| 8709 | checkjobs(NULL); |
| 8710 | if (errno == ECHILD) |
| 8711 | break; |
| 8712 | /* Wait for SIGCHLD or any other signal of interest */ |
| 8713 | /* sigtimedwait with infinite timeout: */ |
| 8714 | sig = sigwaitinfo(&G.blocked_set, NULL); |
| 8715 | if (sig > 0) { |
| 8716 | sig = check_and_run_traps(sig); |
| 8717 | if (sig && sig != SIGCHLD) { /* see note 2 */ |
| 8718 | ret = 128 + sig; |
| 8719 | break; |
| 8720 | } |
| 8721 | } |
| 8722 | } |
| 8723 | sigdelset(&G.blocked_set, SIGCHLD); |
| 8724 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 8725 | return ret; |
| 8726 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8727 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8728 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8729 | while (*argv) { |
| 8730 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 8731 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8732 | /* mimic bash message */ |
| 8733 | 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] | 8734 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8735 | } |
| 8736 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8737 | if (WIFSIGNALED(status)) |
| 8738 | ret = 128 + WTERMSIG(status); |
| 8739 | else if (WIFEXITED(status)) |
| 8740 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8741 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8742 | ret = EXIT_FAILURE; |
| 8743 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8744 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8745 | ret = 127; |
| 8746 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8747 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8748 | } |
| 8749 | |
| 8750 | return ret; |
| 8751 | } |
| 8752 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8753 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 8754 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 8755 | { |
| 8756 | if (argv[1]) { |
| 8757 | def = bb_strtou(argv[1], NULL, 10); |
| 8758 | if (errno || def < def_min || argv[2]) { |
| 8759 | bb_error_msg("%s: bad arguments", argv[0]); |
| 8760 | def = UINT_MAX; |
| 8761 | } |
| 8762 | } |
| 8763 | return def; |
| 8764 | } |
| 8765 | #endif |
| 8766 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 8767 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8768 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8769 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8770 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8771 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 8772 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 8773 | return EXIT_SUCCESS; /* bash compat */ |
| 8774 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8775 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8776 | |
| 8777 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 8778 | if (depth == UINT_MAX) |
| 8779 | G.flag_break_continue = BC_BREAK; |
| 8780 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8781 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8782 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8783 | return EXIT_SUCCESS; |
| 8784 | } |
| 8785 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8786 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8787 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 8788 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 8789 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8790 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 8791 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8792 | |
| 8793 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8794 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8795 | { |
| 8796 | int rc; |
| 8797 | |
| 8798 | if (G.flag_return_in_progress != -1) { |
| 8799 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 8800 | return EXIT_FAILURE; /* bash compat */ |
| 8801 | } |
| 8802 | |
| 8803 | G.flag_return_in_progress = 1; |
| 8804 | |
| 8805 | /* bash: |
| 8806 | * out of range: wraps around at 256, does not error out |
| 8807 | * non-numeric param: |
| 8808 | * f() { false; return qwe; }; f; echo $? |
| 8809 | * bash: return: qwe: numeric argument required <== we do this |
| 8810 | * 255 <== we also do this |
| 8811 | */ |
| 8812 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 8813 | return rc; |
| 8814 | } |
| 8815 | #endif |