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 | * |
| 11 | * Credits: |
| 12 | * The parser routines proper are all original material, first |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 13 | * written Dec 2000 and Jan 2001 by Larry Doolittle. The |
| 14 | * execution engine, the builtins, and much of the underlying |
| 15 | * support has been adapted from busybox-0.49pre's lash, which is |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 16 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 17 | * written by Erik Andersen <andersen@codepoet.org>. That, in turn, |
| 18 | * is based in part on ladsh.c, by Michael K. Johnson and Erik W. |
| 19 | * Troan, which they placed in the public domain. I don't know |
| 20 | * how much of the Johnson/Troan code has survived the repeated |
| 21 | * rewrites. |
| 22 | * |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 23 | * Other credits: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 24 | * o_addchr derived from similar w_addchar function in glibc-2.2. |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 25 | * parse_redirect, redirect_opt_num, and big chunks of main |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 26 | * and many builtins derived from contributions by Erik Andersen. |
| 27 | * Miscellaneous bugfixes from Matt Kraai. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 28 | * |
| 29 | * There are two big (and related) architecture differences between |
| 30 | * this parser and the lash parser. One is that this version is |
| 31 | * actually designed from the ground up to understand nearly all |
| 32 | * of the Bourne grammar. The second, consequential change is that |
| 33 | * the parser and input reader have been turned inside out. Now, |
| 34 | * the parser is in control, and asks for input as needed. The old |
| 35 | * way had the input reader in control, and it asked for parsing to |
| 36 | * take place as needed. The new way makes it much easier to properly |
| 37 | * handle the recursion implicit in the various substitutions, especially |
| 38 | * across continuation lines. |
| 39 | * |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 40 | * POSIX syntax not implemented: |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 41 | * aliases |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 42 | * Tilde Expansion |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 43 | * |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 44 | * Bash compat TODO: |
| 45 | * redirection of stdout+stderr: &> and >& |
| 46 | * brace expansion: one/{two,three,four} |
| 47 | * reserved words: function select |
| 48 | * advanced test: [[ ]] |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 49 | * process substitution: <(list) and >(list) |
| 50 | * =~: regex operator |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 51 | * let EXPR [EXPR...] |
| 52 | * Each EXPR is an arithmetic expression (ARITHMETIC EVALUATION) |
| 53 | * If the last arg evaluates to 0, let returns 1; 0 otherwise. |
| 54 | * NB: let `echo 'a=a + 1'` - error (IOW: multi-word expansion is used) |
| 55 | * ((EXPR)) |
| 56 | * The EXPR is evaluated according to ARITHMETIC EVALUATION. |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 57 | * This is exactly equivalent to let "EXPR". |
| 58 | * $[EXPR]: synonym for $((EXPR)) |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 59 | * |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 60 | * TODOs: |
| 61 | * grep for "TODO" and fix (some of them are easy) |
Denys Vlasenko | 8806d64 | 2010-05-16 02:36:18 +0200 | [diff] [blame] | 62 | * special variables (done: PWD, PPID, RANDOM) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 63 | * follow IFS rules more precisely, including update semantics |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 64 | * export builtin should be special, its arguments are assignments |
| 65 | * and therefore expansion of them should be "one-word" expansion: |
| 66 | * $ export i=`echo 'a b'` # export has one arg: "i=a b" |
| 67 | * compare with: |
| 68 | * $ ls i=`echo 'a b'` # ls has two args: "i=a" and "b" |
| 69 | * ls: cannot access i=a: No such file or directory |
| 70 | * ls: cannot access b: No such file or directory |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 71 | * Note1: same applies to local builtin. |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 72 | * Note2: bash 3.2.33(1) does this only if export word itself |
| 73 | * is not quoted: |
| 74 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 75 | * aaa bbb |
| 76 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 77 | * aaa |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 78 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 79 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 80 | */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 81 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 82 | #include <malloc.h> /* for malloc_trim */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 83 | #include <glob.h> |
| 84 | /* #include <dmalloc.h> */ |
| 85 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 86 | # include <fnmatch.h> |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 87 | #endif |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 88 | |
| 89 | #include "shell_common.h" |
| 90 | #include "builtin_read.h" |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 91 | #include "builtin_ulimit.h" |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 92 | #include "math.h" |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 93 | #include "match.h" |
Denys Vlasenko | cbe0b7f | 2009-10-09 22:00:58 +0200 | [diff] [blame] | 94 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 95 | # include "random.h" |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 96 | #else |
| 97 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 98 | #endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 99 | #ifndef PIPE_BUF |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 100 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 101 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 102 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 103 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 104 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 105 | #define LEAK_HUNTING 0 |
| 106 | #define BUILD_AS_NOMMU 0 |
| 107 | /* Enable/disable sanity checks. Ok to enable in production, |
| 108 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 109 | * Keeping 1 for now even in released versions. |
| 110 | */ |
| 111 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 112 | /* Slightly bigger (+200 bytes), but faster hush. |
| 113 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 114 | * which allows us to do fewer waitpid's. |
| 115 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 116 | * and therefore waitpid will return the same result as last time) |
| 117 | */ |
| 118 | #define ENABLE_HUSH_FAST 0 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 119 | |
| 120 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 121 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 122 | # undef BB_MMU |
| 123 | # undef USE_FOR_NOMMU |
| 124 | # undef USE_FOR_MMU |
| 125 | # define BB_MMU 0 |
| 126 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 127 | # define USE_FOR_MMU(...) |
| 128 | #endif |
| 129 | |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 130 | #define SKIP_definitions 1 |
| 131 | #include "applet_tables.h" |
| 132 | #undef SKIP_definitions |
| 133 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 134 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 135 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 136 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 137 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 138 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 139 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 140 | # define IF_FEATURE_SH_STANDALONE(...) |
| 141 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 142 | #endif |
| 143 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 144 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 145 | # undef ENABLE_FEATURE_EDITING |
| 146 | # define ENABLE_FEATURE_EDITING 0 |
| 147 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 148 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 149 | #endif |
| 150 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 151 | /* Do we support ANY keywords? */ |
| 152 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 153 | # define HAS_KEYWORDS 1 |
| 154 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 155 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 156 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 157 | # define HAS_KEYWORDS 0 |
| 158 | # define IF_HAS_KEYWORDS(...) |
| 159 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 160 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 161 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 162 | /* If you comment out one of these below, it will be #defined later |
| 163 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 164 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 165 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 166 | #define debug_printf_parse(...) do {} while (0) |
| 167 | #define debug_print_tree(a, b) do {} while (0) |
| 168 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 169 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 170 | #define debug_printf_jobs(...) do {} while (0) |
| 171 | #define debug_printf_expand(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 172 | #define debug_printf_glob(...) do {} while (0) |
| 173 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 174 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 175 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 176 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 177 | #define ERR_PTR ((void*)(long)1) |
| 178 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 179 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 180 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 181 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 182 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 183 | struct variable; |
| 184 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 185 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 186 | |
| 187 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 188 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 189 | */ |
| 190 | #if !BB_MMU |
| 191 | typedef struct nommu_save_t { |
| 192 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 193 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 194 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 195 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 196 | } nommu_save_t; |
| 197 | #endif |
| 198 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 199 | typedef enum reserved_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 200 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 201 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 202 | RES_IF , |
| 203 | RES_THEN , |
| 204 | RES_ELIF , |
| 205 | RES_ELSE , |
| 206 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 207 | #endif |
| 208 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 209 | RES_FOR , |
| 210 | RES_WHILE , |
| 211 | RES_UNTIL , |
| 212 | RES_DO , |
| 213 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 214 | #endif |
| 215 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 216 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 217 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 218 | #if ENABLE_HUSH_CASE |
| 219 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 220 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 221 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 222 | RES_MATCH , /* "word)" */ |
| 223 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 224 | RES_ESAC , |
| 225 | #endif |
| 226 | RES_XXXX , |
| 227 | RES_SNTX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 228 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 229 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 230 | typedef struct o_string { |
| 231 | char *data; |
| 232 | int length; /* position where data is appended */ |
| 233 | int maxlen; |
| 234 | /* Protect newly added chars against globbing |
| 235 | * (by prepending \ to *, ?, [, \) */ |
| 236 | smallint o_escape; |
| 237 | smallint o_glob; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 238 | /* At least some part of the string was inside '' or "", |
| 239 | * possibly empty one: word"", wo''rd etc. */ |
| 240 | smallint o_quoted; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 241 | smallint has_empty_slot; |
| 242 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 243 | } o_string; |
| 244 | enum { |
| 245 | MAYBE_ASSIGNMENT = 0, |
| 246 | DEFINITELY_ASSIGNMENT = 1, |
| 247 | NOT_ASSIGNMENT = 2, |
| 248 | WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */ |
| 249 | }; |
| 250 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 251 | #define NULL_O_STRING { NULL } |
| 252 | |
| 253 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
| 254 | * available? Where is it documented? */ |
| 255 | typedef struct in_str { |
| 256 | const char *p; |
| 257 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 258 | char eof_flag; /* meaningless if ->p == NULL */ |
| 259 | char peek_buf[2]; |
| 260 | #if ENABLE_HUSH_INTERACTIVE |
| 261 | smallint promptme; |
| 262 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 263 | #endif |
| 264 | FILE *file; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 265 | int (*get) (struct in_str *) FAST_FUNC; |
| 266 | int (*peek) (struct in_str *) FAST_FUNC; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 267 | } in_str; |
| 268 | #define i_getch(input) ((input)->get(input)) |
| 269 | #define i_peek(input) ((input)->peek(input)) |
| 270 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 271 | /* The descrip member of this structure is only used to make |
| 272 | * debugging output pretty */ |
| 273 | static const struct { |
| 274 | int mode; |
| 275 | signed char default_fd; |
| 276 | char descrip[3]; |
| 277 | } redir_table[] = { |
| 278 | { O_RDONLY, 0, "<" }, |
| 279 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 280 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 281 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 282 | { O_RDONLY, 0, "<<" }, |
| 283 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 284 | /* { O_RDONLY, 77, "<<" }, */ |
| 285 | }; |
| 286 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 287 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 288 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 289 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 290 | int rd_fd; /* fd to redirect */ |
| 291 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 292 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 293 | smallint rd_type; /* (enum redir_type) */ |
| 294 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 295 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 296 | * bit 0: do we need to trim leading tabs? |
| 297 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 298 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 299 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 300 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 301 | REDIRECT_INPUT = 0, |
| 302 | REDIRECT_OVERWRITE = 1, |
| 303 | REDIRECT_APPEND = 2, |
| 304 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 305 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 306 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 307 | |
| 308 | REDIRFD_CLOSE = -3, |
| 309 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 310 | REDIRFD_TO_FILE = -1, |
| 311 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 312 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 313 | HEREDOC_SKIPTABS = 1, |
| 314 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 315 | } redir_type; |
| 316 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 317 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 318 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 319 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 320 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 321 | smallint is_stopped; /* is the command currently running? */ |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 322 | smallint cmd_type; /* CMD_xxx */ |
| 323 | #define CMD_NORMAL 0 |
| 324 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 325 | |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 326 | /* used for "[[ EXPR ]]" */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 327 | #if ENABLE_HUSH_BASH_COMPAT |
| 328 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 329 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 330 | |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 331 | /* used for "export noglob=* glob* a=`echo a b`" */ |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 332 | //#define CMD_SINGLEWORD_NOGLOB_COND 3 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 333 | // It is hard to implement correctly, it adds significant amounts of tricky code, |
| 334 | // and all this is only useful for really obscure export statements |
| 335 | // almost nobody would use anyway. #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 336 | // guards the code which implements it, but I have doubts it works |
| 337 | // in all cases (especially with mixed globbed/non-globbed arguments) |
| 338 | |
| 339 | #if ENABLE_HUSH_FUNCTIONS |
| 340 | # define CMD_FUNCDEF 3 |
| 341 | #endif |
| 342 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 343 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 344 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 345 | #if !BB_MMU |
| 346 | char *group_as_string; |
| 347 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 348 | #if ENABLE_HUSH_FUNCTIONS |
| 349 | struct function *child_func; |
| 350 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 351 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 352 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 353 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 354 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 355 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 356 | * we put those fields back into cmd->xxx |
| 357 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 358 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 359 | * Without this trick, loop would execute a;b;b;b;... |
| 360 | * instead of correct sequence a;b;a;b;... |
| 361 | * When command is freed, it severs the link |
| 362 | * (sets ->child_func->parent_cmd to NULL). |
| 363 | */ |
| 364 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 365 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 366 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 367 | * and on execution these are substituted with their values. |
| 368 | * Substitution can make _several_ words out of one argv[n]! |
| 369 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 370 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 371 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 372 | struct redir_struct *redirects; /* I/O redirections */ |
| 373 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 374 | /* Is there anything in this command at all? */ |
| 375 | #define IS_NULL_CMD(cmd) \ |
| 376 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 377 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 378 | |
| 379 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 380 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 381 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 382 | int alive_cmds; /* number of commands running (not exited) */ |
| 383 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 384 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 385 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 386 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 387 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 388 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 389 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 390 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 391 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 392 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 393 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 394 | typedef enum pipe_style { |
| 395 | PIPE_SEQ = 1, |
| 396 | PIPE_AND = 2, |
| 397 | PIPE_OR = 3, |
| 398 | PIPE_BG = 4, |
| 399 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 400 | /* Is there anything in this pipe at all? */ |
| 401 | #define IS_NULL_PIPE(pi) \ |
| 402 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 403 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 404 | /* This holds pointers to the various results of parsing */ |
| 405 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 406 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 407 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 408 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 409 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 410 | /* last command in pipe (being constructed right now) */ |
| 411 | struct command *command; |
| 412 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 413 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 414 | #if !BB_MMU |
| 415 | o_string as_string; |
| 416 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 417 | #if HAS_KEYWORDS |
| 418 | smallint ctx_res_w; |
| 419 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 420 | #if ENABLE_HUSH_CASE |
| 421 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 422 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 423 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 424 | int old_flag; |
| 425 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 426 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 427 | * when we see "if" or "then", we malloc and copy current context, |
| 428 | * and make ->stack point to it. then we parse pipeN. |
| 429 | * when closing "then" / fi" / whatever is found, |
| 430 | * we move list_head into ->stack->command->group, |
| 431 | * copy ->stack into current context, and delete ->stack. |
| 432 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 433 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 434 | struct parse_context *stack; |
| 435 | #endif |
| 436 | }; |
| 437 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 438 | /* On program start, environ points to initial environment. |
| 439 | * putenv adds new pointers into it, unsetenv removes them. |
| 440 | * Neither of these (de)allocates the strings. |
| 441 | * setenv allocates new strings in malloc space and does putenv, |
| 442 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 443 | #define setenv(...) setenv_is_leaky_dont_use() |
| 444 | struct variable { |
| 445 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 446 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 447 | #if ENABLE_HUSH_LOCAL |
| 448 | unsigned func_nest_level; |
| 449 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 450 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 451 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 452 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 453 | }; |
| 454 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 455 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 456 | BC_BREAK = 1, |
| 457 | BC_CONTINUE = 2, |
| 458 | }; |
| 459 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 460 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 461 | struct function { |
| 462 | struct function *next; |
| 463 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 464 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 465 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 466 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 467 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 468 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 469 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 470 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 471 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 472 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 473 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 474 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 475 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 476 | /* interactive_fd != 0 means we are an interactive shell. |
| 477 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 478 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 479 | * job control still works, but terminal signals |
| 480 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 481 | * process groups can only be created with "cmd &". |
| 482 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 483 | * to give tty to the foreground process group, |
| 484 | * and will take it back when the group is stopped (^Z) |
| 485 | * or killed (^C). |
| 486 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 487 | #if ENABLE_HUSH_INTERACTIVE |
| 488 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 489 | * _AND_ if we decided to act interactively */ |
| 490 | int interactive_fd; |
| 491 | const char *PS1; |
| 492 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 493 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 494 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 495 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 496 | #endif |
| 497 | #if ENABLE_FEATURE_EDITING |
| 498 | line_input_t *line_input_state; |
| 499 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 500 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 501 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 502 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 503 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 504 | random_t random_gen; |
| 505 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 506 | #if ENABLE_HUSH_JOB |
| 507 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 508 | int last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 509 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 510 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 511 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 512 | #else |
| 513 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 514 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 515 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 516 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 517 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 518 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 519 | #if ENABLE_HUSH_FUNCTIONS |
| 520 | /* 0: outside of a function (or sourced file) |
| 521 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 522 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 523 | */ |
| 524 | smallint flag_return_in_progress; |
| 525 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 526 | smallint fake_mode; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 527 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 528 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 529 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 530 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 531 | smalluint global_args_malloced; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 532 | smalluint inherited_set_is_saved; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 533 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 534 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 535 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 536 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 537 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 538 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 539 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 540 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 541 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 542 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 543 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 544 | const char *cwd; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 545 | struct variable *top_var; /* = &G.shell_ver (set in main()) */ |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 546 | struct variable shell_ver; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 547 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 548 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 549 | # if ENABLE_HUSH_LOCAL |
| 550 | struct variable **shadowed_vars_pp; |
| 551 | unsigned func_nest_level; |
| 552 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 553 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 554 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 555 | #if ENABLE_HUSH_FAST |
| 556 | unsigned count_SIGCHLD; |
| 557 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 558 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 559 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 560 | /* which signals have non-DFL handler (even with no traps set)? */ |
| 561 | unsigned non_DFL_mask; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 562 | char **traps; /* char *traps[NSIG] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 563 | sigset_t blocked_set; |
| 564 | sigset_t inherited_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 565 | #if HUSH_DEBUG |
| 566 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 567 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 568 | #endif |
Denys Vlasenko | aaa22d2 | 2009-10-19 16:34:39 +0200 | [diff] [blame] | 569 | 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] | 570 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 571 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 572 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 573 | * (too many defines). Also, I actually prefer to see when a variable |
| 574 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 575 | #define INIT_G() do { \ |
| 576 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 577 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 578 | |
| 579 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 580 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 581 | static int builtin_cd(char **argv) FAST_FUNC; |
| 582 | static int builtin_echo(char **argv) FAST_FUNC; |
| 583 | static int builtin_eval(char **argv) FAST_FUNC; |
| 584 | static int builtin_exec(char **argv) FAST_FUNC; |
| 585 | static int builtin_exit(char **argv) FAST_FUNC; |
| 586 | static int builtin_export(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 587 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 588 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 589 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 590 | #endif |
| 591 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 592 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 593 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 594 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 595 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 596 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 597 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 598 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 599 | #endif |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 600 | #if ENABLE_PRINTF |
| 601 | static int builtin_printf(char **argv) FAST_FUNC; |
| 602 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 603 | static int builtin_pwd(char **argv) FAST_FUNC; |
| 604 | static int builtin_read(char **argv) FAST_FUNC; |
| 605 | static int builtin_set(char **argv) FAST_FUNC; |
| 606 | static int builtin_shift(char **argv) FAST_FUNC; |
| 607 | static int builtin_source(char **argv) FAST_FUNC; |
| 608 | static int builtin_test(char **argv) FAST_FUNC; |
| 609 | static int builtin_trap(char **argv) FAST_FUNC; |
| 610 | static int builtin_type(char **argv) FAST_FUNC; |
| 611 | static int builtin_true(char **argv) FAST_FUNC; |
| 612 | static int builtin_umask(char **argv) FAST_FUNC; |
| 613 | static int builtin_unset(char **argv) FAST_FUNC; |
| 614 | static int builtin_wait(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 615 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 616 | static int builtin_break(char **argv) FAST_FUNC; |
| 617 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 618 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 619 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 620 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 621 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 622 | |
| 623 | /* Table of built-in functions. They can be forked or not, depending on |
| 624 | * context: within pipes, they fork. As simple commands, they do not. |
| 625 | * When used in non-forking context, they can change global variables |
| 626 | * in the parent shell process. If forked, of course they cannot. |
| 627 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 628 | * still be set at the end. */ |
| 629 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 630 | const char *b_cmd; |
| 631 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 632 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 633 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 634 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 635 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 636 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 637 | #endif |
| 638 | }; |
| 639 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 640 | static const struct built_in_command bltins1[] = { |
| 641 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 642 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 643 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 644 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 645 | #endif |
| 646 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 647 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 648 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 649 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 650 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 651 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 652 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 653 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 654 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 655 | BLTIN("exit" , builtin_exit , "Exit"), |
| 656 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 657 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 658 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 659 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 660 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 661 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 662 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 663 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 664 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 665 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 666 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 667 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 668 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 669 | #if HUSH_DEBUG |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 670 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 671 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 672 | BLTIN("read" , builtin_read , "Input into variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 673 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 674 | BLTIN("return" , builtin_return , "Return from a function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 675 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 676 | BLTIN("set" , builtin_set , "Set/unset positional parameters"), |
| 677 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 678 | #if ENABLE_HUSH_BASH_COMPAT |
| 679 | BLTIN("source" , builtin_source , "Run commands in a file"), |
| 680 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 681 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 682 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 683 | BLTIN("ulimit" , shell_builtin_ulimit , "Control resource limits"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 684 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 685 | BLTIN("unset" , builtin_unset , "Unset variables"), |
| 686 | BLTIN("wait" , builtin_wait , "Wait for process"), |
| 687 | }; |
| 688 | /* For now, echo and test are unconditionally enabled. |
| 689 | * Maybe make it configurable? */ |
| 690 | static const struct built_in_command bltins2[] = { |
| 691 | BLTIN("[" , builtin_test , NULL), |
| 692 | BLTIN("echo" , builtin_echo , NULL), |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 693 | #if ENABLE_PRINTF |
| 694 | BLTIN("printf" , builtin_printf , NULL), |
| 695 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 696 | BLTIN("pwd" , builtin_pwd , NULL), |
| 697 | BLTIN("test" , builtin_test , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 698 | }; |
| 699 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 700 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 701 | /* Debug printouts. |
| 702 | */ |
| 703 | #if HUSH_DEBUG |
| 704 | /* prevent disasters with G.debug_indent < 0 */ |
| 705 | # define indent() fprintf(stderr, "%*s", (G.debug_indent * 2) & 0xff, "") |
| 706 | # define debug_enter() (G.debug_indent++) |
| 707 | # define debug_leave() (G.debug_indent--) |
| 708 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 709 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 710 | # define debug_enter() ((void)0) |
| 711 | # define debug_leave() ((void)0) |
| 712 | #endif |
| 713 | |
| 714 | #ifndef debug_printf |
| 715 | # define debug_printf(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 716 | #endif |
| 717 | |
| 718 | #ifndef debug_printf_parse |
| 719 | # define debug_printf_parse(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 720 | #endif |
| 721 | |
| 722 | #ifndef debug_printf_exec |
| 723 | #define debug_printf_exec(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 724 | #endif |
| 725 | |
| 726 | #ifndef debug_printf_env |
| 727 | # define debug_printf_env(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 728 | #endif |
| 729 | |
| 730 | #ifndef debug_printf_jobs |
| 731 | # define debug_printf_jobs(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 732 | # define DEBUG_JOBS 1 |
| 733 | #else |
| 734 | # define DEBUG_JOBS 0 |
| 735 | #endif |
| 736 | |
| 737 | #ifndef debug_printf_expand |
| 738 | # define debug_printf_expand(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 739 | # define DEBUG_EXPAND 1 |
| 740 | #else |
| 741 | # define DEBUG_EXPAND 0 |
| 742 | #endif |
| 743 | |
| 744 | #ifndef debug_printf_glob |
| 745 | # define debug_printf_glob(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 746 | # define DEBUG_GLOB 1 |
| 747 | #else |
| 748 | # define DEBUG_GLOB 0 |
| 749 | #endif |
| 750 | |
| 751 | #ifndef debug_printf_list |
| 752 | # define debug_printf_list(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 753 | #endif |
| 754 | |
| 755 | #ifndef debug_printf_subst |
| 756 | # define debug_printf_subst(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 757 | #endif |
| 758 | |
| 759 | #ifndef debug_printf_clean |
| 760 | # define debug_printf_clean(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 761 | # define DEBUG_CLEAN 1 |
| 762 | #else |
| 763 | # define DEBUG_CLEAN 0 |
| 764 | #endif |
| 765 | |
| 766 | #if DEBUG_EXPAND |
| 767 | static void debug_print_strings(const char *prefix, char **vv) |
| 768 | { |
| 769 | indent(); |
| 770 | fprintf(stderr, "%s:\n", prefix); |
| 771 | while (*vv) |
| 772 | fprintf(stderr, " '%s'\n", *vv++); |
| 773 | } |
| 774 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 775 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 776 | #endif |
| 777 | |
| 778 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 779 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 780 | */ |
| 781 | #if LEAK_HUNTING |
| 782 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 783 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 784 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 785 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 786 | return ptr; |
| 787 | } |
| 788 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 789 | { |
| 790 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 791 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 792 | return ptr; |
| 793 | } |
| 794 | static char *xxstrdup(int lineno, const char *str) |
| 795 | { |
| 796 | char *ptr = xstrdup(str); |
| 797 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 798 | return ptr; |
| 799 | } |
| 800 | static void xxfree(void *ptr) |
| 801 | { |
| 802 | fdprintf(2, "free %p\n", ptr); |
| 803 | free(ptr); |
| 804 | } |
| 805 | #define xmalloc(s) xxmalloc(__LINE__, s) |
| 806 | #define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 807 | #define xstrdup(s) xxstrdup(__LINE__, s) |
| 808 | #define free(p) xxfree(p) |
| 809 | #endif |
| 810 | |
| 811 | |
| 812 | /* Syntax and runtime errors. They always abort scripts. |
| 813 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 814 | * and return to the prompt. |
| 815 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 816 | */ |
| 817 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 818 | # define die_if_script(lineno, ...) die_if_script(__VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 819 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 820 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 821 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 822 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 823 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 824 | #endif |
| 825 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 826 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 827 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 828 | va_list p; |
| 829 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 830 | #if HUSH_DEBUG >= 2 |
| 831 | bb_error_msg("hush.c:%u", lineno); |
| 832 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 833 | va_start(p, fmt); |
| 834 | bb_verror_msg(fmt, p, NULL); |
| 835 | va_end(p); |
| 836 | if (!G_interactive_fd) |
| 837 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 838 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 839 | |
| 840 | static void syntax_error(unsigned lineno, const char *msg) |
| 841 | { |
| 842 | if (msg) |
| 843 | die_if_script(lineno, "syntax error: %s", msg); |
| 844 | else |
| 845 | die_if_script(lineno, "syntax error", NULL); |
| 846 | } |
| 847 | |
| 848 | static void syntax_error_at(unsigned lineno, const char *msg) |
| 849 | { |
| 850 | die_if_script(lineno, "syntax error at '%s'", msg); |
| 851 | } |
| 852 | |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 853 | static void syntax_error_unterm_str(unsigned lineno, const char *s) |
| 854 | { |
| 855 | die_if_script(lineno, "syntax error: unterminated %s", s); |
| 856 | } |
| 857 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 858 | /* It so happens that all such cases are totally fatal |
| 859 | * even if shell is interactive: EOF while looking for closing |
| 860 | * delimiter. There is nowhere to read stuff from after that, |
| 861 | * it's EOF! The only choice is to terminate. |
| 862 | */ |
| 863 | static void syntax_error_unterm_ch(unsigned lineno, char ch) NORETURN; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 864 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 865 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 866 | char msg[2] = { ch, '\0' }; |
| 867 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 868 | xfunc_die(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 871 | static void syntax_error_unexpected_ch(unsigned lineno, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 872 | { |
| 873 | char msg[2]; |
| 874 | msg[0] = ch; |
| 875 | msg[1] = '\0'; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 876 | die_if_script(lineno, "syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 879 | #if HUSH_DEBUG < 2 |
| 880 | # undef die_if_script |
| 881 | # undef syntax_error |
| 882 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 883 | # undef syntax_error_unterm_ch |
| 884 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 885 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 886 | #else |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 887 | # define die_if_script(...) die_if_script(__LINE__, __VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 888 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 889 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 890 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 891 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 892 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 893 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 894 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 895 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 896 | #if ENABLE_HUSH_INTERACTIVE |
| 897 | static void cmdedit_update_prompt(void); |
| 898 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 899 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 900 | #endif |
| 901 | |
| 902 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 903 | /* Utility functions |
| 904 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 905 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 906 | static char *unbackslash(char *src) |
| 907 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 908 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 909 | while (1) { |
| 910 | if (*src == '\\') |
| 911 | src++; |
| 912 | if ((*dst++ = *src++) == '\0') |
| 913 | break; |
| 914 | } |
| 915 | return dst; |
| 916 | } |
| 917 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 918 | 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] | 919 | { |
| 920 | int i; |
| 921 | unsigned count1; |
| 922 | unsigned count2; |
| 923 | char **v; |
| 924 | |
| 925 | v = strings; |
| 926 | count1 = 0; |
| 927 | if (v) { |
| 928 | while (*v) { |
| 929 | count1++; |
| 930 | v++; |
| 931 | } |
| 932 | } |
| 933 | count2 = 0; |
| 934 | v = add; |
| 935 | while (*v) { |
| 936 | count2++; |
| 937 | v++; |
| 938 | } |
| 939 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 940 | v[count1 + count2] = NULL; |
| 941 | i = count2; |
| 942 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 943 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 944 | return v; |
| 945 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 946 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 947 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 948 | { |
| 949 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 950 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 951 | return ptr; |
| 952 | } |
| 953 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 954 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 955 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 956 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 957 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 958 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 959 | { |
| 960 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 961 | v[0] = add; |
| 962 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 963 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 964 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 965 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 966 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 967 | { |
| 968 | char **ptr = add_string_to_strings(strings, add); |
| 969 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 970 | return ptr; |
| 971 | } |
| 972 | #define add_string_to_strings(strings, add) \ |
| 973 | xx_add_string_to_strings(__LINE__, strings, add) |
| 974 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 975 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 976 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 977 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 978 | char **v; |
| 979 | |
| 980 | if (!strings) |
| 981 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 982 | v = strings; |
| 983 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 984 | free(*v); |
| 985 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 986 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 987 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 990 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 991 | /* Helpers for setting new $n and restoring them back |
| 992 | */ |
| 993 | typedef struct save_arg_t { |
| 994 | char *sv_argv0; |
| 995 | char **sv_g_argv; |
| 996 | int sv_g_argc; |
| 997 | smallint sv_g_malloced; |
| 998 | } save_arg_t; |
| 999 | |
| 1000 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1001 | { |
| 1002 | int n; |
| 1003 | |
| 1004 | sv->sv_argv0 = argv[0]; |
| 1005 | sv->sv_g_argv = G.global_argv; |
| 1006 | sv->sv_g_argc = G.global_argc; |
| 1007 | sv->sv_g_malloced = G.global_args_malloced; |
| 1008 | |
| 1009 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1010 | G.global_argv = argv; |
| 1011 | G.global_args_malloced = 0; |
| 1012 | |
| 1013 | n = 1; |
| 1014 | while (*++argv) |
| 1015 | n++; |
| 1016 | G.global_argc = n; |
| 1017 | } |
| 1018 | |
| 1019 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1020 | { |
| 1021 | char **pp; |
| 1022 | |
| 1023 | if (G.global_args_malloced) { |
| 1024 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 1025 | pp = G.global_argv; |
| 1026 | while (*++pp) /* note: does not free $0 */ |
| 1027 | free(*pp); |
| 1028 | free(G.global_argv); |
| 1029 | } |
| 1030 | argv[0] = sv->sv_argv0; |
| 1031 | G.global_argv = sv->sv_g_argv; |
| 1032 | G.global_argc = sv->sv_g_argc; |
| 1033 | G.global_args_malloced = sv->sv_g_malloced; |
| 1034 | } |
| 1035 | |
| 1036 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1037 | /* Basic theory of signal handling in shell |
| 1038 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1039 | * This does not describe what hush does, rather, it is current understanding |
| 1040 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1041 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1042 | * |
| 1043 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1044 | * is finished or backgrounded. It is the same in interactive and |
| 1045 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1046 | * 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] | 1047 | * ^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] | 1048 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1049 | * pipe. |
| 1050 | * |
| 1051 | * Wait builtin in interruptible by signals for which user trap is set |
| 1052 | * or by SIGINT in interactive shell. |
| 1053 | * |
| 1054 | * Trap handlers will execute even within trap handlers. (right?) |
| 1055 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1056 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1057 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1058 | * |
| 1059 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1060 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1061 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1062 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1063 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1064 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1065 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1066 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1067 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1068 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1069 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1070 | * |
| 1071 | * SIGQUIT: ignore |
| 1072 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1073 | * SIGHUP (interactive): |
| 1074 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1075 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1076 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1077 | * that all pipe members are stopped. Try this in bash: |
| 1078 | * while :; do :; done - ^Z does not background it |
| 1079 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1080 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1081 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1082 | * to interactive shell while shell is waiting for a pipe, |
| 1083 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1084 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1085 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1086 | * Example 2: this does not wait and does not execute ls: |
| 1087 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1088 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1089 | * "sleep 5; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1090 | * |
| 1091 | * (What happens to signals which are IGN on shell start?) |
| 1092 | * (What happens with signal mask on shell start?) |
| 1093 | * |
| 1094 | * Implementation in hush |
| 1095 | * ====================== |
| 1096 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1097 | * We block all signals which we don't want to take action immediately, |
| 1098 | * i.e. we block all signals which need to have special handling as described |
| 1099 | * above, and all signals which have traps set. |
| 1100 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1101 | * and act on them. |
| 1102 | * |
| 1103 | * unsigned non_DFL_mask: a mask of such "special" signals |
| 1104 | * sigset_t blocked_set: current blocked signal set |
| 1105 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1106 | * "trap - SIGxxx": |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1107 | * 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] | 1108 | * "trap 'cmd' SIGxxx": |
| 1109 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1110 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1111 | * unblock signals with special interactive handling |
| 1112 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1113 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1114 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1115 | * 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] | 1116 | * 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] | 1117 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1118 | * 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] | 1119 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1120 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1121 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1122 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1123 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1124 | * 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] | 1125 | * 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] | 1126 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1127 | enum { |
| 1128 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1129 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1130 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1131 | | (1 << SIGHUP) |
| 1132 | , |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1133 | SPECIAL_JOB_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1134 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1135 | | (1 << SIGTTIN) |
| 1136 | | (1 << SIGTTOU) |
| 1137 | | (1 << SIGTSTP) |
| 1138 | #endif |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1139 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1140 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1141 | #if ENABLE_HUSH_FAST |
| 1142 | static void SIGCHLD_handler(int sig UNUSED_PARAM) |
| 1143 | { |
| 1144 | G.count_SIGCHLD++; |
| 1145 | //bb_error_msg("[%d] SIGCHLD_handler: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1146 | } |
| 1147 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1148 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1149 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1150 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1151 | /* After [v]fork, in child: do not restore tty pgrp on xfunc death */ |
| 1152 | #define disable_restore_tty_pgrp_on_exit() (die_sleep = 0) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1153 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1154 | #define enable_restore_tty_pgrp_on_exit() (die_sleep = -1) |
| 1155 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1156 | /* Restores tty foreground process group, and exits. |
| 1157 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1158 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1159 | * or called directly with -EXITCODE. |
| 1160 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1161 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1162 | static void sigexit(int sig) |
| 1163 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1164 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 1165 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1166 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1167 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1168 | * tty pgrp then, only top-level shell process does that */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1169 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 1170 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1171 | |
| 1172 | /* Not a signal, just exit */ |
| 1173 | if (sig <= 0) |
| 1174 | _exit(- sig); |
| 1175 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1176 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1177 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1178 | #else |
| 1179 | |
| 1180 | #define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1181 | #define enable_restore_tty_pgrp_on_exit() ((void)0) |
| 1182 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1183 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1184 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1185 | /* Restores tty foreground process group, and exits. */ |
| 1186 | static void hush_exit(int exitcode) NORETURN; |
| 1187 | static void hush_exit(int exitcode) |
| 1188 | { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1189 | if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) { |
| 1190 | /* Prevent recursion: |
| 1191 | * trap "echo Hi; exit" EXIT; exit |
| 1192 | */ |
| 1193 | char *argv[] = { NULL, G.traps[0], NULL }; |
| 1194 | G.traps[0] = NULL; |
| 1195 | G.exiting = 1; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1196 | builtin_eval(argv); |
| 1197 | free(argv[1]); |
| 1198 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1199 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1200 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1201 | fflush_all(); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1202 | sigexit(- (exitcode & 0xff)); |
| 1203 | #else |
| 1204 | exit(exitcode); |
| 1205 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1208 | static int check_and_run_traps(int sig) |
| 1209 | { |
| 1210 | static const struct timespec zero_timespec = { 0, 0 }; |
| 1211 | smalluint save_rcode; |
| 1212 | int last_sig = 0; |
| 1213 | |
| 1214 | if (sig) |
| 1215 | goto jump_in; |
| 1216 | while (1) { |
| 1217 | sig = sigtimedwait(&G.blocked_set, NULL, &zero_timespec); |
| 1218 | if (sig <= 0) |
| 1219 | break; |
| 1220 | jump_in: |
| 1221 | last_sig = sig; |
| 1222 | if (G.traps && G.traps[sig]) { |
| 1223 | if (G.traps[sig][0]) { |
| 1224 | /* We have user-defined handler */ |
| 1225 | char *argv[] = { NULL, xstrdup(G.traps[sig]), NULL }; |
| 1226 | save_rcode = G.last_exitcode; |
| 1227 | builtin_eval(argv); |
| 1228 | free(argv[1]); |
| 1229 | G.last_exitcode = save_rcode; |
| 1230 | } /* else: "" trap, ignoring signal */ |
| 1231 | continue; |
| 1232 | } |
| 1233 | /* not a trap: special action */ |
| 1234 | switch (sig) { |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1235 | #if ENABLE_HUSH_FAST |
| 1236 | case SIGCHLD: |
| 1237 | G.count_SIGCHLD++; |
| 1238 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1239 | break; |
| 1240 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1241 | case SIGINT: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1242 | /* Builtin was ^C'ed, make it look prettier: */ |
| 1243 | bb_putchar('\n'); |
| 1244 | G.flag_SIGINT = 1; |
| 1245 | break; |
| 1246 | #if ENABLE_HUSH_JOB |
| 1247 | case SIGHUP: { |
| 1248 | struct pipe *job; |
| 1249 | /* bash is observed to signal whole process groups, |
| 1250 | * not individual processes */ |
| 1251 | for (job = G.job_list; job; job = job->next) { |
| 1252 | if (job->pgrp <= 0) |
| 1253 | continue; |
| 1254 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 1255 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 1256 | kill(- job->pgrp, SIGCONT); |
| 1257 | } |
| 1258 | sigexit(SIGHUP); |
| 1259 | } |
| 1260 | #endif |
| 1261 | default: /* ignored: */ |
| 1262 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
| 1263 | break; |
| 1264 | } |
| 1265 | } |
| 1266 | return last_sig; |
| 1267 | } |
| 1268 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1269 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1270 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1271 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1272 | if (force || G.cwd == NULL) { |
| 1273 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1274 | * we must not try to free(bb_msg_unknown) */ |
| 1275 | if (G.cwd == bb_msg_unknown) |
| 1276 | G.cwd = NULL; |
| 1277 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1278 | if (!G.cwd) |
| 1279 | G.cwd = bb_msg_unknown; |
| 1280 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1281 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1284 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1285 | /* |
| 1286 | * Shell and environment variable support |
| 1287 | */ |
| 1288 | static struct variable **get_ptr_to_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1289 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1290 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1291 | struct variable *cur; |
| 1292 | int len; |
| 1293 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1294 | len = strlen(name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1295 | pp = &G.top_var; |
| 1296 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1297 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1298 | return pp; |
| 1299 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1300 | } |
| 1301 | return NULL; |
| 1302 | } |
| 1303 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1304 | static struct variable *get_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1305 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1306 | struct variable **pp = get_ptr_to_local_var(name); |
| 1307 | if (pp) |
| 1308 | return *pp; |
| 1309 | return NULL; |
| 1310 | } |
| 1311 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1312 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1313 | { |
| 1314 | struct variable **pp = get_ptr_to_local_var(name); |
| 1315 | if (pp) |
| 1316 | return strchr((*pp)->varstr, '=') + 1; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 1317 | if (strcmp(name, "PPID") == 0) |
| 1318 | return utoa(G.root_ppid); |
| 1319 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1320 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 8c66a9d | 2009-10-11 02:15:49 +0200 | [diff] [blame] | 1321 | if (strcmp(name, "RANDOM") == 0) { |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1322 | return utoa(next_random(&G.random_gen)); |
Denys Vlasenko | 8c66a9d | 2009-10-11 02:15:49 +0200 | [diff] [blame] | 1323 | } |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1324 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1325 | return NULL; |
| 1326 | } |
| 1327 | |
| 1328 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1329 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1330 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1331 | * 0: do not change export flag |
| 1332 | * (if creating new variable, flag will be 0) |
| 1333 | * 1: set export flag and putenv the variable |
| 1334 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1335 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1336 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1337 | #if !BB_MMU && ENABLE_HUSH_LOCAL |
| 1338 | /* all params are used */ |
| 1339 | #elif BB_MMU && ENABLE_HUSH_LOCAL |
| 1340 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1341 | set_local_var(str, flg_export, local_lvl) |
| 1342 | #elif BB_MMU && !ENABLE_HUSH_LOCAL |
| 1343 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1344 | set_local_var(str, flg_export) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1345 | #elif !BB_MMU && !ENABLE_HUSH_LOCAL |
| 1346 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1347 | set_local_var(str, flg_export, flg_read_only) |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1348 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1349 | 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] | 1350 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1351 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1352 | struct variable *cur; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1353 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1354 | int name_len; |
| 1355 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1356 | eq_sign = strchr(str, '='); |
| 1357 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1358 | free(str); |
| 1359 | return -1; |
| 1360 | } |
| 1361 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1362 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1363 | var_pp = &G.top_var; |
| 1364 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1365 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1366 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1367 | continue; |
| 1368 | } |
| 1369 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1370 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1371 | #if !BB_MMU |
| 1372 | if (!flg_read_only) |
| 1373 | #endif |
| 1374 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1375 | free(str); |
| 1376 | return -1; |
| 1377 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1378 | if (flg_export == -1) { // "&& cur->flg_export" ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1379 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1380 | *eq_sign = '\0'; |
| 1381 | unsetenv(str); |
| 1382 | *eq_sign = '='; |
| 1383 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1384 | #if ENABLE_HUSH_LOCAL |
| 1385 | if (cur->func_nest_level < local_lvl) { |
| 1386 | /* New variable is declared as local, |
| 1387 | * and existing one is global, or local |
| 1388 | * from enclosing function. |
| 1389 | * Remove and save old one: */ |
| 1390 | *var_pp = cur->next; |
| 1391 | cur->next = *G.shadowed_vars_pp; |
| 1392 | *G.shadowed_vars_pp = cur; |
| 1393 | /* bash 3.2.33(1) and exported vars: |
| 1394 | * # export z=z |
| 1395 | * # f() { local z=a; env | grep ^z; } |
| 1396 | * # f |
| 1397 | * z=a |
| 1398 | * # env | grep ^z |
| 1399 | * z=z |
| 1400 | */ |
| 1401 | if (cur->flg_export) |
| 1402 | flg_export = 1; |
| 1403 | break; |
| 1404 | } |
| 1405 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1406 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1407 | free_and_exp: |
| 1408 | free(str); |
| 1409 | goto exp; |
| 1410 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1411 | if (cur->max_len != 0) { |
| 1412 | if (cur->max_len >= strlen(str)) { |
| 1413 | /* This one is from startup env, reuse space */ |
| 1414 | strcpy(cur->varstr, str); |
| 1415 | goto free_and_exp; |
| 1416 | } |
| 1417 | } else { |
| 1418 | /* max_len == 0 signifies "malloced" var, which we can |
| 1419 | * (and has to) free */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1420 | free(cur->varstr); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1421 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1422 | cur->max_len = 0; |
| 1423 | goto set_str_and_exp; |
| 1424 | } |
| 1425 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1426 | /* Not found - create new variable struct */ |
| 1427 | cur = xzalloc(sizeof(*cur)); |
| 1428 | #if ENABLE_HUSH_LOCAL |
| 1429 | cur->func_nest_level = local_lvl; |
| 1430 | #endif |
| 1431 | cur->next = *var_pp; |
| 1432 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1433 | |
| 1434 | set_str_and_exp: |
| 1435 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1436 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1437 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1438 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1439 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1440 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1441 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1442 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1443 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1444 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1445 | if (flg_export == -1) { |
| 1446 | cur->flg_export = 0; |
| 1447 | /* unsetenv was already done */ |
| 1448 | } else { |
| 1449 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1450 | return putenv(cur->varstr); |
| 1451 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1452 | } |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1456 | /* Used at startup and after each cd */ |
| 1457 | static void set_pwd_var(int exp) |
| 1458 | { |
| 1459 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), |
| 1460 | /*exp:*/ exp, /*lvl:*/ 0, /*ro:*/ 0); |
| 1461 | } |
| 1462 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1463 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1464 | { |
| 1465 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1466 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1467 | |
| 1468 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1469 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1470 | var_pp = &G.top_var; |
| 1471 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1472 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1473 | if (cur->flg_read_only) { |
| 1474 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1475 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1476 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1477 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1478 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1479 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1480 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1481 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1482 | if (!cur->max_len) |
| 1483 | free(cur->varstr); |
| 1484 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1485 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1486 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1487 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1488 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1489 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1492 | static int unset_local_var(const char *name) |
| 1493 | { |
| 1494 | return unset_local_var_len(name, strlen(name)); |
| 1495 | } |
| 1496 | |
| 1497 | static void unset_vars(char **strings) |
| 1498 | { |
| 1499 | char **v; |
| 1500 | |
| 1501 | if (!strings) |
| 1502 | return; |
| 1503 | v = strings; |
| 1504 | while (*v) { |
| 1505 | const char *eq = strchrnul(*v, '='); |
| 1506 | unset_local_var_len(*v, (int)(eq - *v)); |
| 1507 | v++; |
| 1508 | } |
| 1509 | free(strings); |
| 1510 | } |
| 1511 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1512 | #if ENABLE_SH_MATH_SUPPORT |
| 1513 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1514 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1515 | static char* FAST_FUNC endofname(const char *name) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1516 | { |
| 1517 | char *p; |
| 1518 | |
| 1519 | p = (char *) name; |
| 1520 | if (!is_name(*p)) |
| 1521 | return p; |
| 1522 | while (*++p) { |
| 1523 | if (!is_in_name(*p)) |
| 1524 | break; |
| 1525 | } |
| 1526 | return p; |
| 1527 | } |
Denys Vlasenko | 6b01b71 | 2010-01-24 22:52:21 +0100 | [diff] [blame] | 1528 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1529 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1530 | 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] | 1531 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1532 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1533 | set_local_var(var, /*flags:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1534 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1535 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1536 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1537 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1538 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 1539 | */ |
| 1540 | static void add_vars(struct variable *var) |
| 1541 | { |
| 1542 | struct variable *next; |
| 1543 | |
| 1544 | while (var) { |
| 1545 | next = var->next; |
| 1546 | var->next = G.top_var; |
| 1547 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1548 | if (var->flg_export) { |
| 1549 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1550 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1551 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1552 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1553 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1554 | var = next; |
| 1555 | } |
| 1556 | } |
| 1557 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1558 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1559 | { |
| 1560 | char **s; |
| 1561 | struct variable *old = NULL; |
| 1562 | |
| 1563 | if (!strings) |
| 1564 | return old; |
| 1565 | s = strings; |
| 1566 | while (*s) { |
| 1567 | struct variable *var_p; |
| 1568 | struct variable **var_pp; |
| 1569 | char *eq; |
| 1570 | |
| 1571 | eq = strchr(*s, '='); |
| 1572 | if (eq) { |
| 1573 | *eq = '\0'; |
| 1574 | var_pp = get_ptr_to_local_var(*s); |
| 1575 | *eq = '='; |
| 1576 | if (var_pp) { |
| 1577 | /* Remove variable from global linked list */ |
| 1578 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1579 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1580 | *var_pp = var_p->next; |
| 1581 | /* Add it to returned list */ |
| 1582 | var_p->next = old; |
| 1583 | old = var_p; |
| 1584 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1585 | set_local_var(*s, /*exp:*/ 1, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1586 | } |
| 1587 | s++; |
| 1588 | } |
| 1589 | return old; |
| 1590 | } |
| 1591 | |
| 1592 | |
| 1593 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1594 | * in_str support |
| 1595 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1596 | static int FAST_FUNC static_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1597 | { |
| 1598 | int ch = *i->p++; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1599 | if (ch != '\0') |
| 1600 | return ch; |
| 1601 | i->p--; |
| 1602 | return EOF; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1605 | static int FAST_FUNC static_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1606 | { |
| 1607 | return *i->p; |
| 1608 | } |
| 1609 | |
| 1610 | #if ENABLE_HUSH_INTERACTIVE |
| 1611 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1612 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1613 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1614 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1615 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1616 | if (G.PS1 == NULL) |
| 1617 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1618 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1619 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1620 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1621 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1622 | if (G.PS2 == NULL) |
| 1623 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1624 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1625 | |
| 1626 | static const char* setup_prompt_string(int promptmode) |
| 1627 | { |
| 1628 | const char *prompt_str; |
| 1629 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1630 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1631 | /* Set up the prompt */ |
| 1632 | if (promptmode == 0) { /* PS1 */ |
| 1633 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1634 | /* bash uses $PWD value, even if it is set by user. |
| 1635 | * It uses current dir only if PWD is unset. |
| 1636 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1637 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1638 | prompt_str = G.PS1; |
| 1639 | } else |
| 1640 | prompt_str = G.PS2; |
| 1641 | } else |
| 1642 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1643 | debug_printf("result '%s'\n", prompt_str); |
| 1644 | return prompt_str; |
| 1645 | } |
| 1646 | |
| 1647 | static void get_user_input(struct in_str *i) |
| 1648 | { |
| 1649 | int r; |
| 1650 | const char *prompt_str; |
| 1651 | |
| 1652 | prompt_str = setup_prompt_string(i->promptmode); |
| 1653 | #if ENABLE_FEATURE_EDITING |
| 1654 | /* Enable command line editing only while a command line |
| 1655 | * is actually being read */ |
| 1656 | do { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1657 | G.flag_SIGINT = 0; |
| 1658 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 1659 | * only after <Enter>. (^C will work) */ |
Denys Vlasenko | aaa22d2 | 2009-10-19 16:34:39 +0200 | [diff] [blame] | 1660 | 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] | 1661 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1662 | check_and_run_traps(0); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1663 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1664 | i->eof_flag = (r < 0); |
| 1665 | if (i->eof_flag) { /* EOF/error detected */ |
| 1666 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1667 | G.user_input_buf[1] = '\0'; |
| 1668 | } |
| 1669 | #else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1670 | do { |
| 1671 | G.flag_SIGINT = 0; |
| 1672 | fputs(prompt_str, stdout); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1673 | fflush_all(); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1674 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1675 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1676 | //do we need check_and_run_traps(0)? (maybe only if stdin) |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1677 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1678 | i->eof_flag = (r == EOF); |
| 1679 | #endif |
| 1680 | i->p = G.user_input_buf; |
| 1681 | } |
| 1682 | |
| 1683 | #endif /* INTERACTIVE */ |
| 1684 | |
| 1685 | /* This is the magic location that prints prompts |
| 1686 | * and gets data back from the user */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1687 | static int FAST_FUNC file_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1688 | { |
| 1689 | int ch; |
| 1690 | |
| 1691 | /* If there is data waiting, eat it up */ |
| 1692 | if (i->p && *i->p) { |
| 1693 | #if ENABLE_HUSH_INTERACTIVE |
| 1694 | take_cached: |
| 1695 | #endif |
| 1696 | ch = *i->p++; |
| 1697 | if (i->eof_flag && !*i->p) |
| 1698 | ch = EOF; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1699 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1700 | } else { |
| 1701 | /* need to double check i->file because we might be doing something |
| 1702 | * more complicated by now, like sourcing or substituting. */ |
| 1703 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 1704 | if (G_interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1705 | do { |
| 1706 | get_user_input(i); |
| 1707 | } while (!*i->p); /* need non-empty line */ |
| 1708 | i->promptmode = 1; /* PS2 */ |
| 1709 | i->promptme = 0; |
| 1710 | goto take_cached; |
| 1711 | } |
| 1712 | #endif |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1713 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1714 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1715 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1716 | #if ENABLE_HUSH_INTERACTIVE |
| 1717 | if (ch == '\n') |
| 1718 | i->promptme = 1; |
| 1719 | #endif |
| 1720 | return ch; |
| 1721 | } |
| 1722 | |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1723 | /* All callers guarantee this routine will never |
| 1724 | * be used right after a newline, so prompting is not needed. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1725 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1726 | static int FAST_FUNC file_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1727 | { |
| 1728 | int ch; |
| 1729 | if (i->p && *i->p) { |
| 1730 | if (i->eof_flag && !i->p[1]) |
| 1731 | return EOF; |
| 1732 | return *i->p; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1733 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1734 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1735 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1736 | i->eof_flag = (ch == EOF); |
| 1737 | i->peek_buf[0] = ch; |
| 1738 | i->peek_buf[1] = '\0'; |
| 1739 | i->p = i->peek_buf; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1740 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1741 | return ch; |
| 1742 | } |
| 1743 | |
| 1744 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1745 | { |
| 1746 | i->peek = file_peek; |
| 1747 | i->get = file_get; |
| 1748 | #if ENABLE_HUSH_INTERACTIVE |
| 1749 | i->promptme = 1; |
| 1750 | i->promptmode = 0; /* PS1 */ |
| 1751 | #endif |
| 1752 | i->file = f; |
| 1753 | i->p = NULL; |
| 1754 | } |
| 1755 | |
| 1756 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1757 | { |
| 1758 | i->peek = static_peek; |
| 1759 | i->get = static_get; |
| 1760 | #if ENABLE_HUSH_INTERACTIVE |
| 1761 | i->promptme = 1; |
| 1762 | i->promptmode = 0; /* PS1 */ |
| 1763 | #endif |
| 1764 | i->p = s; |
| 1765 | i->eof_flag = 0; |
| 1766 | } |
| 1767 | |
| 1768 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1769 | /* |
| 1770 | * o_string support |
| 1771 | */ |
| 1772 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1773 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1774 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1775 | { |
| 1776 | o->length = 0; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 1777 | o->o_quoted = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1778 | if (o->data) |
| 1779 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1782 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1783 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1784 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1785 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1788 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 1789 | { |
| 1790 | free(o->data); |
| 1791 | } |
| 1792 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1793 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1794 | { |
| 1795 | if (o->length + len > o->maxlen) { |
| 1796 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 1797 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 1798 | } |
| 1799 | } |
| 1800 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1801 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1802 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1803 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 1804 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1805 | o->data[o->length] = ch; |
| 1806 | o->length++; |
| 1807 | o->data[o->length] = '\0'; |
| 1808 | } |
| 1809 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1810 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1811 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1812 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1813 | memcpy(&o->data[o->length], str, len); |
| 1814 | o->length += len; |
| 1815 | o->data[o->length] = '\0'; |
| 1816 | } |
| 1817 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1818 | #if !BB_MMU |
| 1819 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1820 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1821 | o_addblock(o, str, strlen(str)); |
| 1822 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1823 | static void nommu_addchr(o_string *o, int ch) |
| 1824 | { |
| 1825 | if (o) |
| 1826 | o_addchr(o, ch); |
| 1827 | } |
| 1828 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1829 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1830 | #endif |
| 1831 | |
| 1832 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 1833 | { |
| 1834 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1837 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1838 | { |
| 1839 | while (len) { |
| 1840 | o_addchr(o, *str); |
| 1841 | if (*str++ == '\\' |
| 1842 | && (*str != '*' && *str != '?' && *str != '[') |
| 1843 | ) { |
| 1844 | o_addchr(o, '\\'); |
| 1845 | } |
| 1846 | len--; |
| 1847 | } |
| 1848 | } |
| 1849 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 1850 | #undef HUSH_BRACE_EXP |
| 1851 | /* |
| 1852 | * HUSH_BRACE_EXP code needs corresponding quoting on variable expansion side. |
| 1853 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 1854 | * Apparently, on unquoted $v bash still does globbing |
| 1855 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 1856 | * but NOT brace expansion! Thus, there should be TWO independent |
| 1857 | * quoting mechanisms on $v expansion side: one protects |
| 1858 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 1859 | * We have only second one. |
| 1860 | */ |
| 1861 | |
| 1862 | #ifdef HUSH_BRACE_EXP |
| 1863 | # define MAYBE_BRACES "{}" |
| 1864 | #else |
| 1865 | # define MAYBE_BRACES "" |
| 1866 | #endif |
| 1867 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1868 | /* My analysis of quoting semantics tells me that state information |
| 1869 | * is associated with a destination, not a source. |
| 1870 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1871 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1872 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1873 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 1874 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1875 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1876 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1877 | o_grow_by(o, sz); |
| 1878 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1879 | o->data[o->length] = '\\'; |
| 1880 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1881 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1882 | o->data[o->length] = ch; |
| 1883 | o->length++; |
| 1884 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1887 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1888 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1889 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 1890 | if (o->o_escape && strchr("*?[\\" MAYBE_BRACES, ch)) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1891 | sz++; |
| 1892 | o->data[o->length] = '\\'; |
| 1893 | o->length++; |
| 1894 | } |
| 1895 | o_grow_by(o, sz); |
| 1896 | o->data[o->length] = ch; |
| 1897 | o->length++; |
| 1898 | o->data[o->length] = '\0'; |
| 1899 | } |
| 1900 | |
| 1901 | static void o_addQstr(o_string *o, const char *str, int len) |
| 1902 | { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1903 | if (!o->o_escape) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1904 | o_addblock(o, str, len); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1905 | return; |
| 1906 | } |
| 1907 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1908 | char ch; |
| 1909 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 1910 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1911 | if (ordinary_cnt > len) /* paranoia */ |
| 1912 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1913 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1914 | if (ordinary_cnt == len) |
| 1915 | return; |
| 1916 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1917 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1918 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1919 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1920 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 1921 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1922 | sz++; |
| 1923 | o->data[o->length] = '\\'; |
| 1924 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1925 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1926 | o_grow_by(o, sz); |
| 1927 | o->data[o->length] = ch; |
| 1928 | o->length++; |
| 1929 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1930 | } |
| 1931 | } |
| 1932 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1933 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1934 | * 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] | 1935 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1936 | * list[i] contains an INDEX (int!) into this string data. |
| 1937 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1938 | * but list[i]'s need not be modified. |
| 1939 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1940 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1941 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1942 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1943 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1944 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1945 | { |
| 1946 | char **list = (char**)o->data; |
| 1947 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1948 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1949 | |
| 1950 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1951 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1952 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1953 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1954 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1955 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1956 | o->data + (int)list[i] + string_start, |
| 1957 | o->data + (int)list[i] + string_start); |
| 1958 | i++; |
| 1959 | } |
| 1960 | if (n) { |
| 1961 | const char *p = o->data + (int)list[n - 1] + string_start; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1962 | indent(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1963 | fprintf(stderr, " total_sz:%ld\n", (long)((p + strlen(p) + 1) - o->data)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1964 | } |
| 1965 | } |
| 1966 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1967 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1968 | #endif |
| 1969 | |
| 1970 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1971 | * in list[n] so that it points past last stored byte so far. |
| 1972 | * It returns n+1. */ |
| 1973 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1974 | { |
| 1975 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1976 | int string_start; |
| 1977 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1978 | |
| 1979 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1980 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1981 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1982 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1983 | 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] | 1984 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1985 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1986 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 1987 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1988 | memmove(list + n + 0x10, list + n, string_len); |
| 1989 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1990 | } else { |
| 1991 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 1992 | n, string_len, string_start); |
| 1993 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1994 | } else { |
| 1995 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1996 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1997 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1998 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 1999 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2000 | o->has_empty_slot = 0; |
| 2001 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 2002 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2003 | return n + 1; |
| 2004 | } |
| 2005 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2006 | /* "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] | 2007 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2008 | { |
| 2009 | char **list = (char**)o->data; |
| 2010 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2011 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 2012 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2015 | #ifdef HUSH_BRACE_EXP |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2016 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 2017 | * first, it processes even {a} (no commas), second, |
| 2018 | * 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] | 2019 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2020 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2021 | |
| 2022 | /* Helper */ |
| 2023 | static int glob_needed(const char *s) |
| 2024 | { |
| 2025 | while (*s) { |
| 2026 | if (*s == '\\') { |
| 2027 | if (!s[1]) |
| 2028 | return 0; |
| 2029 | s += 2; |
| 2030 | continue; |
| 2031 | } |
| 2032 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 2033 | return 1; |
| 2034 | s++; |
| 2035 | } |
| 2036 | return 0; |
| 2037 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2038 | /* Return pointer to next closing brace or to comma */ |
| 2039 | static const char *next_brace_sub(const char *cp) |
| 2040 | { |
| 2041 | unsigned depth = 0; |
| 2042 | cp++; |
| 2043 | while (*cp != '\0') { |
| 2044 | if (*cp == '\\') { |
| 2045 | if (*++cp == '\0') |
| 2046 | break; |
| 2047 | cp++; |
| 2048 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 2049 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2050 | /*{*/ if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
| 2051 | break; |
| 2052 | if (*cp++ == '{') /*}*/ |
| 2053 | depth++; |
| 2054 | } |
| 2055 | |
| 2056 | return *cp != '\0' ? cp : NULL; |
| 2057 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2058 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 2059 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2060 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2061 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2062 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2063 | const char *next; |
| 2064 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2065 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2066 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2067 | |
| 2068 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 2069 | |
| 2070 | begin = pattern; |
| 2071 | while (1) { |
| 2072 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2073 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2074 | if (*begin == '{') /*}*/ { |
| 2075 | /* Find the first sub-pattern and at the same time |
| 2076 | * find the rest after the closing brace */ |
| 2077 | next = next_brace_sub(begin); |
| 2078 | if (next == NULL) { |
| 2079 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2080 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2081 | } |
| 2082 | /*{*/ if (*next == '}') { |
| 2083 | /* "{abc}" with no commas - illegal |
| 2084 | * brace expr, disregard and skip it */ |
| 2085 | begin = next + 1; |
| 2086 | continue; |
| 2087 | } |
| 2088 | break; |
| 2089 | } |
| 2090 | if (*begin == '\\' && begin[1] != '\0') |
| 2091 | begin++; |
| 2092 | begin++; |
| 2093 | } |
| 2094 | debug_printf_glob("begin:%s\n", begin); |
| 2095 | debug_printf_glob("next:%s\n", next); |
| 2096 | |
| 2097 | /* Now find the end of the whole brace expression */ |
| 2098 | rest = next; |
| 2099 | /*{*/ while (*rest != '}') { |
| 2100 | rest = next_brace_sub(rest); |
| 2101 | if (rest == NULL) { |
| 2102 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2103 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2104 | } |
| 2105 | debug_printf_glob("rest:%s\n", rest); |
| 2106 | } |
| 2107 | rest_len = strlen(++rest) + 1; |
| 2108 | |
| 2109 | /* We are sure the brace expression is well-formed */ |
| 2110 | |
| 2111 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2112 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2113 | |
| 2114 | /* We have a brace expression. BEGIN points to the opening {, |
| 2115 | * NEXT points past the terminator of the first element, and REST |
| 2116 | * points past the final }. We will accumulate result names from |
| 2117 | * recursive runs for each brace alternative in the buffer using |
| 2118 | * GLOB_APPEND. */ |
| 2119 | |
| 2120 | p = begin + 1; |
| 2121 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2122 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2123 | memcpy( |
| 2124 | mempcpy( |
| 2125 | mempcpy(new_pattern_buf, |
| 2126 | /* We know the prefix for all sub-patterns */ |
| 2127 | pattern, begin - pattern), |
| 2128 | p, next - p), |
| 2129 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2130 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2131 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 2132 | * That's why we re-copy prefix every time (1st memcpy above). |
| 2133 | */ |
| 2134 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2135 | /*{*/ if (*next == '}') { |
| 2136 | /* We saw the last entry */ |
| 2137 | break; |
| 2138 | } |
| 2139 | p = next + 1; |
| 2140 | next = next_brace_sub(next); |
| 2141 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2142 | free(new_pattern_buf); |
| 2143 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2144 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2145 | simple_glob: |
| 2146 | { |
| 2147 | int gr; |
| 2148 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2149 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2150 | memset(&globdata, 0, sizeof(globdata)); |
| 2151 | gr = glob(pattern, 0, NULL, &globdata); |
| 2152 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 2153 | if (gr != 0) { |
| 2154 | if (gr == GLOB_NOMATCH) { |
| 2155 | globfree(&globdata); |
| 2156 | /* NB: garbles parameter */ |
| 2157 | unbackslash(pattern); |
| 2158 | o_addstr_with_NUL(o, pattern); |
| 2159 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2160 | return o_save_ptr_helper(o, n); |
| 2161 | } |
| 2162 | if (gr == GLOB_NOSPACE) |
| 2163 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
| 2164 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2165 | * but we didn't specify it. Paranoia again. */ |
| 2166 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 2167 | } |
| 2168 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2169 | char **argv = globdata.gl_pathv; |
| 2170 | while (1) { |
| 2171 | o_addstr_with_NUL(o, *argv); |
| 2172 | n = o_save_ptr_helper(o, n); |
| 2173 | argv++; |
| 2174 | if (!*argv) |
| 2175 | break; |
| 2176 | } |
| 2177 | } |
| 2178 | globfree(&globdata); |
| 2179 | } |
| 2180 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2181 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2182 | /* Performs globbing on last list[], |
| 2183 | * saving each result as a new list[]. |
| 2184 | */ |
| 2185 | static int o_glob(o_string *o, int n) |
| 2186 | { |
| 2187 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2188 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2189 | debug_printf_glob("start o_glob: n:%d o->data:%p\n", n, o->data); |
| 2190 | if (!o->data) |
| 2191 | return o_save_ptr_helper(o, n); |
| 2192 | pattern = o->data + o_get_last_ptr(o, n); |
| 2193 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 2194 | if (!glob_needed(pattern)) { |
| 2195 | /* unbackslash last string in o in place, fix length */ |
| 2196 | o->length = unbackslash(pattern) - o->data; |
| 2197 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2198 | return o_save_ptr_helper(o, n); |
| 2199 | } |
| 2200 | |
| 2201 | copy = xstrdup(pattern); |
| 2202 | /* "forget" pattern in o */ |
| 2203 | o->length = pattern - o->data; |
| 2204 | n = glob_brace(copy, o, n); |
| 2205 | free(copy); |
| 2206 | if (DEBUG_GLOB) |
| 2207 | debug_print_list("o_glob returning", o, n); |
| 2208 | return n; |
| 2209 | } |
| 2210 | |
| 2211 | #else |
| 2212 | |
| 2213 | /* Helper */ |
| 2214 | static int glob_needed(const char *s) |
| 2215 | { |
| 2216 | while (*s) { |
| 2217 | if (*s == '\\') { |
| 2218 | if (!s[1]) |
| 2219 | return 0; |
| 2220 | s += 2; |
| 2221 | continue; |
| 2222 | } |
| 2223 | if (*s == '*' || *s == '[' || *s == '?') |
| 2224 | return 1; |
| 2225 | s++; |
| 2226 | } |
| 2227 | return 0; |
| 2228 | } |
| 2229 | /* Performs globbing on last list[], |
| 2230 | * saving each result as a new list[]. |
| 2231 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2232 | static int o_glob(o_string *o, int n) |
| 2233 | { |
| 2234 | glob_t globdata; |
| 2235 | int gr; |
| 2236 | char *pattern; |
| 2237 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2238 | debug_printf_glob("start o_glob: n:%d o->data:%p\n", n, o->data); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2239 | if (!o->data) |
| 2240 | return o_save_ptr_helper(o, n); |
| 2241 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2242 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2243 | if (!glob_needed(pattern)) { |
| 2244 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2245 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2246 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2247 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2248 | return o_save_ptr_helper(o, n); |
| 2249 | } |
| 2250 | |
| 2251 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2252 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 2253 | * If we glob "*.\*" and don't find anything, we need |
| 2254 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 2255 | * will return "*.\*"! |
| 2256 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2257 | gr = glob(pattern, 0, NULL, &globdata); |
| 2258 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2259 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2260 | if (gr == GLOB_NOMATCH) { |
| 2261 | globfree(&globdata); |
| 2262 | goto literal; |
| 2263 | } |
| 2264 | if (gr == GLOB_NOSPACE) |
| 2265 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2266 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2267 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2268 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2269 | } |
| 2270 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2271 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2272 | /* "forget" pattern in o */ |
| 2273 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2274 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2275 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2276 | n = o_save_ptr_helper(o, n); |
| 2277 | argv++; |
| 2278 | if (!*argv) |
| 2279 | break; |
| 2280 | } |
| 2281 | } |
| 2282 | globfree(&globdata); |
| 2283 | if (DEBUG_GLOB) |
| 2284 | debug_print_list("o_glob returning", o, n); |
| 2285 | return n; |
| 2286 | } |
| 2287 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2288 | #endif |
| 2289 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2290 | /* If o->o_glob == 1, glob the string so far remembered. |
| 2291 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2292 | static int o_save_ptr(o_string *o, int n) |
| 2293 | { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2294 | if (o->o_glob) { /* if globbing is requested */ |
| 2295 | /* If o->has_empty_slot, list[n] was already globbed |
| 2296 | * (if it was requested back then when it was filled) |
| 2297 | * so don't do that again! */ |
| 2298 | if (!o->has_empty_slot) |
| 2299 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 2300 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2301 | return o_save_ptr_helper(o, n); |
| 2302 | } |
| 2303 | |
| 2304 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2305 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2306 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2307 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2308 | int string_start; |
| 2309 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2310 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 2311 | if (DEBUG_EXPAND) |
| 2312 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2313 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2314 | list = (char**)o->data; |
| 2315 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2316 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2317 | while (n) { |
| 2318 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 2319 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2320 | } |
| 2321 | return list; |
| 2322 | } |
| 2323 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2324 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2325 | /* Expansion can recurse */ |
| 2326 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2327 | static int process_command_subs(o_string *dest, const char *s); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2328 | #endif |
| 2329 | static char *expand_string_to_string(const char *str); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2330 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2331 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2332 | parse_stream_dquoted(dest, input, dquote_end) |
| 2333 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2334 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2335 | o_string *dest, |
| 2336 | struct in_str *input, |
| 2337 | int dquote_end); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2338 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2339 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 2340 | * all variable references within and returns a pointer to |
| 2341 | * a list of expanded strings, possibly with larger number |
| 2342 | * of strings. (Think VAR="a b"; echo $VAR). |
| 2343 | * This new list is allocated as a single malloc block. |
| 2344 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 2345 | * followed by strings themself. |
| 2346 | * Caller can deallocate entire list by single free(list). */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2347 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2348 | /* Store given string, finalizing the word and starting new one whenever |
| 2349 | * we encounter IFS char(s). This is used for expanding variable values. |
| 2350 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 2351 | static int expand_on_ifs(o_string *output, int n, const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2352 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2353 | while (1) { |
| 2354 | int word_len = strcspn(str, G.ifs); |
| 2355 | if (word_len) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2356 | if (output->o_escape || !output->o_glob) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2357 | o_addQstr(output, str, word_len); |
| 2358 | else /* protect backslashes against globbing up :) */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2359 | o_addblock_duplicate_backslash(output, str, word_len); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2360 | str += word_len; |
| 2361 | } |
| 2362 | if (!*str) /* EOL - do not finalize word */ |
| 2363 | break; |
| 2364 | o_addchr(output, '\0'); |
| 2365 | debug_print_list("expand_on_ifs", output, n); |
| 2366 | n = o_save_ptr(output, n); |
| 2367 | str += strspn(str, G.ifs); /* skip ifs chars */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2368 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2369 | debug_print_list("expand_on_ifs[1]", output, n); |
| 2370 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2373 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 2374 | * they are in double quotes, with the exception that they are not :). |
| 2375 | * Just the rules are similar: "expand only $var and `cmd`" |
| 2376 | * |
| 2377 | * Returns malloced string. |
| 2378 | * As an optimization, we return NULL if expansion is not needed. |
| 2379 | */ |
| 2380 | static char *expand_pseudo_dquoted(const char *str) |
| 2381 | { |
| 2382 | char *exp_str; |
| 2383 | struct in_str input; |
| 2384 | o_string dest = NULL_O_STRING; |
| 2385 | |
| 2386 | if (strchr(str, '$') == NULL |
| 2387 | #if ENABLE_HUSH_TICK |
| 2388 | && strchr(str, '`') == NULL |
| 2389 | #endif |
| 2390 | ) { |
| 2391 | return NULL; |
| 2392 | } |
| 2393 | |
| 2394 | /* We need to expand. Example: |
| 2395 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 2396 | */ |
| 2397 | setup_string_in_str(&input, str); |
| 2398 | parse_stream_dquoted(NULL, &dest, &input, EOF); |
| 2399 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
| 2400 | exp_str = expand_string_to_string(dest.data); |
| 2401 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 2402 | o_free_unsafe(&dest); |
| 2403 | return exp_str; |
| 2404 | } |
| 2405 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2406 | /* Expand all variable references in given string, adding words to list[] |
| 2407 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 2408 | * to be filled). This routine is extremely tricky: has to deal with |
| 2409 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 2410 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | a7bb3c1 | 2009-10-08 12:28:08 +0200 | [diff] [blame] | 2411 | static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg, char or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2412 | { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 2413 | /* or_mask is either 0 (normal case) or 0x80 - |
| 2414 | * expansion of right-hand side of assignment == 1-element expand. |
| 2415 | * It will also do no globbing, and thus we must not backslash-quote! |
| 2416 | */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2417 | char ored_ch; |
| 2418 | char *p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2419 | |
| 2420 | ored_ch = 0; |
| 2421 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 2422 | debug_printf_expand("expand_vars_to_list: arg:'%s' or_mask:%x\n", arg, or_mask); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2423 | debug_print_list("expand_vars_to_list", output, n); |
| 2424 | n = o_save_ptr(output, n); |
| 2425 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 2426 | |
| 2427 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2428 | char first_ch; |
| 2429 | int i; |
| 2430 | char *dyn_val = NULL; |
| 2431 | const char *val = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2432 | #if ENABLE_HUSH_TICK |
| 2433 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2434 | #endif |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2435 | #if ENABLE_SH_MATH_SUPPORT |
| 2436 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 2437 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2438 | o_addblock(output, arg, p - arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2439 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 2440 | arg = ++p; |
| 2441 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 2442 | |
| 2443 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
| 2444 | /* "$@" is special. Even if quoted, it can still |
| 2445 | * expand to nothing (not even an empty string) */ |
| 2446 | if ((first_ch & 0x7f) != '@') |
| 2447 | ored_ch |= first_ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2448 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2449 | switch (first_ch & 0x7f) { |
| 2450 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 2451 | case '$': /* pid */ |
| 2452 | val = utoa(G.root_pid); |
| 2453 | break; |
| 2454 | case '!': /* bg pid */ |
| 2455 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
| 2456 | break; |
| 2457 | case '?': /* exitcode */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 2458 | val = utoa(G.last_exitcode); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2459 | break; |
| 2460 | case '#': /* argc */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2461 | if (arg[1] != SPECIAL_VAR_SYMBOL) |
| 2462 | /* actually, it's a ${#var} */ |
| 2463 | goto case_default; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2464 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 2465 | break; |
| 2466 | case '*': |
| 2467 | case '@': |
| 2468 | i = 1; |
| 2469 | if (!G.global_argv[i]) |
| 2470 | break; |
| 2471 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
| 2472 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2473 | smallint sv = output->o_escape; |
| 2474 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2475 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2476 | while (G.global_argv[i]) { |
| 2477 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 2478 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 2479 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 2480 | /* this argv[] is not empty and not last: |
| 2481 | * put terminating NUL, start new word */ |
| 2482 | o_addchr(output, '\0'); |
| 2483 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 2484 | n = o_save_ptr(output, n); |
| 2485 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 2486 | } |
| 2487 | } |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2488 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2489 | } else |
| 2490 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
| 2491 | * and in this case should treat it like '$*' - see 'else...' below */ |
| 2492 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
| 2493 | while (1) { |
| 2494 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2495 | if (++i >= G.global_argc) |
| 2496 | break; |
| 2497 | o_addchr(output, '\0'); |
| 2498 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 2499 | n = o_save_ptr(output, n); |
| 2500 | } |
| 2501 | } else { /* quoted $*: add as one word */ |
| 2502 | while (1) { |
| 2503 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2504 | if (!G.global_argv[++i]) |
| 2505 | break; |
| 2506 | if (G.ifs[0]) |
| 2507 | o_addchr(output, G.ifs[0]); |
| 2508 | } |
| 2509 | } |
| 2510 | break; |
| 2511 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 2512 | /* "Empty variable", used to make "" etc to not disappear */ |
| 2513 | arg++; |
| 2514 | ored_ch = 0x80; |
| 2515 | break; |
| 2516 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2517 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2518 | *p = '\0'; |
| 2519 | arg++; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2520 | /* Can't just stuff it into output o_string, |
| 2521 | * expanded result may need to be globbed |
| 2522 | * and $IFS-splitted */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2523 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 2524 | G.last_exitcode = process_command_subs(&subst_result, arg); |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 2525 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2526 | val = subst_result.data; |
| 2527 | goto store_val; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2528 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2529 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2530 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2531 | arith_eval_hooks_t hooks; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2532 | arith_t res; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2533 | int errcode; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2534 | char *exp_str; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2535 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2536 | arg++; /* skip '+' */ |
| 2537 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2538 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2539 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2540 | exp_str = expand_pseudo_dquoted(arg); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 2541 | hooks.lookupvar = get_local_var_value; |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2542 | hooks.setvar = set_local_var_from_halves; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2543 | hooks.endofname = endofname; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2544 | res = arith(exp_str ? exp_str : arg, &errcode, &hooks); |
| 2545 | free(exp_str); |
| 2546 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2547 | if (errcode < 0) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2548 | const char *msg = "error in arithmetic"; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2549 | switch (errcode) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2550 | case -3: |
| 2551 | msg = "exponent less than 0"; |
| 2552 | break; |
| 2553 | case -2: |
| 2554 | msg = "divide by 0"; |
| 2555 | break; |
| 2556 | case -5: |
| 2557 | msg = "expression recursion loop detected"; |
| 2558 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2559 | } |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2560 | die_if_script(msg); |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2561 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2562 | debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2563 | sprintf(arith_buf, arith_t_fmt, res); |
| 2564 | val = arith_buf; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2565 | break; |
| 2566 | } |
| 2567 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2568 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2569 | case_default: { |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2570 | char *var = arg; |
Denys Vlasenko | ee0775d | 2010-05-20 16:37:53 +0200 | [diff] [blame] | 2571 | char exp_len; /* '#' if it's ${#var} */ |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2572 | char exp_op; |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2573 | char exp_save = exp_save; /* for compiler */ |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2574 | char *exp_saveptr = exp_saveptr; /* points to expansion operator */ |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2575 | char *exp_word = exp_word; /* for compiler */ |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2576 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2577 | *p = '\0'; |
| 2578 | arg[0] = first_ch & 0x7f; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2579 | |
| 2580 | /* prepare for expansions */ |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2581 | exp_op = 0; |
Denys Vlasenko | ee0775d | 2010-05-20 16:37:53 +0200 | [diff] [blame] | 2582 | exp_len = var[0]; |
| 2583 | if (exp_len == '#') { |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2584 | /* handle length expansion ${#var} */ |
Denys Vlasenko | ee0775d | 2010-05-20 16:37:53 +0200 | [diff] [blame] | 2585 | var++; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2586 | } else { |
| 2587 | /* maybe handle parameter expansion */ |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 2588 | exp_saveptr = var + strcspn(var, "%#:-=+?"); |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2589 | exp_save = *exp_saveptr; |
| 2590 | if (exp_save) { |
| 2591 | exp_word = exp_saveptr; |
| 2592 | if (exp_save == ':') |
| 2593 | exp_word++; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2594 | exp_op = *exp_word++; |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2595 | *exp_saveptr = '\0'; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2596 | } |
| 2597 | } |
| 2598 | |
| 2599 | /* lookup the variable in question */ |
| 2600 | if (isdigit(var[0])) { |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 2601 | /* handle_dollar() should have vetted var for us */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2602 | i = xatoi_u(var); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2603 | if (i < G.global_argc) |
| 2604 | val = G.global_argv[i]; |
| 2605 | /* else val remains NULL: $N with too big N */ |
| 2606 | } else |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 2607 | val = get_local_var_value(var); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2608 | |
| 2609 | /* handle any expansions */ |
Denys Vlasenko | ee0775d | 2010-05-20 16:37:53 +0200 | [diff] [blame] | 2610 | if (exp_len == '#') { |
| 2611 | debug_printf_expand("expand: length(%s)=", val); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2612 | val = utoa(val ? strlen(val) : 0); |
| 2613 | debug_printf_expand("%s\n", val); |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2614 | } else if (exp_op) { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2615 | if (exp_op == '%' || exp_op == '#') { |
Denys Vlasenko | 53b5133 | 2010-05-20 21:46:45 +0200 | [diff] [blame] | 2616 | /* Standard-mandated substring removal ops: |
| 2617 | * ${parameter%word} - remove smallest suffix pattern |
| 2618 | * ${parameter%%word} - remove largest suffix pattern |
| 2619 | * ${parameter#word} - remove smallest prefix pattern |
| 2620 | * ${parameter##word} - remove largest prefix pattern |
| 2621 | * |
| 2622 | * Word is expanded to produce a glob pattern. |
| 2623 | * Then var's value is matched to it and matching part removed. |
| 2624 | */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2625 | if (val) { |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2626 | bool match_at_left; |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2627 | char *loc; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2628 | scan_t scan = pick_scan(exp_op, *exp_word, &match_at_left); |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2629 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2630 | exp_word++; |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2631 | val = dyn_val = xstrdup(val); |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2632 | loc = scan(dyn_val, exp_word, match_at_left); |
| 2633 | if (match_at_left) /* # or ## */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2634 | val = loc; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2635 | else if (loc) /* % or %% and match was found */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2636 | *loc = '\0'; |
| 2637 | } |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 2638 | } else if (!strchr("%#:-=+?"+3, exp_op)) { |
| 2639 | #if ENABLE_HUSH_BASH_COMPAT |
| 2640 | /* exp_op is ':' and next char isn't a subst operator. |
| 2641 | * Assuming it's ${var:[N][:M]} bashism. |
| 2642 | * TODO: N, M can be expressions similar to $((EXPR)): 2+2, 2+var etc |
| 2643 | */ |
| 2644 | char *end; |
| 2645 | unsigned len = INT_MAX; |
| 2646 | unsigned beg = 0; |
| 2647 | end = --exp_word; |
| 2648 | if (*exp_word != ':') /* not ${var::...} */ |
| 2649 | beg = bb_strtou(exp_word, &end, 0); |
Denys Vlasenko | 4f2c59b | 2010-05-21 01:25:16 +0200 | [diff] [blame^] | 2650 | //bb_error_msg("beg:'%s'=%u end:'%s'", exp_word, beg, end); |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 2651 | if (*end == ':') { |
| 2652 | len = bb_strtou(end + 1, &end, 0); |
| 2653 | //bb_error_msg("len:%u end:'%s'", len, end); |
| 2654 | } |
Denys Vlasenko | 4f2c59b | 2010-05-21 01:25:16 +0200 | [diff] [blame^] | 2655 | if (*end == '\0') { |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 2656 | //bb_error_msg("from val:'%s'", val); |
| 2657 | if (!val || beg >= strlen(val)) |
| 2658 | val = ""; |
| 2659 | else |
| 2660 | val = dyn_val = xstrndup(val + beg, len); |
| 2661 | //bb_error_msg("val:'%s'", val); |
| 2662 | } else |
| 2663 | #endif |
| 2664 | { |
| 2665 | die_if_script("malformed ${%s...}", var); |
| 2666 | } |
| 2667 | } else { /* one of "-=+?" */ |
Denys Vlasenko | 53b5133 | 2010-05-20 21:46:45 +0200 | [diff] [blame] | 2668 | /* Standard-mandated substitution ops: |
| 2669 | * ${var?word} - indicate error if unset |
| 2670 | * If var is unset, word (or a message indicating it is unset |
| 2671 | * if word is null) is written to standard error |
| 2672 | * and the shell exits with a non-zero exit status. |
| 2673 | * Otherwise, the value of var is substituted. |
| 2674 | * ${var-word} - use default value |
| 2675 | * If var is unset, word is substituted. |
| 2676 | * ${var=word} - assign and use default value |
| 2677 | * If var is unset, word is assigned to var. |
| 2678 | * In all cases, final value of var is substituted. |
| 2679 | * ${var+word} - use alternative value |
| 2680 | * If var is unset, null is substituted. |
| 2681 | * Otherwise, word is substituted. |
| 2682 | * |
| 2683 | * Word is subjected to tilde expansion, parameter expansion, |
| 2684 | * command substitution, and arithmetic expansion. |
| 2685 | * If word is not needed, it is not expanded. |
| 2686 | * |
| 2687 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 2688 | * but also treat null var as if it is unset. |
| 2689 | */ |
| 2690 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2691 | if (exp_op == '+') |
Denys Vlasenko | 53b5133 | 2010-05-20 21:46:45 +0200 | [diff] [blame] | 2692 | use_word = !use_word; |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2693 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
Denys Vlasenko | 53b5133 | 2010-05-20 21:46:45 +0200 | [diff] [blame] | 2694 | (exp_save == ':') ? "true" : "false", use_word); |
| 2695 | if (use_word) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2696 | if (exp_op == '?') { |
| 2697 | //TODO: how interactive bash aborts expansion mid-command? |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2698 | /* mimic bash message */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2699 | die_if_script("%s: %s", |
| 2700 | var, |
| 2701 | exp_word[0] ? exp_word : "parameter null or not set" |
| 2702 | ); |
| 2703 | } else { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2704 | val = exp_word; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2705 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2706 | |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2707 | if (exp_op == '=') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2708 | /* ${var=[word]} or ${var:=[word]} */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2709 | if (isdigit(var[0]) || var[0] == '#') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2710 | /* mimic bash message */ |
| 2711 | die_if_script("$%s: cannot assign in this way", var); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2712 | val = NULL; |
| 2713 | } else { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2714 | char *new_var = xasprintf("%s=%s", var, val); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2715 | set_local_var(new_var, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2716 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2717 | } |
| 2718 | } |
| 2719 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2720 | |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2721 | *exp_saveptr = exp_save; |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 2722 | } /* if (exp_op) */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2723 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2724 | arg[0] = first_ch; |
| 2725 | #if ENABLE_HUSH_TICK |
| 2726 | store_val: |
| 2727 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2728 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2729 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, output->o_escape); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2730 | if (val) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2731 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2732 | smallint sv = output->o_escape; |
| 2733 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2734 | n = expand_on_ifs(output, n, val); |
| 2735 | val = NULL; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2736 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2737 | } |
| 2738 | } else { /* quoted $VAR, val will be appended below */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2739 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, output->o_escape); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2740 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2741 | } /* default: */ |
| 2742 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2743 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2744 | if (val) { |
| 2745 | o_addQstr(output, val, strlen(val)); |
| 2746 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2747 | free(dyn_val); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2748 | /* Do the check to avoid writing to a const string */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2749 | if (*p != SPECIAL_VAR_SYMBOL) |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2750 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2751 | |
| 2752 | #if ENABLE_HUSH_TICK |
| 2753 | o_free(&subst_result); |
| 2754 | #endif |
| 2755 | arg = ++p; |
| 2756 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 2757 | |
| 2758 | if (arg[0]) { |
| 2759 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 2760 | /* this part is literal, and it was already pre-quoted |
| 2761 | * if needed (much earlier), do not use o_addQstr here! */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2762 | o_addstr_with_NUL(output, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2763 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 2764 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 2765 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 2766 | ) { |
| 2767 | n--; |
| 2768 | /* allow to reuse list[n] later without re-growth */ |
| 2769 | output->has_empty_slot = 1; |
| 2770 | } else { |
| 2771 | o_addchr(output, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2772 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2773 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2776 | static char **expand_variables(char **argv, int or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2777 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2778 | int n; |
| 2779 | char **list; |
| 2780 | char **v; |
| 2781 | o_string output = NULL_O_STRING; |
| 2782 | |
| 2783 | if (or_mask & 0x100) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2784 | output.o_escape = 1; /* protect against globbing for "$var" */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2785 | /* (unquoted $var will temporarily switch it off) */ |
| 2786 | output.o_glob = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2787 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2788 | |
| 2789 | n = 0; |
| 2790 | v = argv; |
| 2791 | while (*v) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2792 | n = expand_vars_to_list(&output, n, *v, (unsigned char)or_mask); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2793 | v++; |
| 2794 | } |
| 2795 | debug_print_list("expand_variables", &output, n); |
| 2796 | |
| 2797 | /* output.data (malloced in one block) gets returned in "list" */ |
| 2798 | list = o_finalize_list(&output, n); |
| 2799 | debug_print_strings("expand_variables[1]", list); |
| 2800 | return list; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2803 | static char **expand_strvec_to_strvec(char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2804 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2805 | return expand_variables(argv, 0x100); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 2808 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2809 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 2810 | { |
| 2811 | return expand_variables(argv, 0x80); |
| 2812 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 2813 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2814 | |
| 2815 | #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 2816 | static char **expand_strvec_to_strvec_singleword_noglob_cond(char **argv) |
| 2817 | { |
| 2818 | int n; |
| 2819 | char **list; |
| 2820 | char **v; |
| 2821 | o_string output = NULL_O_STRING; |
| 2822 | |
| 2823 | n = 0; |
| 2824 | v = argv; |
| 2825 | while (*v) { |
| 2826 | int is_var = is_well_formed_var_name(*v, '='); |
| 2827 | /* is_var * 0x80: singleword expansion for vars */ |
| 2828 | n = expand_vars_to_list(&output, n, *v, is_var * 0x80); |
| 2829 | |
| 2830 | /* Subtle! expand_vars_to_list did not glob last word yet. |
| 2831 | * It does this only when fed with further data. |
| 2832 | * Therefore we set globbing flags AFTER it, not before: |
| 2833 | */ |
| 2834 | |
| 2835 | /* if it is not recognizably abc=...; then: */ |
| 2836 | output.o_escape = !is_var; /* protect against globbing for "$var" */ |
| 2837 | /* (unquoted $var will temporarily switch it off) */ |
| 2838 | output.o_glob = !is_var; /* and indeed do globbing */ |
| 2839 | v++; |
| 2840 | } |
| 2841 | debug_print_list("expand_cond", &output, n); |
| 2842 | |
| 2843 | /* output.data (malloced in one block) gets returned in "list" */ |
| 2844 | list = o_finalize_list(&output, n); |
| 2845 | debug_print_strings("expand_cond[1]", list); |
| 2846 | return list; |
| 2847 | } |
| 2848 | #endif |
| 2849 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2850 | /* Used for expansion of right hand of assignments */ |
| 2851 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 2852 | * "v=/bin/c*" */ |
| 2853 | static char *expand_string_to_string(const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2854 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2855 | char *argv[2], **list; |
| 2856 | |
| 2857 | argv[0] = (char*)str; |
| 2858 | argv[1] = NULL; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2859 | list = expand_variables(argv, 0x80); /* 0x80: singleword expansion */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2860 | if (HUSH_DEBUG) |
| 2861 | if (!list[0] || list[1]) |
| 2862 | bb_error_msg_and_die("BUG in varexp2"); |
| 2863 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 2864 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 2865 | unbackslash((char*)list); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2866 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 2867 | return (char*)list; |
| 2868 | } |
| 2869 | |
| 2870 | /* Used for "eval" builtin */ |
| 2871 | static char* expand_strvec_to_string(char **argv) |
| 2872 | { |
| 2873 | char **list; |
| 2874 | |
| 2875 | list = expand_variables(argv, 0x80); |
| 2876 | /* Convert all NULs to spaces */ |
| 2877 | if (list[0]) { |
| 2878 | int n = 1; |
| 2879 | while (list[n]) { |
| 2880 | if (HUSH_DEBUG) |
| 2881 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 2882 | bb_error_msg_and_die("BUG in varexp3"); |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 2883 | /* bash uses ' ' regardless of $IFS contents */ |
| 2884 | list[n][-1] = ' '; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2885 | n++; |
| 2886 | } |
| 2887 | } |
| 2888 | overlapping_strcpy((char*)list, list[0]); |
| 2889 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 2890 | return (char*)list; |
| 2891 | } |
| 2892 | |
| 2893 | static char **expand_assignments(char **argv, int count) |
| 2894 | { |
| 2895 | int i; |
| 2896 | char **p = NULL; |
| 2897 | /* Expand assignments into one string each */ |
| 2898 | for (i = 0; i < count; i++) { |
| 2899 | p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
| 2900 | } |
| 2901 | return p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2904 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2905 | #if BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2906 | /* never called */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2907 | void re_execute_shell(char ***to_free, const char *s, |
| 2908 | char *g_argv0, char **g_argv, |
| 2909 | char **builtin_argv) NORETURN; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2910 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2911 | static void reset_traps_to_defaults(void) |
| 2912 | { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2913 | /* This function is always called in a child shell |
| 2914 | * after fork (not vfork, NOMMU doesn't use this function). |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2915 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2916 | unsigned sig; |
| 2917 | unsigned mask; |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2918 | |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2919 | /* Child shells are not interactive. |
| 2920 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 2921 | * Testcase: (while :; do :; done) + ^Z should background. |
| 2922 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 2923 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2924 | if (!G.traps && !(G.non_DFL_mask & SPECIAL_INTERACTIVE_SIGS)) |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2925 | return; /* already no traps and no SPECIAL_INTERACTIVE_SIGS */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2926 | |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2927 | /* Switching off SPECIAL_INTERACTIVE_SIGS. |
| 2928 | * Stupid. It can be done with *single* &= op, but we can't use |
| 2929 | * the fact that G.blocked_set is implemented as a bitmask |
| 2930 | * in libc... */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2931 | mask = (SPECIAL_INTERACTIVE_SIGS >> 1); |
| 2932 | sig = 1; |
| 2933 | while (1) { |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2934 | if (mask & 1) { |
| 2935 | /* Careful. Only if no trap or trap is not "" */ |
| 2936 | if (!G.traps || !G.traps[sig] || G.traps[sig][0]) |
| 2937 | sigdelset(&G.blocked_set, sig); |
| 2938 | } |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2939 | mask >>= 1; |
| 2940 | if (!mask) |
| 2941 | break; |
| 2942 | sig++; |
| 2943 | } |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2944 | /* Our homegrown sig mask is saner to work with :) */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2945 | G.non_DFL_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2946 | |
| 2947 | /* Resetting all traps to default except empty ones */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2948 | mask = G.non_DFL_mask; |
| 2949 | if (G.traps) for (sig = 0; sig < NSIG; sig++, mask >>= 1) { |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2950 | if (!G.traps[sig] || !G.traps[sig][0]) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2951 | continue; |
| 2952 | free(G.traps[sig]); |
| 2953 | G.traps[sig] = NULL; |
| 2954 | /* There is no signal for 0 (EXIT) */ |
| 2955 | if (sig == 0) |
| 2956 | continue; |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2957 | /* There was a trap handler, we just removed it. |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2958 | * But if sig still has non-DFL handling, |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2959 | * we should not unblock the sig. */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2960 | if (mask & 1) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2961 | continue; |
| 2962 | sigdelset(&G.blocked_set, sig); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2963 | } |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2964 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | #else /* !BB_MMU */ |
| 2968 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2969 | static void re_execute_shell(char ***to_free, const char *s, |
| 2970 | char *g_argv0, char **g_argv, |
| 2971 | char **builtin_argv) NORETURN; |
| 2972 | static void re_execute_shell(char ***to_free, const char *s, |
| 2973 | char *g_argv0, char **g_argv, |
| 2974 | char **builtin_argv) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2975 | { |
Denys Vlasenko | 6c93b24 | 2010-01-12 19:28:10 +0100 | [diff] [blame] | 2976 | #define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 2977 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 2978 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2979 | char *heredoc_argv[4]; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2980 | struct variable *cur; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2981 | # if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2982 | struct function *funcp; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2983 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2984 | char **argv, **pp; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2985 | unsigned cnt; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 2986 | unsigned long long empty_trap_mask; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2987 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2988 | if (!g_argv0) { /* heredoc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2989 | argv = heredoc_argv; |
| 2990 | argv[0] = (char *) G.argv0_for_re_execing; |
| 2991 | argv[1] = (char *) "-<"; |
| 2992 | argv[2] = (char *) s; |
| 2993 | argv[3] = NULL; |
Denis Vlasenko | f50caac | 2009-04-09 01:40:15 +0000 | [diff] [blame] | 2994 | pp = &argv[3]; /* used as pointer to empty environment */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2995 | goto do_exec; |
| 2996 | } |
| 2997 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2998 | cnt = 0; |
| 2999 | pp = builtin_argv; |
| 3000 | if (pp) while (*pp++) |
| 3001 | cnt++; |
| 3002 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 3003 | empty_trap_mask = 0; |
| 3004 | if (G.traps) { |
| 3005 | int sig; |
| 3006 | for (sig = 1; sig < NSIG; sig++) { |
| 3007 | if (G.traps[sig] && !G.traps[sig][0]) |
| 3008 | empty_trap_mask |= 1LL << sig; |
| 3009 | } |
| 3010 | } |
| 3011 | |
Denys Vlasenko | 6c93b24 | 2010-01-12 19:28:10 +0100 | [diff] [blame] | 3012 | sprintf(param_buf, NOMMU_HACK_FMT |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3013 | , (unsigned) G.root_pid |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 3014 | , (unsigned) G.root_ppid |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3015 | , (unsigned) G.last_bg_pid |
| 3016 | , (unsigned) G.last_exitcode |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3017 | , cnt |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 3018 | , empty_trap_mask |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 3019 | IF_HUSH_LOOPS(, G.depth_of_loop) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3020 | ); |
Denys Vlasenko | 6c93b24 | 2010-01-12 19:28:10 +0100 | [diff] [blame] | 3021 | #undef NOMMU_HACK_FMT |
| 3022 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3023 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3024 | */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3025 | cnt += 6; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3026 | for (cur = G.top_var; cur; cur = cur->next) { |
| 3027 | if (!cur->flg_export || cur->flg_read_only) |
| 3028 | cnt += 2; |
| 3029 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3030 | # if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3031 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 3032 | cnt += 3; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3033 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3034 | pp = g_argv; |
| 3035 | while (*pp++) |
| 3036 | cnt++; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3037 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3038 | *pp++ = (char *) G.argv0_for_re_execing; |
| 3039 | *pp++ = param_buf; |
| 3040 | for (cur = G.top_var; cur; cur = cur->next) { |
| 3041 | if (cur->varstr == hush_version_str) |
| 3042 | continue; |
| 3043 | if (cur->flg_read_only) { |
| 3044 | *pp++ = (char *) "-R"; |
| 3045 | *pp++ = cur->varstr; |
| 3046 | } else if (!cur->flg_export) { |
| 3047 | *pp++ = (char *) "-V"; |
| 3048 | *pp++ = cur->varstr; |
| 3049 | } |
| 3050 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3051 | # if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3052 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 3053 | *pp++ = (char *) "-F"; |
| 3054 | *pp++ = funcp->name; |
| 3055 | *pp++ = funcp->body_as_string; |
| 3056 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3057 | # endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3058 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 3059 | * |
| 3060 | * However, POSIX says that subshells reset signals with traps |
| 3061 | * to SIG_DFL. |
| 3062 | * I tested bash-3.2 and it not only does that with true subshells |
| 3063 | * of the form ( list ), but with any forked children shells. |
| 3064 | * I set trap "echo W" WINCH; and then tried: |
| 3065 | * |
| 3066 | * { echo 1; sleep 20; echo 2; } & |
| 3067 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 3068 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 3069 | * |
| 3070 | * In all these cases sending SIGWINCH to the child shell |
| 3071 | * did not run the trap. If I add trap "echo V" WINCH; |
| 3072 | * _inside_ group (just before echo 1), it works. |
| 3073 | * |
| 3074 | * I conclude it means we don't need to pass active traps here. |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 3075 | * Even if we would use signal handlers instead of signal masking |
| 3076 | * in order to implement trap handling, |
| 3077 | * exec syscall below resets signals to SIG_DFL for us. |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3078 | */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3079 | *pp++ = (char *) "-c"; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3080 | *pp++ = (char *) s; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3081 | if (builtin_argv) { |
| 3082 | while (*++builtin_argv) |
| 3083 | *pp++ = *builtin_argv; |
| 3084 | *pp++ = (char *) ""; |
| 3085 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3086 | *pp++ = g_argv0; |
| 3087 | while (*g_argv) |
| 3088 | *pp++ = *g_argv++; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3089 | /* *pp = NULL; - is already there */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3090 | pp = environ; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3091 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3092 | do_exec: |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3093 | debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s); |
| 3094 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3095 | execve(bb_busybox_exec_path, argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3096 | /* Fallback. Useful for init=/bin/hush usage etc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3097 | if (argv[0][0] == '/') |
| 3098 | execve(argv[0], argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3099 | xfunc_error_retval = 127; |
| 3100 | bb_error_msg_and_die("can't re-execute the shell"); |
| 3101 | } |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3102 | #endif /* !BB_MMU */ |
| 3103 | |
| 3104 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3105 | static void setup_heredoc(struct redir_struct *redir) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3106 | { |
| 3107 | struct fd_pair pair; |
| 3108 | pid_t pid; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3109 | int len, written; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3110 | /* the _body_ of heredoc (misleading field name) */ |
| 3111 | const char *heredoc = redir->rd_filename; |
| 3112 | char *expanded; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3113 | #if !BB_MMU |
| 3114 | char **to_free; |
| 3115 | #endif |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3116 | |
| 3117 | expanded = NULL; |
| 3118 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
| 3119 | expanded = expand_pseudo_dquoted(heredoc); |
| 3120 | if (expanded) |
| 3121 | heredoc = expanded; |
| 3122 | } |
| 3123 | len = strlen(heredoc); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3124 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3125 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3126 | xpiped_pair(pair); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3127 | xmove_fd(pair.rd, redir->rd_fd); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3128 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3129 | /* Try writing without forking. Newer kernels have |
| 3130 | * dynamically growing pipes. Must use non-blocking write! */ |
| 3131 | ndelay_on(pair.wr); |
| 3132 | while (1) { |
| 3133 | written = write(pair.wr, heredoc, len); |
| 3134 | if (written <= 0) |
| 3135 | break; |
| 3136 | len -= written; |
| 3137 | if (len == 0) { |
| 3138 | close(pair.wr); |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 3139 | free(expanded); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3140 | return; |
| 3141 | } |
| 3142 | heredoc += written; |
| 3143 | } |
| 3144 | ndelay_off(pair.wr); |
| 3145 | |
| 3146 | /* Okay, pipe buffer was not big enough */ |
| 3147 | /* Note: we must not create a stray child (bastard? :) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3148 | * for the unsuspecting parent process. Child creates a grandchild |
| 3149 | * and exits before parent execs the process which consumes heredoc |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3150 | * (that exec happens after we return from this function) */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 3151 | #if !BB_MMU |
| 3152 | to_free = NULL; |
| 3153 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3154 | pid = vfork(); |
| 3155 | if (pid < 0) |
| 3156 | bb_perror_msg_and_die("vfork"); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3157 | if (pid == 0) { |
| 3158 | /* child */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 3159 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3160 | pid = BB_MMU ? fork() : vfork(); |
| 3161 | if (pid < 0) |
| 3162 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
| 3163 | if (pid != 0) |
| 3164 | _exit(0); |
| 3165 | /* grandchild */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3166 | close(redir->rd_fd); /* read side of the pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3167 | #if BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3168 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3169 | _exit(0); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3170 | #else |
| 3171 | /* Delegate blocking writes to another process */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3172 | xmove_fd(pair.wr, STDOUT_FILENO); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3173 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3174 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3175 | } |
| 3176 | /* parent */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3177 | #if ENABLE_HUSH_FAST |
| 3178 | G.count_SIGCHLD++; |
| 3179 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 3180 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3181 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3182 | #if !BB_MMU |
| 3183 | free(to_free); |
| 3184 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3185 | close(pair.wr); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3186 | free(expanded); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3187 | wait(NULL); /* wait till child has died */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3190 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 3191 | * and stderr if they are redirected. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3192 | static int setup_redirects(struct command *prog, int squirrel[]) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3193 | { |
| 3194 | int openfd, mode; |
| 3195 | struct redir_struct *redir; |
| 3196 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3197 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3198 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3199 | /* rd_fd<<HERE case */ |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 3200 | if (squirrel && redir->rd_fd < 3 |
| 3201 | && squirrel[redir->rd_fd] < 0 |
| 3202 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3203 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3204 | } |
| 3205 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 3206 | * of the heredoc */ |
| 3207 | debug_printf_parse("set heredoc '%s'\n", |
| 3208 | redir->rd_filename); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3209 | setup_heredoc(redir); |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 3210 | continue; |
| 3211 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3212 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3213 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
| 3214 | /* rd_fd<*>file case (<*> is <,>,>>,<>) */ |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 3215 | char *p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3216 | if (redir->rd_filename == NULL) { |
| 3217 | /* Something went wrong in the parse. |
| 3218 | * Pretend it didn't happen */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3219 | bb_error_msg("bug in redirect parse"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3220 | continue; |
| 3221 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3222 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3223 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 3224 | openfd = open_or_warn(p, mode); |
| 3225 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3226 | if (openfd < 0) { |
| 3227 | /* this could get lost if stderr has been redirected, but |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3228 | * bash and ash both lose it as well (though zsh doesn't!) */ |
| 3229 | //what the above comment tries to say? |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3230 | return 1; |
| 3231 | } |
| 3232 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3233 | /* rd_fd<*>rd_dup or rd_fd<*>- cases */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3234 | openfd = redir->rd_dup; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3237 | if (openfd != redir->rd_fd) { |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 3238 | if (squirrel && redir->rd_fd < 3 |
| 3239 | && squirrel[redir->rd_fd] < 0 |
| 3240 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3241 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3242 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3243 | if (openfd == REDIRFD_CLOSE) { |
| 3244 | /* "n>-" means "close me" */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3245 | close(redir->rd_fd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3246 | } else { |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3247 | xdup2(openfd, redir->rd_fd); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3248 | if (redir->rd_dup == REDIRFD_TO_FILE) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3249 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3250 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3251 | } |
| 3252 | } |
| 3253 | return 0; |
| 3254 | } |
| 3255 | |
| 3256 | static void restore_redirects(int squirrel[]) |
| 3257 | { |
| 3258 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3259 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3260 | fd = squirrel[i]; |
| 3261 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3262 | /* We simply die on error */ |
| 3263 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3264 | } |
| 3265 | } |
| 3266 | } |
| 3267 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3268 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3269 | static void free_pipe_list(struct pipe *head); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3270 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3271 | /* Return code is the exit status of the pipe */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3272 | static void free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3273 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3274 | char **p; |
| 3275 | struct command *command; |
| 3276 | struct redir_struct *r, *rnext; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3277 | int a, i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3278 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3279 | if (pi->stopped_cmds > 0) /* why? */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3280 | return; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3281 | debug_printf_clean("run pipe: (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3282 | for (i = 0; i < pi->num_cmds; i++) { |
| 3283 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3284 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3285 | if (command->argv) { |
| 3286 | for (a = 0, p = command->argv; *p; a++, p++) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3287 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3288 | } |
| 3289 | free_strings(command->argv); |
| 3290 | command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3291 | } |
| 3292 | /* not "else if": on syntax error, we may have both! */ |
| 3293 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3294 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 3295 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3296 | free_pipe_list(command->group); |
| 3297 | debug_printf_clean(" end group\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3298 | command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3299 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3300 | /* else is crucial here. |
| 3301 | * If group != NULL, child_func is meaningless */ |
| 3302 | #if ENABLE_HUSH_FUNCTIONS |
| 3303 | else if (command->child_func) { |
| 3304 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 3305 | command->child_func->parent_cmd = NULL; |
| 3306 | } |
| 3307 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3308 | #if !BB_MMU |
| 3309 | free(command->group_as_string); |
| 3310 | command->group_as_string = NULL; |
| 3311 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3312 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3313 | debug_printf_clean(" redirect %d%s", |
| 3314 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3315 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3316 | if (r->rd_filename) { |
| 3317 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3318 | free(r->rd_filename); |
| 3319 | r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3320 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3321 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3322 | rnext = r->next; |
| 3323 | free(r); |
| 3324 | } |
| 3325 | command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3326 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3327 | free(pi->cmds); /* children are an array, they get freed all at once */ |
| 3328 | pi->cmds = NULL; |
| 3329 | #if ENABLE_HUSH_JOB |
| 3330 | free(pi->cmdtext); |
| 3331 | pi->cmdtext = NULL; |
| 3332 | #endif |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3333 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3334 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3335 | static void free_pipe_list(struct pipe *head) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3336 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3337 | struct pipe *pi, *next; |
| 3338 | |
| 3339 | for (pi = head; pi; pi = next) { |
| 3340 | #if HAS_KEYWORDS |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3341 | debug_printf_clean(" pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3342 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3343 | free_pipe(pi); |
| 3344 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3345 | next = pi->next; |
| 3346 | /*pi->next = NULL;*/ |
| 3347 | free(pi); |
| 3348 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
| 3351 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3352 | static int run_list(struct pipe *pi); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3353 | #if BB_MMU |
| 3354 | #define parse_stream(pstring, input, end_trigger) \ |
| 3355 | parse_stream(input, end_trigger) |
| 3356 | #endif |
| 3357 | static struct pipe *parse_stream(char **pstring, |
| 3358 | struct in_str *input, |
| 3359 | int end_trigger); |
| 3360 | static void parse_and_run_string(const char *s); |
| 3361 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3362 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 3363 | static char *find_in_path(const char *arg) |
| 3364 | { |
| 3365 | char *ret = NULL; |
| 3366 | const char *PATH = get_local_var_value("PATH"); |
| 3367 | |
| 3368 | if (!PATH) |
| 3369 | return NULL; |
| 3370 | |
| 3371 | while (1) { |
| 3372 | const char *end = strchrnul(PATH, ':'); |
| 3373 | int sz = end - PATH; /* must be int! */ |
| 3374 | |
| 3375 | free(ret); |
| 3376 | if (sz != 0) { |
| 3377 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 3378 | } else { |
| 3379 | /* We have xxx::yyyy in $PATH, |
| 3380 | * it means "use current dir" */ |
| 3381 | ret = xstrdup(arg); |
| 3382 | } |
| 3383 | if (access(ret, F_OK) == 0) |
| 3384 | break; |
| 3385 | |
| 3386 | if (*end == '\0') { |
| 3387 | free(ret); |
| 3388 | return NULL; |
| 3389 | } |
| 3390 | PATH = end + 1; |
| 3391 | } |
| 3392 | |
| 3393 | return ret; |
| 3394 | } |
| 3395 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3396 | static const struct built_in_command* find_builtin_helper(const char *name, |
| 3397 | const struct built_in_command *x, |
| 3398 | const struct built_in_command *end) |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3399 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3400 | while (x != end) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 3401 | if (strcmp(name, x->b_cmd) != 0) { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3402 | x++; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3403 | continue; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3404 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3405 | debug_printf_exec("found builtin '%s'\n", name); |
| 3406 | return x; |
| 3407 | } |
| 3408 | return NULL; |
| 3409 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3410 | static const struct built_in_command* find_builtin1(const char *name) |
| 3411 | { |
| 3412 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 3413 | } |
| 3414 | static const struct built_in_command* find_builtin(const char *name) |
| 3415 | { |
| 3416 | const struct built_in_command *x = find_builtin1(name); |
| 3417 | if (x) |
| 3418 | return x; |
| 3419 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 3420 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3421 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3422 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3423 | static struct function **find_function_slot(const char *name) |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3424 | { |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3425 | struct function **funcpp = &G.top_func; |
| 3426 | while (*funcpp) { |
| 3427 | if (strcmp(name, (*funcpp)->name) == 0) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3428 | break; |
| 3429 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3430 | funcpp = &(*funcpp)->next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3431 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3432 | return funcpp; |
| 3433 | } |
| 3434 | |
| 3435 | static const struct function *find_function(const char *name) |
| 3436 | { |
| 3437 | const struct function *funcp = *find_function_slot(name); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3438 | if (funcp) |
| 3439 | debug_printf_exec("found function '%s'\n", name); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3440 | return funcp; |
| 3441 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3442 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3443 | /* Note: takes ownership on name ptr */ |
| 3444 | static struct function *new_function(char *name) |
| 3445 | { |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3446 | struct function **funcpp = find_function_slot(name); |
| 3447 | struct function *funcp = *funcpp; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3448 | |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3449 | if (funcp != NULL) { |
| 3450 | struct command *cmd = funcp->parent_cmd; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3451 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 3452 | if (!cmd) { |
| 3453 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 3454 | free(funcp->name); |
| 3455 | /* Note: if !funcp->body, do not free body_as_string! |
| 3456 | * This is a special case of "-F name body" function: |
| 3457 | * body_as_string was not malloced! */ |
| 3458 | if (funcp->body) { |
| 3459 | free_pipe_list(funcp->body); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3460 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3461 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3462 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3463 | } |
| 3464 | } else { |
| 3465 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 3466 | cmd->argv[0] = funcp->name; |
| 3467 | cmd->group = funcp->body; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3468 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3469 | cmd->group_as_string = funcp->body_as_string; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3470 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3471 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3472 | } else { |
| 3473 | debug_printf_exec("remembering new function '%s'\n", name); |
| 3474 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 3475 | /*funcp->next = NULL;*/ |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3476 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3477 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3478 | funcp->name = name; |
| 3479 | return funcp; |
| 3480 | } |
| 3481 | |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3482 | static void unset_func(const char *name) |
| 3483 | { |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3484 | struct function **funcpp = find_function_slot(name); |
| 3485 | struct function *funcp = *funcpp; |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3486 | |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3487 | if (funcp != NULL) { |
| 3488 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 3489 | *funcpp = funcp->next; |
| 3490 | /* funcp is unlinked now, deleting it. |
| 3491 | * Note: if !funcp->body, the function was created by |
| 3492 | * "-F name body", do not free ->body_as_string |
| 3493 | * and ->name as they were not malloced. */ |
| 3494 | if (funcp->body) { |
| 3495 | free_pipe_list(funcp->body); |
| 3496 | free(funcp->name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3497 | # if !BB_MMU |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3498 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3499 | # endif |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3500 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3501 | free(funcp); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3502 | } |
| 3503 | } |
| 3504 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3505 | # if BB_MMU |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3506 | #define exec_function(to_free, funcp, argv) \ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3507 | exec_function(funcp, argv) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3508 | # endif |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3509 | static void exec_function(char ***to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3510 | const struct function *funcp, |
| 3511 | char **argv) NORETURN; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3512 | static void exec_function(char ***to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3513 | const struct function *funcp, |
| 3514 | char **argv) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3515 | { |
| 3516 | # if BB_MMU |
| 3517 | int n = 1; |
| 3518 | |
| 3519 | argv[0] = G.global_argv[0]; |
| 3520 | G.global_argv = argv; |
| 3521 | while (*++argv) |
| 3522 | n++; |
| 3523 | G.global_argc = n; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3524 | /* On MMU, funcp->body is always non-NULL */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3525 | n = run_list(funcp->body); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 3526 | fflush_all(); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3527 | _exit(n); |
| 3528 | # else |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3529 | re_execute_shell(to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3530 | funcp->body_as_string, |
| 3531 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3532 | argv + 1, |
| 3533 | NULL); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3534 | # endif |
| 3535 | } |
| 3536 | |
| 3537 | static int run_function(const struct function *funcp, char **argv) |
| 3538 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3539 | int rc; |
| 3540 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 3541 | smallint sv_flg; |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3542 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3543 | save_and_replace_G_args(&sv, argv); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3544 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3545 | /* "we are in function, ok to use return" */ |
| 3546 | sv_flg = G.flag_return_in_progress; |
| 3547 | G.flag_return_in_progress = -1; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3548 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3549 | G.func_nest_level++; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3550 | # endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3551 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3552 | /* On MMU, funcp->body is always non-NULL */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3553 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3554 | if (!funcp->body) { |
| 3555 | /* Function defined by -F */ |
| 3556 | parse_and_run_string(funcp->body_as_string); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3557 | rc = G.last_exitcode; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3558 | } else |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3559 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3560 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3561 | rc = run_list(funcp->body); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3562 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3563 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3564 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3565 | { |
| 3566 | struct variable *var; |
| 3567 | struct variable **var_pp; |
| 3568 | |
| 3569 | var_pp = &G.top_var; |
| 3570 | while ((var = *var_pp) != NULL) { |
| 3571 | if (var->func_nest_level < G.func_nest_level) { |
| 3572 | var_pp = &var->next; |
| 3573 | continue; |
| 3574 | } |
| 3575 | /* Unexport */ |
| 3576 | if (var->flg_export) |
| 3577 | bb_unsetenv(var->varstr); |
| 3578 | /* Remove from global list */ |
| 3579 | *var_pp = var->next; |
| 3580 | /* Free */ |
| 3581 | if (!var->max_len) |
| 3582 | free(var->varstr); |
| 3583 | free(var); |
| 3584 | } |
| 3585 | G.func_nest_level--; |
| 3586 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3587 | # endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3588 | G.flag_return_in_progress = sv_flg; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3589 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3590 | restore_G_args(&sv, argv); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3591 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3592 | return rc; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3593 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3594 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3595 | |
| 3596 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3597 | #if BB_MMU |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3598 | #define exec_builtin(to_free, x, argv) \ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3599 | exec_builtin(x, argv) |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3600 | #else |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3601 | #define exec_builtin(to_free, x, argv) \ |
| 3602 | exec_builtin(to_free, argv) |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3603 | #endif |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3604 | static void exec_builtin(char ***to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3605 | const struct built_in_command *x, |
| 3606 | char **argv) NORETURN; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3607 | static void exec_builtin(char ***to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3608 | const struct built_in_command *x, |
| 3609 | char **argv) |
| 3610 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3611 | #if BB_MMU |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 3612 | int rcode = x->b_function(argv); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 3613 | fflush_all(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3614 | _exit(rcode); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3615 | #else |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3616 | /* On NOMMU, we must never block! |
| 3617 | * Example: { sleep 99 | read line; } & echo Ok |
| 3618 | */ |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3619 | re_execute_shell(to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3620 | argv[0], |
| 3621 | G.global_argv[0], |
| 3622 | G.global_argv + 1, |
| 3623 | argv); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3624 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3625 | } |
| 3626 | |
| 3627 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 3628 | static void execvp_or_die(char **argv) NORETURN; |
| 3629 | static void execvp_or_die(char **argv) |
| 3630 | { |
| 3631 | debug_printf_exec("execing '%s'\n", argv[0]); |
| 3632 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 3633 | execvp(argv[0], argv); |
| 3634 | bb_perror_msg("can't execute '%s'", argv[0]); |
| 3635 | _exit(127); /* bash compat */ |
| 3636 | } |
| 3637 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 3638 | #if BB_MMU |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3639 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 3640 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 3641 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 3642 | pseudo_exec(command, argv_expanded) |
| 3643 | #endif |
| 3644 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3645 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3646 | * Never returns. |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3647 | * Don't exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3648 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3649 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3650 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 3651 | char **argv, int assignment_cnt, |
| 3652 | char **argv_expanded) NORETURN; |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 3653 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3654 | char **argv, int assignment_cnt, |
| 3655 | char **argv_expanded) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3656 | { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3657 | char **new_env; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3658 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3659 | /* Case when we are here: ... | var=val | ... */ |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3660 | if (!argv[assignment_cnt]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3661 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3662 | |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 3663 | new_env = expand_assignments(argv, assignment_cnt); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3664 | #if BB_MMU |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3665 | set_vars_and_save_old(new_env); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3666 | free(new_env); /* optional */ |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3667 | /* we can also destroy set_vars_and_save_old's return value, |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3668 | * to save memory */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3669 | #else |
| 3670 | nommu_save->new_env = new_env; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3671 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3672 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3673 | if (argv_expanded) { |
| 3674 | argv = argv_expanded; |
| 3675 | } else { |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 3676 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3677 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3678 | nommu_save->argv = argv; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3679 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3680 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 3681 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3682 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 3683 | if (strchr(argv[0], '/') != NULL) |
| 3684 | goto skip; |
| 3685 | #endif |
| 3686 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3687 | /* Check if the command matches any of the builtins. |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3688 | * Depending on context, this might be redundant. But it's |
| 3689 | * easier to waste a few CPU cycles than it is to figure out |
| 3690 | * if this is one of those cases. |
| 3691 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3692 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3693 | /* On NOMMU, it is more expensive to re-execute shell |
| 3694 | * just in order to run echo or test builtin. |
| 3695 | * It's better to skip it here and run corresponding |
| 3696 | * non-builtin later. */ |
| 3697 | const struct built_in_command *x; |
| 3698 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3699 | if (x) { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3700 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3701 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3702 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3703 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3704 | /* Check if the command matches any functions */ |
| 3705 | { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3706 | const struct function *funcp = find_function(argv[0]); |
| 3707 | if (funcp) { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3708 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3709 | } |
| 3710 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3711 | #endif |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3712 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 3713 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3714 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3715 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3716 | int a = find_applet_by_name(argv[0]); |
| 3717 | if (a >= 0) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3718 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3719 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3720 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3721 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3722 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3723 | # endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3724 | /* Re-exec ourselves */ |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3725 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3726 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 3727 | execv(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3728 | /* If they called chroot or otherwise made the binary no longer |
| 3729 | * executable, fall through */ |
| 3730 | } |
| 3731 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 3732 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3733 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3734 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3735 | skip: |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3736 | #endif |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 3737 | execvp_or_die(argv); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3738 | } |
| 3739 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3740 | /* Called after [v]fork() in run_pipe |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3741 | */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3742 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3743 | struct command *command, |
| 3744 | char **argv_expanded) NORETURN; |
| 3745 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3746 | struct command *command, |
| 3747 | char **argv_expanded) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3748 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3749 | if (command->argv) { |
| 3750 | pseudo_exec_argv(nommu_save, command->argv, |
| 3751 | command->assignment_cnt, argv_expanded); |
| 3752 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3753 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3754 | if (command->group) { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3755 | /* Cases when we are here: |
| 3756 | * ( list ) |
| 3757 | * { list } & |
| 3758 | * ... | ( list ) | ... |
| 3759 | * ... | { list } | ... |
| 3760 | */ |
| 3761 | #if BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 3762 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3763 | debug_printf_exec("pseudo_exec: run_list\n"); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3764 | reset_traps_to_defaults(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3765 | rcode = run_list(command->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 3766 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3767 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3768 | _exit(rcode); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3769 | #else |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3770 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 3771 | command->group_as_string, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3772 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3773 | G.global_argv + 1, |
| 3774 | NULL); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3775 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3776 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3777 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3778 | /* Case when we are here: ... | >file */ |
| 3779 | debug_printf_exec("pseudo_exec'ed null command\n"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3780 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3783 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3784 | static const char *get_cmdtext(struct pipe *pi) |
| 3785 | { |
| 3786 | char **argv; |
| 3787 | char *p; |
| 3788 | int len; |
| 3789 | |
| 3790 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 3791 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3792 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3793 | if (pi->cmdtext) |
| 3794 | return pi->cmdtext; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3795 | argv = pi->cmds[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3796 | if (!argv || !argv[0]) { |
| 3797 | pi->cmdtext = xzalloc(1); |
| 3798 | return pi->cmdtext; |
| 3799 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3800 | |
| 3801 | len = 0; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3802 | do { |
| 3803 | len += strlen(*argv) + 1; |
| 3804 | } while (*++argv); |
| 3805 | p = xmalloc(len); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3806 | pi->cmdtext = p; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3807 | argv = pi->cmds[0].argv; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3808 | do { |
| 3809 | len = strlen(*argv); |
| 3810 | memcpy(p, *argv, len); |
| 3811 | p += len; |
| 3812 | *p++ = ' '; |
| 3813 | } while (*++argv); |
| 3814 | p[-1] = '\0'; |
| 3815 | return pi->cmdtext; |
| 3816 | } |
| 3817 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3818 | static void insert_bg_job(struct pipe *pi) |
| 3819 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3820 | struct pipe *job, **jobp; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3821 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3822 | |
| 3823 | /* Linear search for the ID of the job to use */ |
| 3824 | pi->jobid = 1; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3825 | for (job = G.job_list; job; job = job->next) |
| 3826 | if (job->jobid >= pi->jobid) |
| 3827 | pi->jobid = job->jobid + 1; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3828 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3829 | /* Add job to the list of running jobs */ |
| 3830 | jobp = &G.job_list; |
| 3831 | while ((job = *jobp) != NULL) |
| 3832 | jobp = &job->next; |
| 3833 | job = *jobp = xmalloc(sizeof(*job)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3834 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3835 | *job = *pi; /* physical copy */ |
| 3836 | job->next = NULL; |
| 3837 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 3838 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3839 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3840 | job->cmds[i].pid = pi->cmds[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3841 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3842 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3843 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3844 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3845 | if (G_interactive_fd) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3846 | printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext); |
| 3847 | /* Last command's pid goes to $! */ |
| 3848 | G.last_bg_pid = job->cmds[job->num_cmds - 1].pid; |
| 3849 | G.last_jobid = job->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3852 | static void remove_bg_job(struct pipe *pi) |
| 3853 | { |
| 3854 | struct pipe *prev_pipe; |
| 3855 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3856 | if (pi == G.job_list) { |
| 3857 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3858 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3859 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3860 | while (prev_pipe->next != pi) |
| 3861 | prev_pipe = prev_pipe->next; |
| 3862 | prev_pipe->next = pi->next; |
| 3863 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3864 | if (G.job_list) |
| 3865 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3866 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3867 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3868 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3869 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3870 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3871 | static void delete_finished_bg_job(struct pipe *pi) |
| 3872 | { |
| 3873 | remove_bg_job(pi); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3874 | pi->stopped_cmds = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3875 | free_pipe(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3876 | free(pi); |
| 3877 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3878 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3879 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3880 | /* Check to see if any processes have exited -- if they |
| 3881 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3882 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3883 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3884 | int attributes; |
| 3885 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3886 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3887 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3888 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3889 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3890 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3891 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3892 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 3893 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3894 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3895 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3896 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3897 | |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3898 | errno = 0; |
| 3899 | #if ENABLE_HUSH_FAST |
| 3900 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 3901 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 3902 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
Denys Vlasenko | 68759ed | 2009-05-26 14:39:41 +0200 | [diff] [blame] | 3903 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 3904 | /* Avoid doing waitpid syscall if possible */ |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3905 | if (!G.we_have_children) { |
| 3906 | errno = ECHILD; |
| 3907 | return -1; |
| 3908 | } |
| 3909 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 3910 | /* We have children, but they did not exit |
| 3911 | * or stop yet (we saw no SIGCHLD) */ |
| 3912 | return 0; |
| 3913 | } |
| 3914 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 3915 | } |
| 3916 | #endif |
| 3917 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3918 | /* Do we do this right? |
| 3919 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3920 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3921 | * [3]+ Stopped sleep 20 | false |
| 3922 | * bash-3.00# echo $? |
| 3923 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3924 | * [hush 1.14.0: yes we do it right] |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3925 | */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3926 | wait_more: |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3927 | while (1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3928 | int i; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3929 | int dead; |
| 3930 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3931 | #if ENABLE_HUSH_FAST |
| 3932 | i = G.count_SIGCHLD; |
| 3933 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3934 | childpid = waitpid(-1, &status, attributes); |
| 3935 | if (childpid <= 0) { |
| 3936 | if (childpid && errno != ECHILD) |
| 3937 | bb_perror_msg("waitpid"); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3938 | #if ENABLE_HUSH_FAST |
| 3939 | else { /* Until next SIGCHLD, waitpid's are useless */ |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3940 | G.we_have_children = (childpid == 0); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3941 | G.handled_SIGCHLD = i; |
| 3942 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 3943 | } |
| 3944 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3945 | break; |
| 3946 | } |
| 3947 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 3948 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3949 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3950 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3951 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3952 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 3953 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3954 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3955 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 3956 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3957 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3958 | childpid, WEXITSTATUS(status)); |
| 3959 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3960 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3961 | if (fg_pipe) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3962 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 3963 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 3964 | if (fg_pipe->cmds[i].pid != childpid) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3965 | continue; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3966 | if (dead) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3967 | fg_pipe->cmds[i].pid = 0; |
| 3968 | fg_pipe->alive_cmds--; |
| 3969 | if (i == fg_pipe->num_cmds - 1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3970 | /* last process gives overall exitstatus */ |
| 3971 | rcode = WEXITSTATUS(status); |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 3972 | /* bash prints killer signal's name for *last* |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3973 | * process in pipe (prints just newline for SIGINT). |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 3974 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3975 | */ |
| 3976 | if (WIFSIGNALED(status)) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3977 | int sig = WTERMSIG(status); |
| 3978 | printf("%s\n", sig == SIGINT ? "" : get_signame(sig)); |
Denys Vlasenko | a4899ef | 2010-01-04 11:37:09 +0100 | [diff] [blame] | 3979 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 3980 | * Maybe we need to use sig | 128? */ |
| 3981 | rcode = sig + 128; |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3982 | } |
Denys Vlasenko | a4899ef | 2010-01-04 11:37:09 +0100 | [diff] [blame] | 3983 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3984 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3985 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3986 | fg_pipe->cmds[i].is_stopped = 1; |
| 3987 | fg_pipe->stopped_cmds++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3988 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3989 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 3990 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 3991 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3992 | /* All processes in fg pipe have exited or stopped */ |
| 3993 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 3994 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 3995 | * and "killall -STOP cat" */ |
| 3996 | if (G_interactive_fd) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3997 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3998 | if (fg_pipe->alive_cmds) |
| 3999 | insert_bg_job(fg_pipe); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4000 | #endif |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 4001 | return rcode; |
| 4002 | } |
Denys Vlasenko | 6f22624 | 2009-06-03 14:37:30 +0200 | [diff] [blame] | 4003 | if (!fg_pipe->alive_cmds) |
| 4004 | return rcode; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4005 | } |
| 4006 | /* There are still running processes in the fg pipe */ |
| 4007 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4008 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4009 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4012 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4013 | /* We asked to wait for bg or orphaned children */ |
| 4014 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4015 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4016 | for (i = 0; i < pi->num_cmds; i++) { |
| 4017 | if (pi->cmds[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 4018 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4019 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4020 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 4021 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 4022 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4023 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4024 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 4025 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4026 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4027 | /* child exited */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4028 | pi->cmds[i].pid = 0; |
| 4029 | pi->alive_cmds--; |
| 4030 | if (!pi->alive_cmds) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4031 | if (G_interactive_fd) |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 4032 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 4033 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4034 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4035 | } |
| 4036 | } else { |
| 4037 | /* child stopped */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4038 | pi->cmds[i].is_stopped = 1; |
| 4039 | pi->stopped_cmds++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4040 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4041 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4042 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4043 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4044 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4047 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 4048 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 4049 | { |
| 4050 | pid_t p; |
| 4051 | int rcode = checkjobs(fg_pipe); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 4052 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 4053 | /* Job finished, move the shell to the foreground */ |
| 4054 | p = getpgrp(); /* our process group id */ |
| 4055 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 4056 | tcsetpgrp(G_interactive_fd, p); |
| 4057 | } |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 4058 | return rcode; |
| 4059 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4060 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 4061 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 4062 | /* Start all the jobs, but don't wait for anything to finish. |
| 4063 | * See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4064 | * |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4065 | * Return code is normally -1, when the caller has to wait for children |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4066 | * to finish to determine the exit status of the pipe. If the pipe |
| 4067 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4068 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4069 | * return value. |
| 4070 | * |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4071 | * Returns -1 only if started some children. IOW: we have to |
| 4072 | * mask out retvals of builtins etc with 0xff! |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4073 | * |
| 4074 | * The only case when we do not need to [v]fork is when the pipe |
| 4075 | * is single, non-backgrounded, non-subshell command. Examples: |
| 4076 | * cmd ; ... { list } ; ... |
| 4077 | * cmd && ... { list } && ... |
| 4078 | * cmd || ... { list } || ... |
| 4079 | * If it is, then we can run cmd as a builtin, NOFORK [do we do this?], |
| 4080 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4081 | * with run_list. If it isn't one of these, we fork and exec cmd. |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4082 | * |
| 4083 | * Cases when we must fork: |
| 4084 | * non-single: cmd | cmd |
| 4085 | * backgrounded: cmd & { list } & |
| 4086 | * subshell: ( list ) [&] |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4087 | */ |
Denys Vlasenko | a7bb3c1 | 2009-10-08 12:28:08 +0200 | [diff] [blame] | 4088 | static NOINLINE int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4089 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4090 | static const char *const null_ptr = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4091 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4092 | int nextin; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4093 | struct command *command; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4094 | char **argv_expanded; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4095 | char **argv; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4096 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 4097 | /* it is not always needed, but we aim to smaller code */ |
| 4098 | int squirrel[] = { -1, -1, -1 }; |
| 4099 | int rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4100 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4101 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4102 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4103 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4104 | IF_HUSH_JOB(pi->pgrp = -1;) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4105 | pi->stopped_cmds = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4106 | command = &(pi->cmds[0]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4107 | argv_expanded = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4108 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4109 | if (pi->num_cmds != 1 |
| 4110 | || pi->followup == PIPE_BG |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4111 | || command->cmd_type == CMD_SUBSHELL |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4112 | ) { |
| 4113 | goto must_fork; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4114 | } |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4115 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4116 | pi->alive_cmds = 1; |
| 4117 | |
| 4118 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 4119 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 4120 | |
| 4121 | if (command->group) { |
| 4122 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4123 | if (command->cmd_type == CMD_FUNCDEF) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4124 | /* "executing" func () { list } */ |
| 4125 | struct function *funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4126 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 4127 | funcp = new_function(command->argv[0]); |
| 4128 | /* funcp->name is already set to argv[0] */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4129 | funcp->body = command->group; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4130 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4131 | funcp->body_as_string = command->group_as_string; |
| 4132 | command->group_as_string = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4133 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4134 | command->group = NULL; |
| 4135 | command->argv[0] = NULL; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 4136 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 4137 | funcp->parent_cmd = command; |
| 4138 | command->child_func = funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4139 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4140 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 4141 | debug_leave(); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4142 | return EXIT_SUCCESS; |
| 4143 | } |
| 4144 | #endif |
| 4145 | /* { list } */ |
| 4146 | debug_printf("non-subshell group\n"); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4147 | rcode = 1; /* exitcode if redir failed */ |
| 4148 | if (setup_redirects(command, squirrel) == 0) { |
| 4149 | debug_printf_exec(": run_list\n"); |
| 4150 | rcode = run_list(command->group) & 0xff; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4151 | } |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 4152 | restore_redirects(squirrel); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4153 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4154 | debug_leave(); |
| 4155 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4156 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4157 | } |
| 4158 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4159 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 4160 | { |
| 4161 | const struct built_in_command *x; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4162 | #if ENABLE_HUSH_FUNCTIONS |
| 4163 | const struct function *funcp; |
| 4164 | #else |
| 4165 | enum { funcp = 0 }; |
| 4166 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4167 | char **new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4168 | struct variable *old_vars = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4169 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4170 | if (argv[command->assignment_cnt] == NULL) { |
| 4171 | /* Assignments, but no command */ |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 4172 | /* Ensure redirects take effect (that is, create files). |
| 4173 | * Try "a=t >file": */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4174 | rcode = setup_redirects(command, squirrel); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4175 | restore_redirects(squirrel); |
| 4176 | /* Set shell variables */ |
| 4177 | while (*argv) { |
| 4178 | p = expand_string_to_string(*argv); |
| 4179 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 4180 | *argv, p); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4181 | set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4182 | argv++; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 4183 | } |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 4184 | /* Redirect error sets $? to 1. Othervise, |
| 4185 | * if evaluating assignment value set $?, retain it. |
| 4186 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 4187 | if (rcode == 0) |
| 4188 | rcode = G.last_exitcode; |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4189 | /* Do we need to flag set_local_var() errors? |
| 4190 | * "assignment to readonly var" and "putenv error" |
| 4191 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4192 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4193 | debug_leave(); |
| 4194 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4195 | return rcode; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 4196 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4197 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4198 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4199 | if (0) {} |
| 4200 | #if ENABLE_HUSH_BASH_COMPAT |
| 4201 | else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4202 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
| 4203 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4204 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4205 | #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 4206 | else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB_COND) { |
| 4207 | argv_expanded = expand_strvec_to_strvec_singleword_noglob_cond(argv + command->assignment_cnt); |
| 4208 | |
| 4209 | } |
| 4210 | #endif |
| 4211 | else { |
| 4212 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
| 4213 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4214 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4215 | /* if someone gives us an empty string: `cmd with empty output` */ |
Mike Frysinger | 28736c3 | 2009-10-18 01:11:45 -0400 | [diff] [blame] | 4216 | if (!argv_expanded[0]) { |
Denys Vlasenko | 385cc59 | 2010-01-12 06:47:39 +0100 | [diff] [blame] | 4217 | free(argv_expanded); |
Mike Frysinger | 28736c3 | 2009-10-18 01:11:45 -0400 | [diff] [blame] | 4218 | debug_leave(); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 4219 | return G.last_exitcode; |
Mike Frysinger | 28736c3 | 2009-10-18 01:11:45 -0400 | [diff] [blame] | 4220 | } |
| 4221 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4222 | x = find_builtin(argv_expanded[0]); |
| 4223 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4224 | funcp = NULL; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4225 | if (!x) |
| 4226 | funcp = find_function(argv_expanded[0]); |
| 4227 | #endif |
| 4228 | if (x || funcp) { |
| 4229 | if (!funcp) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 4230 | if (x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4231 | debug_printf("exec with redirects only\n"); |
| 4232 | rcode = setup_redirects(command, NULL); |
| 4233 | goto clean_up_and_ret1; |
| 4234 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4235 | } |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 4236 | /* setup_redirects acts on file descriptors, not FILEs. |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4237 | * This is perfect for work that comes after exec(). |
| 4238 | * Is it really safe for inline use? Experimentally, |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 4239 | * things seem to work. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4240 | rcode = setup_redirects(command, squirrel); |
| 4241 | if (rcode == 0) { |
| 4242 | new_env = expand_assignments(argv, command->assignment_cnt); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 4243 | old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4244 | if (!funcp) { |
| 4245 | debug_printf_exec(": builtin '%s' '%s'...\n", |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 4246 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 4247 | rcode = x->b_function(argv_expanded) & 0xff; |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 4248 | fflush_all(); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4249 | } |
| 4250 | #if ENABLE_HUSH_FUNCTIONS |
| 4251 | else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4252 | # if ENABLE_HUSH_LOCAL |
| 4253 | struct variable **sv; |
| 4254 | sv = G.shadowed_vars_pp; |
| 4255 | G.shadowed_vars_pp = &old_vars; |
| 4256 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4257 | debug_printf_exec(": function '%s' '%s'...\n", |
| 4258 | funcp->name, argv_expanded[1]); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 4259 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4260 | # if ENABLE_HUSH_LOCAL |
| 4261 | G.shadowed_vars_pp = sv; |
| 4262 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4263 | } |
| 4264 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4265 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4266 | #if ENABLE_FEATURE_SH_STANDALONE |
| 4267 | clean_up_and_ret: |
| 4268 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4269 | restore_redirects(squirrel); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 4270 | unset_vars(new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4271 | add_vars(old_vars); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4272 | clean_up_and_ret1: |
| 4273 | free(argv_expanded); |
| 4274 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4275 | debug_leave(); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4276 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 4277 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4278 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4279 | |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 4280 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4281 | i = find_applet_by_name(argv_expanded[0]); |
| 4282 | if (i >= 0 && APPLET_IS_NOFORK(i)) { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4283 | rcode = setup_redirects(command, squirrel); |
| 4284 | if (rcode == 0) { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4285 | new_env = expand_assignments(argv, command->assignment_cnt); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 4286 | old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4287 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4288 | argv_expanded[0], argv_expanded[1]); |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4289 | rcode = run_nofork_applet(i, argv_expanded); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4290 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4291 | goto clean_up_and_ret; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 4292 | } |
| 4293 | #endif |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4294 | /* It is neither builtin nor applet. We must fork. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4295 | } |
| 4296 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4297 | must_fork: |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4298 | /* NB: argv_expanded may already be created, and that |
| 4299 | * might include `cmd` runs! Do not rerun it! We *must* |
| 4300 | * use argv_expanded if it's non-NULL */ |
| 4301 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4302 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4303 | pi->alive_cmds = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4304 | nextin = 0; |
| 4305 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4306 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4307 | struct fd_pair pipefds; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 4308 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4309 | volatile nommu_save_t nommu_save; |
| 4310 | nommu_save.new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4311 | nommu_save.old_vars = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4312 | nommu_save.argv = NULL; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4313 | nommu_save.argv_from_re_execing = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 4314 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4315 | command = &(pi->cmds[i]); |
| 4316 | if (command->argv) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4317 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 4318 | command->argv[0], command->argv[1]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4319 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4320 | debug_printf_exec(": pipe member with no argv\n"); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4321 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4322 | |
| 4323 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4324 | pipefds.rd = 0; |
| 4325 | pipefds.wr = 1; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4326 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4327 | xpiped_pair(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4328 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4329 | command->pid = BB_MMU ? fork() : vfork(); |
| 4330 | if (!command->pid) { /* child */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4331 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4332 | disable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4333 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4334 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4335 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4336 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4337 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4338 | pid_t pgrp; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4339 | pgrp = pi->pgrp; |
| 4340 | if (pgrp < 0) /* true for 1st process only */ |
| 4341 | pgrp = getpid(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 4342 | if (setpgid(0, pgrp) == 0 |
| 4343 | && pi->followup != PIPE_BG |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 4344 | && G_saved_tty_pgrp /* we have ctty */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 4345 | ) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4346 | /* We do it in *every* child, not just first, |
| 4347 | * to avoid races */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4348 | tcsetpgrp(G_interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4349 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4350 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4351 | #endif |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4352 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 4353 | /* 1st cmd in backgrounded pipe |
| 4354 | * should have its stdin /dev/null'ed */ |
| 4355 | close(0); |
| 4356 | if (open(bb_dev_null, O_RDONLY)) |
| 4357 | xopen("/", O_RDONLY); |
| 4358 | } else { |
| 4359 | xmove_fd(nextin, 0); |
| 4360 | } |
| 4361 | xmove_fd(pipefds.wr, 1); |
| 4362 | if (pipefds.rd > 1) |
| 4363 | close(pipefds.rd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4364 | /* Like bash, explicit redirects override pipes, |
| 4365 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4366 | if (setup_redirects(command, NULL)) |
| 4367 | _exit(1); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4368 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4369 | /* Restore default handlers just prior to exec */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 4370 | /*signal(SIGCHLD, SIG_DFL); - so far we don't have any handlers */ |
| 4371 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4372 | /* Stores to nommu_save list of env vars putenv'ed |
| 4373 | * (NOMMU, on MMU we don't need that) */ |
| 4374 | /* cast away volatility... */ |
| 4375 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4376 | /* pseudo_exec() does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4377 | } |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 4378 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 4379 | /* parent or error */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 4380 | #if ENABLE_HUSH_FAST |
| 4381 | G.count_SIGCHLD++; |
| 4382 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 4383 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4384 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4385 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4386 | /* Clean up after vforked child */ |
| 4387 | free(nommu_save.argv); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4388 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 4389 | unset_vars(nommu_save.new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4390 | add_vars(nommu_save.old_vars); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4391 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4392 | free(argv_expanded); |
| 4393 | argv_expanded = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4394 | if (command->pid < 0) { /* [v]fork failed */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4395 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 4396 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4397 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4398 | pi->alive_cmds++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4399 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4400 | /* Second and next children need to know pid of first one */ |
| 4401 | if (pi->pgrp < 0) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4402 | pi->pgrp = command->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4403 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4404 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4405 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4406 | if (i) |
| 4407 | close(nextin); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4408 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4409 | close(pipefds.wr); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4410 | /* Pass read (output) pipe end to next iteration */ |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4411 | nextin = pipefds.rd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4412 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4413 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4414 | if (!pi->alive_cmds) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4415 | debug_leave(); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4416 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 4417 | return 1; |
| 4418 | } |
| 4419 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4420 | debug_leave(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4421 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4422 | return -1; |
| 4423 | } |
| 4424 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 4425 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4426 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 4427 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4428 | static const char *const PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4429 | [PIPE_SEQ] = "SEQ", |
| 4430 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4431 | [PIPE_OR ] = "OR" , |
| 4432 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4433 | }; |
| 4434 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4435 | [RES_NONE ] = "NONE" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4436 | # if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4437 | [RES_IF ] = "IF" , |
| 4438 | [RES_THEN ] = "THEN" , |
| 4439 | [RES_ELIF ] = "ELIF" , |
| 4440 | [RES_ELSE ] = "ELSE" , |
| 4441 | [RES_FI ] = "FI" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4442 | # endif |
| 4443 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4444 | [RES_FOR ] = "FOR" , |
| 4445 | [RES_WHILE] = "WHILE", |
| 4446 | [RES_UNTIL] = "UNTIL", |
| 4447 | [RES_DO ] = "DO" , |
| 4448 | [RES_DONE ] = "DONE" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4449 | # endif |
| 4450 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4451 | [RES_IN ] = "IN" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4452 | # endif |
| 4453 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4454 | [RES_CASE ] = "CASE" , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4455 | [RES_CASE_IN ] = "CASE_IN" , |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4456 | [RES_MATCH] = "MATCH", |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4457 | [RES_CASE_BODY] = "CASE_BODY", |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4458 | [RES_ESAC ] = "ESAC" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4459 | # endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4460 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4461 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4462 | }; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4463 | static const char *const CMDTYPE[] = { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4464 | "{}", |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 4465 | "()", |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4466 | "[noglob]", |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4467 | # if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4468 | "func()", |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4469 | # endif |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4470 | }; |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4471 | |
| 4472 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4473 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4474 | pin = 0; |
| 4475 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4476 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 4477 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4478 | prn = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4479 | while (prn < pi->num_cmds) { |
| 4480 | struct command *command = &pi->cmds[prn]; |
| 4481 | char **argv = command->argv; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4482 | |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4483 | fprintf(stderr, "%*s cmd %d assignment_cnt:%d", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4484 | lvl*2, "", prn, |
| 4485 | command->assignment_cnt); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4486 | if (command->group) { |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4487 | fprintf(stderr, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4488 | CMDTYPE[command->cmd_type], |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4489 | argv |
| 4490 | #if !BB_MMU |
| 4491 | , " group_as_string:", command->group_as_string |
| 4492 | #else |
| 4493 | , "", "" |
| 4494 | #endif |
| 4495 | ); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4496 | debug_print_tree(command->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4497 | prn++; |
| 4498 | continue; |
| 4499 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4500 | if (argv) while (*argv) { |
| 4501 | fprintf(stderr, " '%s'", *argv); |
| 4502 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 4503 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4504 | fprintf(stderr, "\n"); |
| 4505 | prn++; |
| 4506 | } |
| 4507 | pi = pi->next; |
| 4508 | pin++; |
| 4509 | } |
| 4510 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4511 | #endif /* debug_print_tree */ |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4512 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 4513 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 4514 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4515 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4516 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4517 | #if ENABLE_HUSH_CASE |
| 4518 | char *case_word = NULL; |
| 4519 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4520 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4521 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4522 | char **for_lcur = NULL; |
| 4523 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4524 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4525 | smallint last_followup; |
| 4526 | smalluint rcode; |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 4527 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4528 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4529 | #else |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4530 | enum { cond_code = 0 }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4531 | #endif |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4532 | #if HAS_KEYWORDS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4533 | smallint rword; /* enum reserved_style */ |
| 4534 | smallint last_rword; /* ditto */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4535 | #endif |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4536 | |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4537 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4538 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4539 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4540 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4541 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4542 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 4543 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4544 | continue; |
| 4545 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4546 | if (cpipe->next == NULL) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4547 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4548 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4549 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4550 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4551 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4552 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4553 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4554 | continue; |
| 4555 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4556 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 4557 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4558 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4559 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4560 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4561 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4562 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4563 | } |
| 4564 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4565 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4566 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4567 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 4568 | * in order to return, no direct "return" statements please. |
| 4569 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4570 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4571 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4572 | G.run_list_level++; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4573 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4574 | |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4575 | #if HAS_KEYWORDS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4576 | rword = RES_NONE; |
| 4577 | last_rword = RES_XXXX; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4578 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4579 | last_followup = PIPE_SEQ; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4580 | rcode = G.last_exitcode; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4581 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4582 | /* Go through list of pipes, (maybe) executing them. */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4583 | for (; pi; pi = IF_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 4584 | if (G.flag_SIGINT) |
| 4585 | break; |
| 4586 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4587 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4588 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 4589 | rword, cond_code, last_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4590 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 4591 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4592 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 4593 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4594 | /* start of a loop: remember where loop starts */ |
| 4595 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4596 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4597 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4598 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4599 | /* Still in the same "if...", "then..." or "do..." branch? */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4600 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4601 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 4602 | || (rcode != 0 && last_followup == PIPE_AND) |
| 4603 | ) { |
| 4604 | /* It is "<true> || CMD" or "<false> && CMD" |
| 4605 | * and we should not execute CMD */ |
| 4606 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 4607 | last_followup = pi->followup; |
| 4608 | continue; |
| 4609 | } |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4610 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4611 | last_followup = pi->followup; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4612 | IF_HAS_KEYWORDS(last_rword = rword;) |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4613 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4614 | if (cond_code) { |
| 4615 | if (rword == RES_THEN) { |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4616 | /* if false; then ... fi has exitcode 0! */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4617 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4618 | /* "if <false> THEN cmd": skip cmd */ |
| 4619 | continue; |
| 4620 | } |
| 4621 | } else { |
| 4622 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 4623 | /* "if <true> then ... ELSE/ELIF cmd": |
| 4624 | * skip cmd and all following ones */ |
| 4625 | break; |
| 4626 | } |
| 4627 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4628 | #endif |
| 4629 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4630 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4631 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 4632 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4633 | |
| 4634 | static const char encoded_dollar_at[] ALIGN1 = { |
| 4635 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 4636 | }; /* encoded representation of "$@" */ |
| 4637 | static const char *const encoded_dollar_at_argv[] = { |
| 4638 | encoded_dollar_at, NULL |
| 4639 | }; /* argv list with one element: "$@" */ |
| 4640 | char **vals; |
| 4641 | |
| 4642 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4643 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4644 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4645 | if (!pi->next->cmds[0].argv) { |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4646 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4647 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4648 | break; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4649 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4650 | vals = pi->next->cmds[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4651 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4652 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4653 | debug_print_strings("for_list made from", vals); |
| 4654 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4655 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4656 | debug_print_strings("for_list", for_list); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4657 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4658 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 4659 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4660 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4661 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4662 | for_lcur = NULL; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4663 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4664 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4665 | /* Insert next value from for_lcur */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4666 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4667 | set_local_var(xasprintf("%s=%s", pi->cmds[0].argv[0], *for_lcur++), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4668 | continue; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4669 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4670 | if (rword == RES_IN) { |
| 4671 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 4672 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4673 | if (rword == RES_DONE) { |
| 4674 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4675 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4676 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4677 | #if ENABLE_HUSH_CASE |
| 4678 | if (rword == RES_CASE) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4679 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4680 | continue; |
| 4681 | } |
| 4682 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4683 | char **argv; |
| 4684 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4685 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 4686 | break; |
| 4687 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4688 | argv = pi->cmds->argv; |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4689 | while (*argv) { |
| 4690 | char *pattern = expand_string_to_string(*argv); |
| 4691 | /* TODO: which FNM_xxx flags to use? */ |
| 4692 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 4693 | free(pattern); |
| 4694 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 4695 | free(case_word); /* make future "word)" stop */ |
| 4696 | case_word = NULL; |
| 4697 | break; |
| 4698 | } |
| 4699 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4700 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4701 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4702 | } |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4703 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4704 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4705 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4706 | } |
| 4707 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4708 | /* Just pressing <enter> in shell should check for jobs. |
| 4709 | * OTOH, in non-interactive shell this is useless |
| 4710 | * and only leads to extra job checks */ |
| 4711 | if (pi->num_cmds == 0) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4712 | if (G_interactive_fd) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4713 | goto check_jobs_and_continue; |
| 4714 | continue; |
| 4715 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4716 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4717 | /* After analyzing all keywords and conditions, we decided |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4718 | * to execute this pipe. NB: have to do checkjobs(NULL) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4719 | * after run_pipe to collect any background children, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4720 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4721 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4722 | { |
| 4723 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4724 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4725 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4726 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4727 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 4728 | if (r != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4729 | /* We ran a builtin, function, or group. |
| 4730 | * rcode is already known |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4731 | * and we don't need to wait for anything. */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4732 | G.last_exitcode = rcode; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4733 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4734 | check_and_run_traps(0); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4735 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4736 | /* Was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4737 | if (G.flag_break_continue) { |
| 4738 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4739 | /* We might fall into outer *loop*, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4740 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4741 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4742 | G.depth_break_continue--; |
| 4743 | if (G.depth_break_continue == 0) |
| 4744 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4745 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 4746 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4747 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4748 | goto check_jobs_and_break; |
| 4749 | /* "continue": simulate end of loop */ |
| 4750 | rword = RES_DONE; |
| 4751 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4752 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4753 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4754 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 673e945 | 2009-05-27 14:39:35 +0200 | [diff] [blame] | 4755 | if (G.flag_return_in_progress == 1) { |
| 4756 | /* same as "goto check_jobs_and_break" */ |
| 4757 | checkjobs(NULL); |
| 4758 | break; |
| 4759 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4760 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4761 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4762 | /* What does bash do with attempts to background builtins? */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4763 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4764 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 4765 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4766 | check_and_run_traps(0); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4767 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4768 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4769 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4770 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4771 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4772 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4773 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4774 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4775 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4776 | /* Waits for completion, then fg's main shell */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4777 | rcode = checkjobs_and_fg_shell(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4778 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4779 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4780 | } else |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4781 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4782 | { /* This one just waits for completion */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4783 | rcode = checkjobs(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4784 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4785 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4786 | } |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4787 | G.last_exitcode = rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4788 | } |
| 4789 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4790 | |
| 4791 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4792 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4793 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4794 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4795 | #endif |
| 4796 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4797 | /* Beware of "while false; true; do ..."! */ |
| 4798 | if (pi->next && pi->next->res_word == RES_DO) { |
| 4799 | if (rword == RES_WHILE) { |
| 4800 | if (rcode) { |
| 4801 | /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4802 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4803 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
| 4804 | goto check_jobs_and_break; |
| 4805 | } |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 4806 | } |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4807 | if (rword == RES_UNTIL) { |
| 4808 | if (!rcode) { |
| 4809 | debug_printf_exec(": until expr is true: breaking\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4810 | check_jobs_and_break: |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4811 | checkjobs(NULL); |
| 4812 | break; |
| 4813 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4814 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4815 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4816 | #endif |
Mike Frysinger | 8ec1c9d | 2009-03-29 00:45:26 +0000 | [diff] [blame] | 4817 | |
| 4818 | check_jobs_and_continue: |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 4819 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4820 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4821 | |
| 4822 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4823 | G.run_list_level--; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4824 | #endif |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4825 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4826 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4827 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4828 | free(for_list); |
| 4829 | #endif |
| 4830 | #if ENABLE_HUSH_CASE |
| 4831 | free(case_word); |
| 4832 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4833 | debug_leave(); |
| 4834 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4835 | return rcode; |
| 4836 | } |
| 4837 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4838 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4839 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4840 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4841 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4842 | debug_printf_exec("run_and_free_list entered\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4843 | if (!G.fake_mode) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4844 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4845 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4846 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4847 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4848 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4849 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4850 | free_pipe_list(pi); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 4851 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4852 | return rcode; |
| 4853 | } |
| 4854 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4855 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 4856 | static struct pipe *new_pipe(void) |
| 4857 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4858 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4859 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4860 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4861 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4862 | return pi; |
| 4863 | } |
| 4864 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4865 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 4866 | * if ctx->command is NULL. |
| 4867 | * No errors possible here. |
| 4868 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4869 | static int done_command(struct parse_context *ctx) |
| 4870 | { |
| 4871 | /* The command is really already in the pipe structure, so |
| 4872 | * advance the pipe counter and make a new, null command. */ |
| 4873 | struct pipe *pi = ctx->pipe; |
| 4874 | struct command *command = ctx->command; |
| 4875 | |
| 4876 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4877 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4878 | 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] | 4879 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4880 | } |
| 4881 | pi->num_cmds++; |
| 4882 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4883 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4884 | } else { |
| 4885 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 4886 | } |
| 4887 | |
| 4888 | /* Only real trickiness here is that the uncommitted |
| 4889 | * command structure is not counted in pi->num_cmds. */ |
| 4890 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4891 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 4892 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4893 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4894 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 4895 | } |
| 4896 | |
| 4897 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 4898 | { |
| 4899 | int not_null; |
| 4900 | |
| 4901 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 4902 | /* Close previous command */ |
| 4903 | not_null = done_command(ctx); |
| 4904 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4905 | #if HAS_KEYWORDS |
| 4906 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 4907 | ctx->ctx_inverted = 0; |
| 4908 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 4909 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4910 | |
| 4911 | /* Without this check, even just <enter> on command line generates |
| 4912 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4913 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4914 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4915 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4916 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4917 | #endif |
| 4918 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4919 | || ctx->ctx_res_w == RES_DONE |
| 4920 | || ctx->ctx_res_w == RES_FOR |
| 4921 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4922 | #endif |
| 4923 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4924 | || ctx->ctx_res_w == RES_ESAC |
| 4925 | #endif |
| 4926 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4927 | struct pipe *new_p; |
| 4928 | debug_printf_parse("done_pipe: adding new pipe: " |
| 4929 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 4930 | not_null, ctx->ctx_res_w); |
| 4931 | new_p = new_pipe(); |
| 4932 | ctx->pipe->next = new_p; |
| 4933 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4934 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4935 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4936 | * This is used to control execution. |
| 4937 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 4938 | * cases where variable or value happens to match a keyword): |
| 4939 | */ |
| 4940 | #if ENABLE_HUSH_LOOPS |
| 4941 | if (ctx->ctx_res_w == RES_FOR |
| 4942 | || ctx->ctx_res_w == RES_IN) |
| 4943 | ctx->ctx_res_w = RES_NONE; |
| 4944 | #endif |
| 4945 | #if ENABLE_HUSH_CASE |
| 4946 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4947 | ctx->ctx_res_w = RES_CASE_BODY; |
| 4948 | if (ctx->ctx_res_w == RES_CASE) |
| 4949 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4950 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4951 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4952 | /* Create the memory for command, roughly: |
| 4953 | * ctx->pipe->cmds = new struct command; |
| 4954 | * ctx->command = &ctx->pipe->cmds[0]; |
| 4955 | */ |
| 4956 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4957 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4958 | } |
| 4959 | debug_printf_parse("done_pipe return\n"); |
| 4960 | } |
| 4961 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4962 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4963 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4964 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4965 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4966 | /* Create the memory for command, roughly: |
| 4967 | * ctx->pipe->cmds = new struct command; |
| 4968 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4969 | */ |
| 4970 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4971 | } |
| 4972 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4973 | /* If a reserved word is found and processed, parse context is modified |
| 4974 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4975 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4976 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4977 | struct reserved_combo { |
| 4978 | char literal[6]; |
| 4979 | unsigned char res; |
| 4980 | unsigned char assignment_flag; |
| 4981 | int flag; |
| 4982 | }; |
| 4983 | enum { |
| 4984 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4985 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4986 | FLAG_IF = (1 << RES_IF ), |
| 4987 | FLAG_THEN = (1 << RES_THEN ), |
| 4988 | FLAG_ELIF = (1 << RES_ELIF ), |
| 4989 | FLAG_ELSE = (1 << RES_ELSE ), |
| 4990 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4991 | # endif |
| 4992 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4993 | FLAG_FOR = (1 << RES_FOR ), |
| 4994 | FLAG_WHILE = (1 << RES_WHILE), |
| 4995 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 4996 | FLAG_DO = (1 << RES_DO ), |
| 4997 | FLAG_DONE = (1 << RES_DONE ), |
| 4998 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4999 | # endif |
| 5000 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5001 | FLAG_MATCH = (1 << RES_MATCH), |
| 5002 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5003 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5004 | FLAG_START = (1 << RES_XXXX ), |
| 5005 | }; |
| 5006 | |
| 5007 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 5008 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5009 | /* Mostly a list of accepted follow-up reserved words. |
| 5010 | * FLAG_END means we are done with the sequence, and are ready |
| 5011 | * to turn the compound list into a command. |
| 5012 | * FLAG_START means the word must start a new compound list. |
| 5013 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 5014 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5015 | # if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5016 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 5017 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 5018 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 5019 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 5020 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 5021 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5022 | # endif |
| 5023 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5024 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 5025 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 5026 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 5027 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 5028 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 5029 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5030 | # endif |
| 5031 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5032 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 5033 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5034 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5035 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5036 | const struct reserved_combo *r; |
| 5037 | |
| 5038 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 5039 | if (strcmp(word->data, r->literal) == 0) |
| 5040 | return r; |
| 5041 | } |
| 5042 | return NULL; |
| 5043 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5044 | /* Return 0: not a keyword, 1: keyword |
| 5045 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5046 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 5047 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5048 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5049 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5050 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5051 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5052 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 5053 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 5054 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5055 | if (word->o_quoted) |
| 5056 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5057 | r = match_reserved_word(word); |
| 5058 | if (!r) |
| 5059 | return 0; |
| 5060 | |
| 5061 | 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] | 5062 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5063 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 5064 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5065 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5066 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5067 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5068 | if (r->flag == 0) { /* '!' */ |
| 5069 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5070 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5071 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5072 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5073 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5074 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5075 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5076 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5077 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5078 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5079 | old = xmalloc(sizeof(*old)); |
| 5080 | debug_printf_parse("push stack %p\n", old); |
| 5081 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5082 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5083 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5084 | } 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] | 5085 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5086 | ctx->ctx_res_w = RES_SNTX; |
| 5087 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5088 | } else { |
| 5089 | /* "{...} fi" is ok. "{...} if" is not |
| 5090 | * Example: |
| 5091 | * if { echo foo; } then { echo bar; } fi */ |
| 5092 | if (ctx->command->group) |
| 5093 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5094 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5095 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5096 | ctx->ctx_res_w = r->res; |
| 5097 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5098 | word->o_assignment = r->assignment_flag; |
| 5099 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5100 | if (ctx->old_flag & FLAG_END) { |
| 5101 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5102 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5103 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5104 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5105 | old = ctx->stack; |
| 5106 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5107 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5108 | # if !BB_MMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5109 | o_addstr(&old->as_string, ctx->as_string.data); |
| 5110 | o_free_unsafe(&ctx->as_string); |
| 5111 | old->command->group_as_string = xstrdup(old->as_string.data); |
| 5112 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 5113 | old->command->group_as_string); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5114 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5115 | *ctx = *old; /* physical copy */ |
| 5116 | free(old); |
| 5117 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5118 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5119 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5120 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5121 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 5122 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5123 | * Normal return is 0. Syntax errors return 1. |
| 5124 | * Note: on return, word is reset, but not o_free'd! |
| 5125 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5126 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5127 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5128 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5129 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5130 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5131 | if (word->length == 0 && word->o_quoted == 0) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 5132 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 5133 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5134 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5135 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5136 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 5137 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 5138 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5139 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 5140 | * "2.7 Redirection |
| 5141 | * ...the word that follows the redirection operator |
| 5142 | * shall be subjected to tilde expansion, parameter expansion, |
| 5143 | * command substitution, arithmetic expansion, and quote |
| 5144 | * removal. Pathname expansion shall not be performed |
| 5145 | * on the word by a non-interactive shell; an interactive |
| 5146 | * shell may perform it, but shall do so only when |
| 5147 | * the expansion would result in one word." |
| 5148 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 5149 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5150 | /* Cater for >\file case: |
| 5151 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 5152 | * Same with heredocs: |
| 5153 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 5154 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 5155 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 5156 | unbackslash(ctx->pending_redirect->rd_filename); |
| 5157 | /* Is it <<"HEREDOC"? */ |
| 5158 | if (word->o_quoted) { |
| 5159 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 5160 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5161 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5162 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5163 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5164 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5165 | /* If this word wasn't an assignment, next ones definitely |
| 5166 | * can't be assignments. Even if they look like ones. */ |
| 5167 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 5168 | && word->o_assignment != WORD_IS_KEYWORD |
| 5169 | ) { |
| 5170 | word->o_assignment = NOT_ASSIGNMENT; |
| 5171 | } else { |
| 5172 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
| 5173 | command->assignment_cnt++; |
| 5174 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 5175 | } |
| 5176 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5177 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5178 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 5179 | if (ctx->ctx_dsemicolon |
| 5180 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 5181 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 5182 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5183 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 5184 | ctx->ctx_dsemicolon = 0; |
| 5185 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5186 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5187 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5188 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5189 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 5190 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5191 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5192 | # if ENABLE_HUSH_CASE |
| 5193 | && ctx->ctx_res_w != RES_CASE |
| 5194 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5195 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5196 | debug_printf_parse("checking '%s' for reserved-ness\n", word->data); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 5197 | if (reserved_word(word, ctx)) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5198 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5199 | debug_printf_parse("done_word return %d\n", |
| 5200 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5201 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5202 | } |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5203 | # ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 5204 | if (strcmp(word->data, "export") == 0 |
| 5205 | # if ENABLE_HUSH_LOCAL |
| 5206 | || strcmp(word->data, "local") == 0 |
| 5207 | # endif |
| 5208 | ) { |
| 5209 | command->cmd_type = CMD_SINGLEWORD_NOGLOB_COND; |
| 5210 | } else |
| 5211 | # endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 5212 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5213 | if (strcmp(word->data, "[[") == 0) { |
| 5214 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 5215 | } |
| 5216 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 5217 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5218 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5219 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5220 | if (command->group) { |
| 5221 | /* "{ echo foo; } echo bar" - bad */ |
| 5222 | syntax_error_at(word->data); |
| 5223 | debug_printf_parse("done_word return 1: syntax error, " |
| 5224 | "groups and arglists don't mix\n"); |
| 5225 | return 1; |
| 5226 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5227 | if (word->o_quoted /* word had "xx" or 'xx' at least as part of it. */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5228 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 5229 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5230 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 5231 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5232 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 5233 | char *p = word->data; |
| 5234 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 5235 | && (p[1] & 0x7f) == '@' |
| 5236 | && p[2] == SPECIAL_VAR_SYMBOL |
| 5237 | ) { |
| 5238 | p += 3; |
| 5239 | } |
| 5240 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5241 | /* saw no "$@", or not only "$@" but some |
| 5242 | * real text is there too */ |
| 5243 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 5244 | * e.g. "", $empty"" etc to not disappear */ |
| 5245 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 5246 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 5247 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 5248 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 5249 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5250 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5251 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5252 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 5253 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5254 | if (ctx->ctx_res_w == RES_FOR) { |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5255 | if (word->o_quoted |
| 5256 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 5257 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5258 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5259 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5260 | return 1; |
| 5261 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5262 | /* Force FOR to have just one word (variable name) */ |
| 5263 | /* NB: basically, this makes hush see "for v in ..." |
| 5264 | * syntax as if it is "for v; in ...". FOR and IN become |
| 5265 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 5266 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5267 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 5268 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5269 | #if ENABLE_HUSH_CASE |
| 5270 | /* Force CASE to have just one word */ |
| 5271 | if (ctx->ctx_res_w == RES_CASE) { |
| 5272 | done_pipe(ctx, PIPE_SEQ); |
| 5273 | } |
| 5274 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5275 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5276 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5277 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5278 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5279 | return 0; |
| 5280 | } |
| 5281 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5282 | |
| 5283 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 5284 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5285 | * Return: |
| 5286 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 5287 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 5288 | * REDIRFD_TO_FILE if no & was seen, |
| 5289 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5290 | */ |
| 5291 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5292 | #define parse_redir_right_fd(as_string, input) \ |
| 5293 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5294 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5295 | 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] | 5296 | { |
| 5297 | int ch, d, ok; |
| 5298 | |
| 5299 | ch = i_peek(input); |
| 5300 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5301 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5302 | |
| 5303 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5304 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5305 | ch = i_peek(input); |
| 5306 | if (ch == '-') { |
| 5307 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5308 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5309 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5310 | } |
| 5311 | d = 0; |
| 5312 | ok = 0; |
| 5313 | while (ch != EOF && isdigit(ch)) { |
| 5314 | d = d*10 + (ch-'0'); |
| 5315 | ok = 1; |
| 5316 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5317 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5318 | ch = i_peek(input); |
| 5319 | } |
| 5320 | if (ok) return d; |
| 5321 | |
| 5322 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 5323 | |
| 5324 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5325 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5326 | } |
| 5327 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5328 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5329 | */ |
| 5330 | static int parse_redirect(struct parse_context *ctx, |
| 5331 | int fd, |
| 5332 | redir_type style, |
| 5333 | struct in_str *input) |
| 5334 | { |
| 5335 | struct command *command = ctx->command; |
| 5336 | struct redir_struct *redir; |
| 5337 | struct redir_struct **redirp; |
| 5338 | int dup_num; |
| 5339 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5340 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5341 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5342 | /* Check for a '>&1' type redirect */ |
| 5343 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 5344 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 5345 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5346 | } else { |
| 5347 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5348 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5349 | if (dup_num) { /* <<-... */ |
| 5350 | ch = i_getch(input); |
| 5351 | nommu_addchr(&ctx->as_string, ch); |
| 5352 | ch = i_peek(input); |
| 5353 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5354 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5355 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5356 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5357 | int ch = i_peek(input); |
| 5358 | if (ch == '|') { |
| 5359 | /* >|FILE redirect ("clobbering" >). |
| 5360 | * Since we do not support "set -o noclobber" yet, |
| 5361 | * >| and > are the same for now. Just eat |. |
| 5362 | */ |
| 5363 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5364 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5365 | } |
| 5366 | } |
| 5367 | |
| 5368 | /* Create a new redir_struct and append it to the linked list */ |
| 5369 | redirp = &command->redirects; |
| 5370 | while ((redir = *redirp) != NULL) { |
| 5371 | redirp = &(redir->next); |
| 5372 | } |
| 5373 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 5374 | /* redir->next = NULL; */ |
| 5375 | /* redir->rd_filename = NULL; */ |
| 5376 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5377 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5378 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5379 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 5380 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5381 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5382 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5383 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5384 | /* Erik had a check here that the file descriptor in question |
| 5385 | * is legit; I postpone that to "run time" |
| 5386 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5387 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 5388 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5389 | } else { |
| 5390 | /* Set ctx->pending_redirect, so we know what to do at the |
| 5391 | * end of the next parsed word. */ |
| 5392 | ctx->pending_redirect = redir; |
| 5393 | } |
| 5394 | return 0; |
| 5395 | } |
| 5396 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5397 | /* If a redirect is immediately preceded by a number, that number is |
| 5398 | * supposed to tell which file descriptor to redirect. This routine |
| 5399 | * looks for such preceding numbers. In an ideal world this routine |
| 5400 | * needs to handle all the following classes of redirects... |
| 5401 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 5402 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 5403 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 5404 | * 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] | 5405 | * |
| 5406 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 5407 | * "2.7 Redirection |
| 5408 | * ... If n is quoted, the number shall not be recognized as part of |
| 5409 | * the redirection expression. For example: |
| 5410 | * echo \2>a |
| 5411 | * writes the character 2 into file a" |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5412 | * We are getting it right by setting ->o_quoted on any \<char> |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5413 | * |
| 5414 | * A -1 return means no valid number was found, |
| 5415 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5416 | */ |
| 5417 | static int redirect_opt_num(o_string *o) |
| 5418 | { |
| 5419 | int num; |
| 5420 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5421 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5422 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5423 | num = bb_strtou(o->data, NULL, 10); |
| 5424 | if (errno || num < 0) |
| 5425 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5426 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5427 | return num; |
| 5428 | } |
| 5429 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5430 | #if BB_MMU |
| 5431 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 5432 | fetch_till_str(input, word, skip_tabs) |
| 5433 | #endif |
| 5434 | static char *fetch_till_str(o_string *as_string, |
| 5435 | struct in_str *input, |
| 5436 | const char *word, |
| 5437 | int skip_tabs) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5438 | { |
| 5439 | o_string heredoc = NULL_O_STRING; |
| 5440 | int past_EOL = 0; |
| 5441 | int ch; |
| 5442 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5443 | goto jump_in; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5444 | while (1) { |
| 5445 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5446 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5447 | if (ch == '\n') { |
| 5448 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 5449 | heredoc.data[past_EOL] = '\0'; |
| 5450 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 5451 | return heredoc.data; |
| 5452 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5453 | do { |
| 5454 | o_addchr(&heredoc, ch); |
| 5455 | past_EOL = heredoc.length; |
| 5456 | jump_in: |
| 5457 | do { |
| 5458 | ch = i_getch(input); |
| 5459 | nommu_addchr(as_string, ch); |
| 5460 | } while (skip_tabs && ch == '\t'); |
| 5461 | } while (ch == '\n'); |
| 5462 | } |
| 5463 | if (ch == EOF) { |
| 5464 | o_free_unsafe(&heredoc); |
| 5465 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5466 | } |
| 5467 | o_addchr(&heredoc, ch); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5468 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5469 | } |
| 5470 | } |
| 5471 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5472 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 5473 | * and load them all. There should be exactly heredoc_cnt of them. |
| 5474 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5475 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 5476 | { |
| 5477 | struct pipe *pi = ctx->list_head; |
| 5478 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5479 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5480 | int i; |
| 5481 | struct command *cmd = pi->cmds; |
| 5482 | |
| 5483 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 5484 | pi->num_cmds, |
| 5485 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 5486 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5487 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5488 | |
| 5489 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 5490 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5491 | while (redir) { |
| 5492 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5493 | char *p; |
| 5494 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5495 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5496 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5497 | p = fetch_till_str(&ctx->as_string, input, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5498 | redir->rd_filename, redir->rd_dup & HEREDOC_SKIPTABS); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5499 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5500 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5501 | return 1; |
| 5502 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5503 | free(redir->rd_filename); |
| 5504 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5505 | heredoc_cnt--; |
| 5506 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5507 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5508 | } |
| 5509 | cmd++; |
| 5510 | } |
| 5511 | pi = pi->next; |
| 5512 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5513 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5514 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5515 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5516 | bb_error_msg_and_die("heredoc BUG 2"); |
| 5517 | #endif |
| 5518 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5519 | } |
| 5520 | |
| 5521 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5522 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5523 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5524 | { |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5525 | pid_t pid; |
| 5526 | int channel[2]; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5527 | # if !BB_MMU |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 5528 | char **to_free = NULL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5529 | # endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 5530 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 5531 | xpipe(channel); |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 5532 | pid = BB_MMU ? fork() : vfork(); |
| 5533 | if (pid < 0) |
| 5534 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5535 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5536 | if (pid == 0) { /* child */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5537 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 5538 | /* Process substitution is not considered to be usual |
| 5539 | * 'command execution'. |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 5540 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 5541 | */ |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 5542 | bb_signals(0 |
| 5543 | + (1 << SIGTSTP) |
| 5544 | + (1 << SIGTTIN) |
| 5545 | + (1 << SIGTTOU) |
| 5546 | , SIG_IGN); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 5547 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5548 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 5549 | xmove_fd(channel[1], 1); |
| 5550 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 5551 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denys Vlasenko | 91836ba | 2009-09-23 01:46:19 +0200 | [diff] [blame] | 5552 | /* Awful hack for `trap` or $(trap). |
| 5553 | * |
| 5554 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 5555 | * contains an example where "trap" is executed in a subshell: |
| 5556 | * |
| 5557 | * save_traps=$(trap) |
| 5558 | * ... |
| 5559 | * eval "$save_traps" |
| 5560 | * |
| 5561 | * Standard does not say that "trap" in subshell shall print |
| 5562 | * parent shell's traps. It only says that its output |
| 5563 | * must have suitable form, but then, in the above example |
| 5564 | * (which is not supposed to be normative), it implies that. |
| 5565 | * |
| 5566 | * bash (and probably other shell) does implement it |
| 5567 | * (traps are reset to defaults, but "trap" still shows them), |
| 5568 | * but as a result, "trap" logic is hopelessly messed up: |
| 5569 | * |
| 5570 | * # trap |
| 5571 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 5572 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 5573 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 5574 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 5575 | * trap -- 'echo Ho' SIGWINCH |
| 5576 | * # echo `(trap)` <--- in subshell in subshell - output |
| 5577 | * trap -- 'echo Ho' SIGWINCH |
| 5578 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 5579 | * trap -- 'echo Ho' SIGWINCH |
| 5580 | * |
| 5581 | * The rules when to forget and when to not forget traps |
| 5582 | * get really complex and nonsensical. |
| 5583 | * |
| 5584 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 5585 | */ |
| 5586 | s = skip_whitespace(s); |
| 5587 | if (strncmp(s, "trap", 4) == 0 && (*skip_whitespace(s + 4) == '\0')) |
| 5588 | { |
| 5589 | static const char *const argv[] = { NULL, NULL }; |
| 5590 | builtin_trap((char**)argv); |
| 5591 | exit(0); /* not _exit() - we need to fflush */ |
| 5592 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5593 | # if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5594 | reset_traps_to_defaults(); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5595 | parse_and_run_string(s); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5596 | _exit(G.last_exitcode); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5597 | # else |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5598 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5599 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 5600 | * huge=`cat BIG` # was blocking here forever |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5601 | * echo OK |
| 5602 | */ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5603 | re_execute_shell(&to_free, |
| 5604 | s, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5605 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 5606 | G.global_argv + 1, |
| 5607 | NULL); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5608 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5609 | } |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5610 | |
| 5611 | /* parent */ |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5612 | *pid_p = pid; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5613 | # if ENABLE_HUSH_FAST |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 5614 | G.count_SIGCHLD++; |
| 5615 | //bb_error_msg("[%d] fork in generate_stream_from_string: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5616 | # endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5617 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5618 | # if !BB_MMU |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5619 | free(to_free); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5620 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5621 | close(channel[1]); |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5622 | close_on_exec_on(channel[0]); |
| 5623 | return xfdopen_for_read(channel[0]); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5624 | } |
| 5625 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 5626 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5627 | static int process_command_subs(o_string *dest, const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5628 | { |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5629 | FILE *fp; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5630 | struct in_str pipe_str; |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5631 | pid_t pid; |
| 5632 | int status, ch, eol_cnt; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5633 | |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5634 | fp = generate_stream_from_string(s, &pid); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5635 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5636 | /* Now send results of command back into original context */ |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5637 | setup_file_in_str(&pipe_str, fp); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5638 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5639 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5640 | if (ch == '\n') { |
| 5641 | eol_cnt++; |
| 5642 | continue; |
| 5643 | } |
| 5644 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5645 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5646 | eol_cnt--; |
| 5647 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 5648 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5649 | } |
| 5650 | |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5651 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5652 | fclose(fp); |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5653 | /* We need to extract exitcode. Test case |
| 5654 | * "true; echo `sleep 1; false` $?" |
| 5655 | * should print 1 */ |
| 5656 | safe_waitpid(pid, &status, 0); |
| 5657 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 5658 | return WEXITSTATUS(status); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5659 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5660 | #endif /* ENABLE_HUSH_TICK */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5661 | |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 5662 | #if !ENABLE_HUSH_FUNCTIONS |
| 5663 | #define parse_group(dest, ctx, input, ch) \ |
| 5664 | parse_group(ctx, input, ch) |
| 5665 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5666 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5667 | struct in_str *input, int ch) |
| 5668 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5669 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5670 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5671 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5672 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5673 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5674 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5675 | |
| 5676 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5677 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5678 | if (ch == '(' && !dest->o_quoted) { |
| 5679 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5680 | if (done_word(dest, ctx)) |
| 5681 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5682 | if (!command->argv) |
| 5683 | goto skip; /* (... */ |
| 5684 | if (command->argv[1]) { /* word word ... (... */ |
| 5685 | syntax_error_unexpected_ch('('); |
| 5686 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5687 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5688 | /* it is "word(..." or "word (..." */ |
| 5689 | do |
| 5690 | ch = i_getch(input); |
| 5691 | while (ch == ' ' || ch == '\t'); |
| 5692 | if (ch != ')') { |
| 5693 | syntax_error_unexpected_ch(ch); |
| 5694 | return 1; |
| 5695 | } |
| 5696 | nommu_addchr(&ctx->as_string, ch); |
| 5697 | do |
| 5698 | ch = i_getch(input); |
| 5699 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 5700 | if (ch != '{') { |
| 5701 | syntax_error_unexpected_ch(ch); |
| 5702 | return 1; |
| 5703 | } |
| 5704 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5705 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5706 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5707 | } |
| 5708 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5709 | |
| 5710 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5711 | if (command->argv /* word [word]{... */ |
| 5712 | || dest->length /* word{... */ |
| 5713 | || dest->o_quoted /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5714 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5715 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5716 | debug_printf_parse("parse_group return 1: " |
| 5717 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5718 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5719 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5720 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5721 | |
| 5722 | #if ENABLE_HUSH_FUNCTIONS |
| 5723 | skip: |
| 5724 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5725 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 5726 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5727 | endch = ')'; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5728 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5729 | } else { |
| 5730 | /* bash does not allow "{echo...", requires whitespace */ |
| 5731 | ch = i_getch(input); |
| 5732 | if (ch != ' ' && ch != '\t' && ch != '\n') { |
| 5733 | syntax_error_unexpected_ch(ch); |
| 5734 | return 1; |
| 5735 | } |
| 5736 | nommu_addchr(&ctx->as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5737 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5738 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5739 | { |
| 5740 | #if !BB_MMU |
| 5741 | char *as_string = NULL; |
| 5742 | #endif |
| 5743 | pipe_list = parse_stream(&as_string, input, endch); |
| 5744 | #if !BB_MMU |
| 5745 | if (as_string) |
| 5746 | o_addstr(&ctx->as_string, as_string); |
| 5747 | #endif |
| 5748 | /* empty ()/{} or parse error? */ |
| 5749 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5750 | /* parse_stream already emitted error msg */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5751 | #if !BB_MMU |
| 5752 | free(as_string); |
| 5753 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5754 | debug_printf_parse("parse_group return 1: " |
| 5755 | "parse_stream returned %p\n", pipe_list); |
| 5756 | return 1; |
| 5757 | } |
| 5758 | command->group = pipe_list; |
| 5759 | #if !BB_MMU |
| 5760 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 5761 | command->group_as_string = as_string; |
| 5762 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 5763 | command->group_as_string); |
| 5764 | #endif |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5765 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5766 | debug_printf_parse("parse_group return 0\n"); |
| 5767 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5768 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5769 | } |
| 5770 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5771 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5772 | /* Subroutines for copying $(...) and `...` things */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5773 | static void add_till_backquote(o_string *dest, struct in_str *input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5774 | /* '...' */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5775 | 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] | 5776 | { |
| 5777 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5778 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5779 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5780 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5781 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5782 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5783 | if (ch == '\'') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5784 | return; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5785 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5786 | } |
| 5787 | } |
| 5788 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5789 | 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] | 5790 | { |
| 5791 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5792 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5793 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5794 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5795 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5796 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5797 | if (ch == '"') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5798 | return; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5799 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5800 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5801 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5802 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5803 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5804 | if (ch == '`') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5805 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5806 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5807 | continue; |
| 5808 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 5809 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5810 | } |
| 5811 | } |
| 5812 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 5813 | * \` quoting. |
| 5814 | * "Within the backquoted style of command substitution, backslash |
| 5815 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 5816 | * The search for the matching backquote shall be satisfied by the first |
| 5817 | * backquote found without a preceding backslash; during this search, |
| 5818 | * if a non-escaped backquote is encountered within a shell comment, |
| 5819 | * a here-document, an embedded command substitution of the $(command) |
| 5820 | * form, or a quoted string, undefined results occur. A single-quoted |
| 5821 | * or double-quoted string that begins, but does not end, within the |
| 5822 | * "`...`" sequence produces undefined results." |
| 5823 | * Example Output |
| 5824 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 5825 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5826 | static void add_till_backquote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5827 | { |
| 5828 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5829 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5830 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5831 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5832 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5833 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5834 | if (ch == '`') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5835 | return; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5836 | if (ch == '\\') { |
| 5837 | /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5838 | int ch2 = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5839 | if (ch2 == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5840 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5841 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5842 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5843 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5844 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5845 | ch = ch2; |
| 5846 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5847 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5848 | } |
| 5849 | } |
| 5850 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 5851 | * quoting and nested ()s. |
| 5852 | * "With the $(command) style of command substitution, all characters |
| 5853 | * following the open parenthesis to the matching closing parenthesis |
| 5854 | * constitute the command. Any valid shell script can be used for command, |
| 5855 | * except a script consisting solely of redirections which produces |
| 5856 | * unspecified results." |
| 5857 | * Example Output |
| 5858 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 5859 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 5860 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 5861 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5862 | static void add_till_closing_paren(o_string *dest, struct in_str *input, bool dbl) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5863 | { |
| 5864 | int count = 0; |
| 5865 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5866 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5867 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5868 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5869 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5870 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5871 | if (ch == '(') |
| 5872 | count++; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5873 | if (ch == ')') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5874 | if (--count < 0) { |
| 5875 | if (!dbl) |
| 5876 | break; |
| 5877 | if (i_peek(input) == ')') { |
| 5878 | i_getch(input); |
| 5879 | break; |
| 5880 | } |
| 5881 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5882 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5883 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5884 | if (ch == '\'') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5885 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5886 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5887 | continue; |
| 5888 | } |
| 5889 | if (ch == '"') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5890 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5891 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5892 | continue; |
| 5893 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5894 | if (ch == '\\') { |
| 5895 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5896 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5897 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5898 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5899 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5900 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5901 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5902 | continue; |
| 5903 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5904 | } |
| 5905 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5906 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5907 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5908 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5909 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5910 | #define handle_dollar(as_string, dest, input) \ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5911 | handle_dollar(dest, input) |
| 5912 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5913 | static int handle_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5914 | o_string *dest, |
| 5915 | struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5916 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5917 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5918 | unsigned char quote_mask = dest->o_escape ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5919 | |
| 5920 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5921 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5922 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5923 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 5924 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5925 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5926 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5927 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5928 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5929 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5930 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5931 | if (!isalnum(ch) && ch != '_') |
| 5932 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5933 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5934 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5935 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5936 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5937 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5938 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5939 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5940 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5941 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5942 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5943 | o_addchr(dest, ch | quote_mask); |
| 5944 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5945 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5946 | case '$': /* pid */ |
| 5947 | case '!': /* last bg pid */ |
| 5948 | case '?': /* last exit code */ |
| 5949 | case '#': /* number of args */ |
| 5950 | case '*': /* args */ |
| 5951 | case '@': /* args */ |
| 5952 | goto make_one_char_var; |
| 5953 | case '{': { |
| 5954 | bool first_char, all_digits; |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 5955 | bool in_expansion_param; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 5956 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5957 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5958 | nommu_addchr(as_string, ch); |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 5959 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5960 | |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 5961 | // TODO: need to handle "a=ab}; echo ${a%\}}" |
| 5962 | // and "a=abc; c=c; echo ${a%${c}}" |
| 5963 | in_expansion_param = false; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5964 | first_char = true; |
| 5965 | all_digits = false; |
| 5966 | while (1) { |
| 5967 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5968 | nommu_addchr(as_string, ch); |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5969 | if (ch == '}') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5970 | break; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5971 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5972 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5973 | if (first_char) { |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5974 | if (ch == '#') { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5975 | /* ${#var}: length of var contents */ |
| 5976 | goto char_ok; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5977 | } |
| 5978 | if (isdigit(ch)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5979 | all_digits = true; |
| 5980 | goto char_ok; |
| 5981 | } |
Mike Frysinger | dc3bc40 | 2009-06-01 14:09:09 -0400 | [diff] [blame] | 5982 | /* They're being verbose and doing ${?} */ |
| 5983 | if (i_peek(input) == '}' && strchr("$!?#*@_", ch)) |
| 5984 | goto char_ok; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5985 | } |
| 5986 | |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 5987 | if (!in_expansion_param |
| 5988 | && ( (all_digits && !isdigit(ch)) /* met non-digit: 123w */ |
| 5989 | || (!all_digits && !isalnum(ch) && ch != '_') /* met non-name char: abc% */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5990 | ) |
| 5991 | ) { |
| 5992 | /* handle parameter expansions |
| 5993 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 5994 | */ |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 5995 | if (first_char /* bad (empty var name): "${%..." */ |
| 5996 | || !strchr("%#:-=+?", ch) /* bad: "${var<bad_char>..." */ |
| 5997 | ) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5998 | syntax_error_unterm_str("${name}"); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5999 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 6000 | return 1; |
| 6001 | } |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 6002 | in_expansion_param = true; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6003 | } |
| 6004 | char_ok: |
| 6005 | debug_printf_parse(": '%c'\n", ch); |
| 6006 | o_addchr(dest, ch | quote_mask); |
| 6007 | quote_mask = 0; |
| 6008 | first_char = false; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 6009 | } /* while (1) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6010 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6011 | break; |
| 6012 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 6013 | #if ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6014 | case '(': { |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6015 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6016 | int pos; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6017 | # endif |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6018 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6019 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6020 | # if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6021 | if (i_peek(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6022 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6023 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6024 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6025 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6026 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6027 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6028 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6029 | add_till_closing_paren(dest, input, true); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6030 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6031 | if (as_string) { |
| 6032 | o_addstr(as_string, dest->data + pos); |
| 6033 | o_addchr(as_string, ')'); |
| 6034 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6035 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6036 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6037 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6038 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 6039 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6040 | # endif |
| 6041 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6042 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6043 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6044 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6045 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6046 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6047 | add_till_closing_paren(dest, input, false); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6048 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6049 | if (as_string) { |
| 6050 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 6051 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6052 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6053 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6054 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6055 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6056 | break; |
| 6057 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6058 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6059 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6060 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6061 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6062 | ch = i_peek(input); |
| 6063 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 6064 | ch = '_'; |
| 6065 | goto make_var; |
| 6066 | } |
| 6067 | /* else: it's $_ */ |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 6068 | /* TODO: $_ and $-: */ |
| 6069 | /* $_ Shell or shell script name; or last argument of last command |
| 6070 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 6071 | * 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] | 6072 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 6073 | default: |
| 6074 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6075 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 6076 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6077 | return 0; |
| 6078 | } |
| 6079 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6080 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6081 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6082 | parse_stream_dquoted(dest, input, dquote_end) |
| 6083 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6084 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6085 | o_string *dest, |
| 6086 | struct in_str *input, |
| 6087 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6088 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6089 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6090 | int next; |
| 6091 | |
| 6092 | again: |
| 6093 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6094 | if (ch != EOF) |
| 6095 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6096 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6097 | if (dest->o_assignment == NOT_ASSIGNMENT) |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 6098 | dest->o_escape ^= 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6099 | debug_printf_parse("parse_stream_dquoted return 0\n"); |
| 6100 | return 0; |
| 6101 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 6102 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6103 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6104 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6105 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6106 | } |
| 6107 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6108 | if (ch != '\n') { |
| 6109 | next = i_peek(input); |
| 6110 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 6111 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6112 | ch, ch, dest->o_escape); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6113 | if (ch == '\\') { |
| 6114 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6115 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6116 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6117 | } |
| 6118 | /* bash: |
| 6119 | * "The backslash retains its special meaning [in "..."] |
| 6120 | * only when followed by one of the following characters: |
| 6121 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 6122 | * within double quotes by preceding it with a backslash." |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6123 | */ |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6124 | if (strchr("$`\"\\\n", next) != NULL) { |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 6125 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6126 | if (ch != '\n') { |
| 6127 | o_addqchr(dest, ch); |
| 6128 | nommu_addchr(as_string, ch); |
| 6129 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6130 | } else { |
| 6131 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 6132 | nommu_addchr(as_string, '\\'); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6133 | } |
| 6134 | goto again; |
| 6135 | } |
| 6136 | if (ch == '$') { |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6137 | if (handle_dollar(as_string, dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6138 | debug_printf_parse("parse_stream_dquoted return 1: " |
| 6139 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6140 | return 1; |
| 6141 | } |
| 6142 | goto again; |
| 6143 | } |
| 6144 | #if ENABLE_HUSH_TICK |
| 6145 | if (ch == '`') { |
| 6146 | //int pos = dest->length; |
| 6147 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6148 | o_addchr(dest, 0x80 | '`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6149 | add_till_backquote(dest, input); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6150 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6151 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 6152 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6153 | } |
| 6154 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 6155 | o_addQchr(dest, ch); |
| 6156 | if (ch == '=' |
| 6157 | && (dest->o_assignment == MAYBE_ASSIGNMENT |
| 6158 | || dest->o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6159 | && is_well_formed_var_name(dest->data, '=') |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 6160 | ) { |
| 6161 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 6162 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6163 | goto again; |
| 6164 | } |
| 6165 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6166 | /* |
| 6167 | * Scan input until EOF or end_trigger char. |
| 6168 | * Return a list of pipes to execute, or NULL on EOF |
| 6169 | * or if end_trigger character is met. |
| 6170 | * On syntax error, exit is shell is not interactive, |
| 6171 | * reset parsing machinery and start parsing anew, |
| 6172 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6173 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6174 | static struct pipe *parse_stream(char **pstring, |
| 6175 | struct in_str *input, |
| 6176 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6177 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6178 | struct parse_context ctx; |
| 6179 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6180 | int is_in_dquote; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6181 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6182 | |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 6183 | /* Double-quote state is handled in the state variable is_in_dquote. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6184 | * A single-quote triggers a bypass of the main loop until its mate is |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6185 | * found. When recursing, quote state is passed in via dest->o_escape. |
| 6186 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6187 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 6188 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6189 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6190 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 6191 | /* If very first arg is "" or '', dest.data may end up NULL. |
| 6192 | * Preventing this: */ |
| 6193 | o_addchr(&dest, '\0'); |
| 6194 | dest.length = 0; |
| 6195 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6196 | G.ifs = get_local_var_value("IFS"); |
| 6197 | if (G.ifs == NULL) |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 6198 | G.ifs = defifs; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6199 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6200 | reset: |
| 6201 | #if ENABLE_HUSH_INTERACTIVE |
| 6202 | input->promptmode = 0; /* PS1 */ |
| 6203 | #endif |
| 6204 | /* dest.o_assignment = MAYBE_ASSIGNMENT; - already is */ |
| 6205 | initialize_context(&ctx); |
| 6206 | is_in_dquote = 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6207 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 6208 | while (1) { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6209 | const char *is_ifs; |
| 6210 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6211 | int ch; |
| 6212 | int next; |
| 6213 | int redir_fd; |
| 6214 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6215 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6216 | if (is_in_dquote) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 6217 | /* dest.o_quoted = 1; - already is (see below) */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6218 | if (parse_stream_dquoted(&ctx.as_string, &dest, input, '"')) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6219 | goto parse_error; |
| 6220 | } |
| 6221 | /* We reached closing '"' */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6222 | is_in_dquote = 0; |
| 6223 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6224 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6225 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 6226 | ch, ch, dest.o_escape); |
| 6227 | if (ch == EOF) { |
| 6228 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6229 | |
| 6230 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6231 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 6232 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6233 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 6234 | /* end_trigger == '}' case errors out earlier, |
| 6235 | * checking only ')' */ |
| 6236 | if (end_trigger == ')') { |
| 6237 | syntax_error_unterm_ch('('); /* exits */ |
| 6238 | /* goto parse_error; */ |
| 6239 | } |
| 6240 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6241 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 6242 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6243 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6244 | o_free(&dest); |
| 6245 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6246 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6247 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6248 | /* (this makes bare "&" cmd a no-op. |
| 6249 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6250 | if (pi->num_cmds == 0 |
| 6251 | IF_HAS_KEYWORDS( && pi->res_word == RES_NONE) |
| 6252 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6253 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6254 | pi = NULL; |
| 6255 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6256 | #if !BB_MMU |
| 6257 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 6258 | if (pstring) |
| 6259 | *pstring = ctx.as_string.data; |
| 6260 | else |
| 6261 | o_free_unsafe(&ctx.as_string); |
| 6262 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6263 | debug_leave(); |
| 6264 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6265 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 6266 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6267 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 6268 | |
| 6269 | next = '\0'; |
| 6270 | if (ch != '\n') |
| 6271 | next = i_peek(input); |
| 6272 | |
| 6273 | is_special = "{}<>;&|()#'" /* special outside of "str" */ |
| 6274 | "\\$\"" IF_HUSH_TICK("`"); /* always special */ |
| 6275 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 6276 | if (ctx.command->argv /* word [word]{... - non-special */ |
| 6277 | || dest.length /* word{... - non-special */ |
| 6278 | || dest.o_quoted /* ""{... - non-special */ |
| 6279 | || (next != ';' /* }; - special */ |
| 6280 | && next != ')' /* }) - special */ |
| 6281 | && next != '&' /* }& and }&& ... - special */ |
| 6282 | && next != '|' /* }|| ... - special */ |
| 6283 | && !strchr(G.ifs, next) /* {word - non-special */ |
| 6284 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 6285 | ) { |
| 6286 | /* They are not special, skip "{}" */ |
| 6287 | is_special += 2; |
| 6288 | } |
| 6289 | is_special = strchr(is_special, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6290 | is_ifs = strchr(G.ifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6291 | |
| 6292 | if (!is_special && !is_ifs) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 6293 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6294 | o_addQchr(&dest, ch); |
| 6295 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 6296 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6297 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6298 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6299 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6300 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6301 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6302 | continue; |
| 6303 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6304 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6305 | if (is_ifs) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6306 | if (done_word(&dest, &ctx)) { |
| 6307 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 6308 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 6309 | if (ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6310 | #if ENABLE_HUSH_CASE |
| 6311 | /* "case ... in <newline> word) ..." - |
| 6312 | * newlines are ignored (but ';' wouldn't be) */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6313 | if (ctx.command->argv == NULL |
| 6314 | && ctx.ctx_res_w == RES_MATCH |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6315 | ) { |
| 6316 | continue; |
| 6317 | } |
| 6318 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6319 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6320 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6321 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 6322 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 6323 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6324 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 6325 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6326 | heredoc_cnt = 0; |
| 6327 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6328 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6329 | ch = ';'; |
Denis Vlasenko | 7c98612 | 2009-04-04 12:15:42 +0000 | [diff] [blame] | 6330 | /* note: if (is_ifs) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6331 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6332 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6333 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 6334 | |
| 6335 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 6336 | * } is an ordinary char in this case, even inside { cmd; } |
| 6337 | * Pathological example: { ""}; } should exec "}" cmd |
| 6338 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 6339 | if (ch == '}') { |
| 6340 | if (!IS_NULL_CMD(ctx.command) /* cmd } */ |
| 6341 | || dest.length != 0 /* word} */ |
| 6342 | || dest.o_quoted /* ""} */ |
| 6343 | ) { |
| 6344 | goto ordinary_char; |
| 6345 | } |
| 6346 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
| 6347 | goto skip_end_trigger; |
| 6348 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 6349 | } |
| 6350 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6351 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 6352 | && (ch != ';' || heredoc_cnt == 0) |
| 6353 | #if ENABLE_HUSH_CASE |
| 6354 | && (ch != ')' |
| 6355 | || ctx.ctx_res_w != RES_MATCH |
| 6356 | || (!dest.o_quoted && strcmp(dest.data, "esac") == 0) |
| 6357 | ) |
| 6358 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6359 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6360 | if (heredoc_cnt) { |
| 6361 | /* This is technically valid: |
| 6362 | * { cat <<HERE; }; echo Ok |
| 6363 | * heredoc |
| 6364 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6365 | * HERE |
| 6366 | * but we don't support this. |
| 6367 | * We require heredoc to be in enclosing {}/(), |
| 6368 | * if any. |
| 6369 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6370 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6371 | goto parse_error; |
| 6372 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6373 | if (done_word(&dest, &ctx)) { |
| 6374 | goto parse_error; |
| 6375 | } |
| 6376 | done_pipe(&ctx, PIPE_SEQ); |
| 6377 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6378 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 6379 | if (!HAS_KEYWORDS |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6380 | 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] | 6381 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6382 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6383 | #if !BB_MMU |
| 6384 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 6385 | if (pstring) |
| 6386 | *pstring = ctx.as_string.data; |
| 6387 | else |
| 6388 | o_free_unsafe(&ctx.as_string); |
| 6389 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6390 | debug_leave(); |
| 6391 | debug_printf_parse("parse_stream return %p: " |
| 6392 | "end_trigger char found\n", |
| 6393 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6394 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6395 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6396 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 6397 | skip_end_trigger: |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6398 | if (is_ifs) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6399 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6400 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 6401 | /* Catch <, > before deciding whether this word is |
| 6402 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 6403 | switch (ch) { |
| 6404 | case '>': |
| 6405 | redir_fd = redirect_opt_num(&dest); |
| 6406 | if (done_word(&dest, &ctx)) { |
| 6407 | goto parse_error; |
| 6408 | } |
| 6409 | redir_style = REDIRECT_OVERWRITE; |
| 6410 | if (next == '>') { |
| 6411 | redir_style = REDIRECT_APPEND; |
| 6412 | ch = i_getch(input); |
| 6413 | nommu_addchr(&ctx.as_string, ch); |
| 6414 | } |
| 6415 | #if 0 |
| 6416 | else if (next == '(') { |
| 6417 | syntax_error(">(process) not supported"); |
| 6418 | goto parse_error; |
| 6419 | } |
| 6420 | #endif |
| 6421 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 6422 | goto parse_error; |
| 6423 | continue; /* back to top of while (1) */ |
| 6424 | case '<': |
| 6425 | redir_fd = redirect_opt_num(&dest); |
| 6426 | if (done_word(&dest, &ctx)) { |
| 6427 | goto parse_error; |
| 6428 | } |
| 6429 | redir_style = REDIRECT_INPUT; |
| 6430 | if (next == '<') { |
| 6431 | redir_style = REDIRECT_HEREDOC; |
| 6432 | heredoc_cnt++; |
| 6433 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 6434 | ch = i_getch(input); |
| 6435 | nommu_addchr(&ctx.as_string, ch); |
| 6436 | } else if (next == '>') { |
| 6437 | redir_style = REDIRECT_IO; |
| 6438 | ch = i_getch(input); |
| 6439 | nommu_addchr(&ctx.as_string, ch); |
| 6440 | } |
| 6441 | #if 0 |
| 6442 | else if (next == '(') { |
| 6443 | syntax_error("<(process) not supported"); |
| 6444 | goto parse_error; |
| 6445 | } |
| 6446 | #endif |
| 6447 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 6448 | goto parse_error; |
| 6449 | continue; /* back to top of while (1) */ |
| 6450 | } |
| 6451 | |
| 6452 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 6453 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 6454 | && !ctx.pending_redirect |
| 6455 | ) { |
| 6456 | /* ch is a special char and thus this word |
| 6457 | * cannot be an assignment */ |
| 6458 | dest.o_assignment = NOT_ASSIGNMENT; |
| 6459 | } |
| 6460 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6461 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 6462 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6463 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6464 | case '#': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6465 | if (dest.length == 0) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6466 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6467 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6468 | if (ch == EOF || ch == '\n') |
| 6469 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6470 | i_getch(input); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6471 | /* note: we do not add it to &ctx.as_string */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6472 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6473 | nommu_addchr(&ctx.as_string, '\n'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6474 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6475 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6476 | } |
| 6477 | break; |
| 6478 | case '\\': |
| 6479 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6480 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6481 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6482 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6483 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6484 | if (ch != '\n') { |
| 6485 | o_addchr(&dest, '\\'); |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6486 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6487 | o_addchr(&dest, ch); |
| 6488 | nommu_addchr(&ctx.as_string, ch); |
| 6489 | /* Example: echo Hello \2>file |
| 6490 | * we need to know that word 2 is quoted */ |
| 6491 | dest.o_quoted = 1; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 6492 | } |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6493 | #if !BB_MMU |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 6494 | else { |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6495 | /* It's "\<newline>". Remove trailing '\' from ctx.as_string */ |
| 6496 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6497 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 6498 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6499 | break; |
| 6500 | case '$': |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6501 | if (handle_dollar(&ctx.as_string, &dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6502 | debug_printf_parse("parse_stream parse error: " |
| 6503 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6504 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6505 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6506 | break; |
| 6507 | case '\'': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6508 | dest.o_quoted = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6509 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6510 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6511 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6512 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6513 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6514 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6515 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6516 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6517 | break; |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 6518 | o_addqchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6519 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6520 | break; |
| 6521 | case '"': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6522 | dest.o_quoted = 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6523 | is_in_dquote ^= 1; /* invert */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6524 | if (dest.o_assignment == NOT_ASSIGNMENT) |
| 6525 | dest.o_escape ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6526 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 6527 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6528 | case '`': { |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6529 | #if !BB_MMU |
| 6530 | int pos; |
| 6531 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6532 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6533 | o_addchr(&dest, '`'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6534 | #if !BB_MMU |
| 6535 | pos = dest.length; |
| 6536 | #endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6537 | add_till_backquote(&dest, input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6538 | #if !BB_MMU |
| 6539 | o_addstr(&ctx.as_string, dest.data + pos); |
| 6540 | o_addchr(&ctx.as_string, '`'); |
| 6541 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6542 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6543 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6544 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6545 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 6546 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6547 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6548 | #if ENABLE_HUSH_CASE |
| 6549 | case_semi: |
| 6550 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6551 | if (done_word(&dest, &ctx)) { |
| 6552 | goto parse_error; |
| 6553 | } |
| 6554 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6555 | #if ENABLE_HUSH_CASE |
| 6556 | /* Eat multiple semicolons, detect |
| 6557 | * whether it means something special */ |
| 6558 | while (1) { |
| 6559 | ch = i_peek(input); |
| 6560 | if (ch != ';') |
| 6561 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6562 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6563 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 6564 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6565 | ctx.ctx_dsemicolon = 1; |
| 6566 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6567 | break; |
| 6568 | } |
| 6569 | } |
| 6570 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6571 | new_cmd: |
| 6572 | /* We just finished a cmd. New one may start |
| 6573 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6574 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6575 | break; |
| 6576 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6577 | if (done_word(&dest, &ctx)) { |
| 6578 | goto parse_error; |
| 6579 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6580 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6581 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6582 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6583 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6584 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6585 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6586 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6587 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6588 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6589 | if (done_word(&dest, &ctx)) { |
| 6590 | goto parse_error; |
| 6591 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 6592 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6593 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6594 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 6595 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6596 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6597 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6598 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6599 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6600 | } else { |
| 6601 | /* we could pick up a file descriptor choice here |
| 6602 | * with redirect_opt_num(), but bash doesn't do it. |
| 6603 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6604 | done_command(&ctx); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 6605 | #if !BB_MMU |
| 6606 | o_reset_to_empty_unquoted(&ctx.as_string); |
| 6607 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6608 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6609 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6610 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6611 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6612 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6613 | if (ctx.ctx_res_w == RES_MATCH |
| 6614 | && ctx.command->argv == NULL /* not (word|(... */ |
| 6615 | && dest.length == 0 /* not word(... */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6616 | && dest.o_quoted == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6617 | ) { |
| 6618 | continue; |
| 6619 | } |
| 6620 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6621 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6622 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 6623 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6624 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6625 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6626 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6627 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6628 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6629 | goto case_semi; |
| 6630 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6631 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 6632 | /* proper use of this character is caught by end_trigger: |
| 6633 | * if we see {, we call parse_group(..., end_trigger='}') |
| 6634 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 6635 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6636 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6637 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 6638 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 6639 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6640 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6641 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6642 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6643 | parse_error: |
| 6644 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6645 | struct parse_context *pctx; |
| 6646 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6647 | |
| 6648 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 6649 | * Sample for finding leaks on syntax error recovery path. |
| 6650 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6651 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 6652 | * Samples to catch leaks at execution: |
| 6653 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 6654 | * 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] | 6655 | */ |
| 6656 | pctx = &ctx; |
| 6657 | do { |
| 6658 | /* Update pipe/command counts, |
| 6659 | * otherwise freeing may miss some */ |
| 6660 | done_pipe(pctx, PIPE_SEQ); |
| 6661 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 6662 | pctx->list_head, pctx); |
| 6663 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6664 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6665 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6666 | #if !BB_MMU |
| 6667 | o_free_unsafe(&pctx->as_string); |
| 6668 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6669 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6670 | if (pctx != &ctx) { |
| 6671 | free(pctx); |
| 6672 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6673 | IF_HAS_KEYWORDS(pctx = p2;) |
| 6674 | } while (HAS_KEYWORDS && pctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6675 | /* Free text, clear all dest fields */ |
| 6676 | o_free(&dest); |
| 6677 | /* If we are not in top-level parse, we return, |
| 6678 | * our caller will propagate error. |
| 6679 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6680 | if (end_trigger != ';') { |
| 6681 | #if !BB_MMU |
| 6682 | if (pstring) |
| 6683 | *pstring = NULL; |
| 6684 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6685 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6686 | return ERR_PTR; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6687 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6688 | /* Discard cached input, force prompt */ |
| 6689 | input->p = NULL; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6690 | IF_HUSH_INTERACTIVE(input->promptme = 1;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6691 | goto reset; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6692 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6693 | } |
| 6694 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6695 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6696 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 6697 | * end_trigger controls how often we stop parsing |
| 6698 | * NUL: parse all, execute, return |
| 6699 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 6700 | */ |
| 6701 | 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] | 6702 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6703 | /* Why we need empty flag? |
| 6704 | * An obscure corner case "false; ``; echo $?": |
| 6705 | * empty command in `` should still set $? to 0. |
| 6706 | * But we can't just set $? to 0 at the start, |
| 6707 | * this breaks "false; echo `echo $?`" case. |
| 6708 | */ |
| 6709 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6710 | while (1) { |
| 6711 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6712 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6713 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6714 | if (!pipe_list) { /* EOF */ |
| 6715 | if (empty) |
| 6716 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6717 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6718 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6719 | debug_print_tree(pipe_list, 0); |
| 6720 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 6721 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6722 | empty = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6723 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6724 | } |
| 6725 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6726 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6727 | { |
| 6728 | struct in_str input; |
| 6729 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6730 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6731 | } |
| 6732 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6733 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6734 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6735 | struct in_str input; |
| 6736 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6737 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6738 | } |
| 6739 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6740 | /* Called a few times only (or even once if "sh -c") */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 6741 | static void init_sigmasks(void) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 6742 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6743 | unsigned sig; |
| 6744 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 6745 | sigset_t old_blocked_set; |
| 6746 | |
| 6747 | if (!G.inherited_set_is_saved) { |
| 6748 | sigprocmask(SIG_SETMASK, NULL, &G.blocked_set); |
| 6749 | G.inherited_set = G.blocked_set; |
| 6750 | } |
| 6751 | old_blocked_set = G.blocked_set; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 6752 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6753 | mask = (1 << SIGQUIT); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6754 | if (G_interactive_fd) { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 6755 | mask = (1 << SIGQUIT) | SPECIAL_INTERACTIVE_SIGS; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6756 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6757 | mask |= SPECIAL_JOB_SIGS; |
| 6758 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6759 | G.non_DFL_mask = mask; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 6760 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6761 | sig = 0; |
| 6762 | while (mask) { |
| 6763 | if (mask & 1) |
| 6764 | sigaddset(&G.blocked_set, sig); |
| 6765 | mask >>= 1; |
| 6766 | sig++; |
| 6767 | } |
| 6768 | sigdelset(&G.blocked_set, SIGCHLD); |
| 6769 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 6770 | if (memcmp(&old_blocked_set, &G.blocked_set, sizeof(old_blocked_set)) != 0) |
| 6771 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 6772 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6773 | /* POSIX allows shell to re-enable SIGCHLD |
| 6774 | * even if it was SIG_IGN on entry */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 6775 | #if ENABLE_HUSH_FAST |
| 6776 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 6777 | if (!G.inherited_set_is_saved) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 6778 | signal(SIGCHLD, SIGCHLD_handler); |
| 6779 | #else |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 6780 | if (!G.inherited_set_is_saved) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 6781 | signal(SIGCHLD, SIG_DFL); |
| 6782 | #endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 6783 | |
| 6784 | G.inherited_set_is_saved = 1; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6785 | } |
| 6786 | |
| 6787 | #if ENABLE_HUSH_JOB |
| 6788 | /* helper */ |
| 6789 | static void maybe_set_to_sigexit(int sig) |
| 6790 | { |
| 6791 | void (*handler)(int); |
| 6792 | /* non_DFL_mask'ed signals are, well, masked, |
| 6793 | * no need to set handler for them. |
| 6794 | */ |
| 6795 | if (!((G.non_DFL_mask >> sig) & 1)) { |
| 6796 | handler = signal(sig, sigexit); |
| 6797 | if (handler == SIG_IGN) /* oops... restore back to IGN! */ |
| 6798 | signal(sig, handler); |
| 6799 | } |
| 6800 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6801 | /* Set handlers to restore tty pgrp and exit */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6802 | static void set_fatal_handlers(void) |
| 6803 | { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 6804 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6805 | if (HUSH_DEBUG) { |
| 6806 | maybe_set_to_sigexit(SIGILL ); |
| 6807 | maybe_set_to_sigexit(SIGFPE ); |
| 6808 | maybe_set_to_sigexit(SIGBUS ); |
| 6809 | maybe_set_to_sigexit(SIGSEGV); |
| 6810 | maybe_set_to_sigexit(SIGTRAP); |
| 6811 | } /* else: hush is perfect. what SEGV? */ |
| 6812 | maybe_set_to_sigexit(SIGABRT); |
| 6813 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 6814 | maybe_set_to_sigexit(SIGPIPE); |
| 6815 | maybe_set_to_sigexit(SIGALRM); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 6816 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are masked. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6817 | * if we aren't interactive... but in this case |
| 6818 | * 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] | 6819 | /*maybe_set_to_sigexit(SIGHUP );*/ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6820 | /*maybe_set_to_sigexit(SIGTERM);*/ |
| 6821 | /*maybe_set_to_sigexit(SIGINT );*/ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 6822 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 6823 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 6824 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6825 | static int set_mode(const char cstate, const char mode) |
| 6826 | { |
| 6827 | int state = (cstate == '-' ? 1 : 0); |
| 6828 | switch (mode) { |
| 6829 | case 'n': G.fake_mode = state; break; |
| 6830 | case 'x': /*G.debug_mode = state;*/ break; |
| 6831 | default: return EXIT_FAILURE; |
| 6832 | } |
| 6833 | return EXIT_SUCCESS; |
| 6834 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6835 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 6836 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 6837 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6838 | { |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6839 | static const struct variable const_shell_ver = { |
| 6840 | .next = NULL, |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 6841 | .varstr = (char*)hush_version_str, |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6842 | .max_len = 1, /* 0 can provoke free(name) */ |
| 6843 | .flg_export = 1, |
| 6844 | .flg_read_only = 1, |
| 6845 | }; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6846 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6847 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6848 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6849 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 6850 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 6851 | INIT_G(); |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 6852 | if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6853 | G.last_exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6854 | #if !BB_MMU |
| 6855 | G.argv0_for_re_execing = argv[0]; |
| 6856 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6857 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6858 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 6859 | G.top_var = &G.shell_ver; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6860 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6861 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 6862 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6863 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6864 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6865 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6866 | if (e) while (*e) { |
| 6867 | char *value = strchr(*e, '='); |
| 6868 | if (value) { /* paranoia */ |
| 6869 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 6870 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 6871 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6872 | cur_var->max_len = strlen(*e); |
| 6873 | cur_var->flg_export = 1; |
| 6874 | } |
| 6875 | e++; |
| 6876 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 6877 | /* reinstate HUSH_VERSION */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6878 | debug_printf_env("putenv '%s'\n", hush_version_str); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 6879 | putenv((char *)hush_version_str); |
| 6880 | |
| 6881 | /* Export PWD */ |
| 6882 | set_pwd_var(/*exp:*/ 1); |
| 6883 | /* bash also exports SHLVL and _, |
| 6884 | * and sets (but doesn't export) the following variables: |
| 6885 | * BASH=/bin/bash |
| 6886 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 6887 | * BASH_VERSION='3.2.0(1)-release' |
| 6888 | * HOSTTYPE=i386 |
| 6889 | * MACHTYPE=i386-pc-linux-gnu |
| 6890 | * OSTYPE=linux-gnu |
| 6891 | * HOSTNAME=<xxxxxxxxxx> |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 6892 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 6893 | * EUID=<NNNNN> |
| 6894 | * UID=<NNNNN> |
| 6895 | * GROUPS=() |
| 6896 | * LINES=<NNN> |
| 6897 | * COLUMNS=<NNN> |
| 6898 | * BASH_ARGC=() |
| 6899 | * BASH_ARGV=() |
| 6900 | * BASH_LINENO=() |
| 6901 | * BASH_SOURCE=() |
| 6902 | * DIRSTACK=() |
| 6903 | * PIPESTATUS=([0]="0") |
| 6904 | * HISTFILE=/<xxx>/.bash_history |
| 6905 | * HISTFILESIZE=500 |
| 6906 | * HISTSIZE=500 |
| 6907 | * MAILCHECK=60 |
| 6908 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 6909 | * SHELL=/bin/bash |
| 6910 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 6911 | * TERM=dumb |
| 6912 | * OPTERR=1 |
| 6913 | * OPTIND=1 |
| 6914 | * IFS=$' \t\n' |
| 6915 | * PS1='\s-\v\$ ' |
| 6916 | * PS2='> ' |
| 6917 | * PS4='+ ' |
| 6918 | */ |
| 6919 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 6920 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6921 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 6922 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6923 | G.global_argc = argc; |
| 6924 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 6925 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 6926 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 6927 | |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 6928 | if (setjmp(die_jmp)) { |
| 6929 | /* xfunc has failed! die die die */ |
| 6930 | /* no EXIT traps, this is an escape hatch! */ |
| 6931 | G.exiting = 1; |
| 6932 | hush_exit(xfunc_error_retval); |
| 6933 | } |
| 6934 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6935 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 6936 | * init_sigmasks() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6937 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 6938 | * If we later decide that we are interactive, we run init_sigmasks() |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6939 | * in order to intercept (more) signals. |
| 6940 | */ |
| 6941 | |
| 6942 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 6943 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6944 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6945 | while (1) { |
Denys Vlasenko | a67a962 | 2009-08-20 03:38:58 +0200 | [diff] [blame] | 6946 | opt = getopt(argc, argv, "+c:xins" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6947 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 6948 | "<:$:R:V:" |
| 6949 | # if ENABLE_HUSH_FUNCTIONS |
| 6950 | "F:" |
| 6951 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6952 | #endif |
| 6953 | ); |
| 6954 | if (opt <= 0) |
| 6955 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6956 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6957 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6958 | /* Possibilities: |
| 6959 | * sh ... -c 'script' |
| 6960 | * sh ... -c 'script' ARG0 [ARG1...] |
| 6961 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 6962 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6963 | * "" needs to be replaced with NULL |
| 6964 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 6965 | * Note: the form without ARG0 never happens: |
| 6966 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6967 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 6968 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6969 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 6970 | G.root_ppid = getppid(); |
| 6971 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6972 | G.global_argv = argv + optind; |
| 6973 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6974 | if (builtin_argc) { |
| 6975 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 6976 | const struct built_in_command *x; |
| 6977 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 6978 | init_sigmasks(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6979 | x = find_builtin(optarg); |
| 6980 | if (x) { /* paranoia */ |
| 6981 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 6982 | G.global_argv += builtin_argc; |
| 6983 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 6984 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6985 | } |
| 6986 | goto final_return; |
| 6987 | } |
| 6988 | if (!G.global_argv[0]) { |
| 6989 | /* -c 'script' (no params): prevent empty $0 */ |
| 6990 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 6991 | G.global_argv[0] = argv[0]; |
| 6992 | G.global_argc--; |
| 6993 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 6994 | init_sigmasks(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6995 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6996 | goto final_return; |
| 6997 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 6998 | /* Well, we cannot just declare interactiveness, |
| 6999 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7000 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7001 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 7002 | case 's': |
| 7003 | /* "-s" means "read from stdin", but this is how we always |
| 7004 | * operate, so simply do nothing here. */ |
| 7005 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7006 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 7007 | case '<': /* "big heredoc" support */ |
| 7008 | full_write(STDOUT_FILENO, optarg, strlen(optarg)); |
| 7009 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7010 | case '$': { |
| 7011 | unsigned long long empty_trap_mask; |
| 7012 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7013 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 7014 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7015 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 7016 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7017 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 7018 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7019 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7020 | optarg++; |
| 7021 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7022 | optarg++; |
| 7023 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 7024 | if (empty_trap_mask != 0) { |
| 7025 | int sig; |
| 7026 | init_sigmasks(); |
| 7027 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 7028 | for (sig = 1; sig < NSIG; sig++) { |
| 7029 | if (empty_trap_mask & (1LL << sig)) { |
| 7030 | G.traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 7031 | sigaddset(&G.blocked_set, sig); |
| 7032 | } |
| 7033 | } |
| 7034 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7035 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7036 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7037 | optarg++; |
| 7038 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7039 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7040 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7041 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7042 | case 'R': |
| 7043 | case 'V': |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7044 | set_local_var(xstrdup(optarg), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ opt == 'R'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7045 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 7046 | # if ENABLE_HUSH_FUNCTIONS |
| 7047 | case 'F': { |
| 7048 | struct function *funcp = new_function(optarg); |
| 7049 | /* funcp->name is already set to optarg */ |
| 7050 | /* funcp->body is set to NULL. It's a special case. */ |
| 7051 | funcp->body_as_string = argv[optind]; |
| 7052 | optind++; |
| 7053 | break; |
| 7054 | } |
| 7055 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7056 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7057 | case 'n': |
| 7058 | case 'x': |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7059 | if (!set_mode('-', opt)) |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7060 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7061 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7062 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7063 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 7064 | " or: sh -c command [args]...\n\n"); |
| 7065 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7066 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7067 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7068 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7069 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7070 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7071 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7072 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7073 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7074 | G.root_ppid = getppid(); |
| 7075 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7076 | |
| 7077 | /* If we are login shell... */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7078 | if (argv[0] && argv[0][0] == '-') { |
| 7079 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7080 | debug_printf("sourcing /etc/profile\n"); |
| 7081 | input = fopen_for_read("/etc/profile"); |
| 7082 | if (input != NULL) { |
| 7083 | close_on_exec_on(fileno(input)); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7084 | init_sigmasks(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7085 | parse_and_run_file(input); |
| 7086 | fclose(input); |
| 7087 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7088 | /* bash: after sourcing /etc/profile, |
| 7089 | * tries to source (in the given order): |
| 7090 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 7091 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7092 | * bash also sources ~/.bash_logout on exit. |
| 7093 | * If called as sh, skips .bash_XXX files. |
| 7094 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7095 | } |
| 7096 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7097 | if (argv[optind]) { |
| 7098 | FILE *input; |
| 7099 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7100 | * "bash <script>" (which is never interactive (unless -i?)) |
| 7101 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7102 | * If called as sh, does the same but with $ENV. |
| 7103 | */ |
| 7104 | debug_printf("running script '%s'\n", argv[optind]); |
| 7105 | G.global_argv = argv + optind; |
| 7106 | G.global_argc = argc - optind; |
| 7107 | input = xfopen_for_read(argv[optind]); |
| 7108 | close_on_exec_on(fileno(input)); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7109 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7110 | parse_and_run_file(input); |
| 7111 | #if ENABLE_FEATURE_CLEAN_UP |
| 7112 | fclose(input); |
| 7113 | #endif |
| 7114 | goto final_return; |
| 7115 | } |
| 7116 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7117 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7118 | * NB: don't forget to (re)run init_sigmasks() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7119 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7120 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 7121 | /* A shell is interactive if the '-i' flag was given, |
| 7122 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 7123 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7124 | * no arguments remaining or the -s flag given |
| 7125 | * standard input is a terminal |
| 7126 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7127 | * Refer to Posix.2, the description of the 'sh' utility. |
| 7128 | */ |
| 7129 | #if ENABLE_HUSH_JOB |
| 7130 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7131 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 7132 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 7133 | if (G_saved_tty_pgrp < 0) |
| 7134 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7135 | |
| 7136 | /* try to dup stdin to high fd#, >= 255 */ |
| 7137 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 7138 | if (G_interactive_fd < 0) { |
| 7139 | /* try to dup to any fd */ |
| 7140 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7141 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7142 | /* give up */ |
| 7143 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7144 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 7145 | } |
| 7146 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7147 | // TODO: track & disallow any attempts of user |
| 7148 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7149 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7150 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7151 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7152 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7153 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7154 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7155 | /* If we were run as 'hush &', sleep until we are |
| 7156 | * in the foreground (tty pgrp == our pgrp). |
| 7157 | * If we get started under a job aware app (like bash), |
| 7158 | * make sure we are now in charge so we don't fight over |
| 7159 | * who gets the foreground */ |
| 7160 | while (1) { |
| 7161 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7162 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 7163 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7164 | break; |
| 7165 | /* send TTIN to ourself (should stop us) */ |
| 7166 | kill(- shell_pgrp, SIGTTIN); |
| 7167 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7168 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7169 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7170 | /* Block some signals */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7171 | init_sigmasks(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7172 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7173 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7174 | /* Set other signals to restore saved_tty_pgrp */ |
| 7175 | set_fatal_handlers(); |
| 7176 | /* Put ourselves in our own process group |
| 7177 | * (bash, too, does this only if ctty is available) */ |
| 7178 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 7179 | /* Grab control of the terminal */ |
| 7180 | tcsetpgrp(G_interactive_fd, getpid()); |
| 7181 | } |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 7182 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 7183 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 7184 | enable_restore_tty_pgrp_on_exit(); /* sets die_sleep = -1 */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7185 | } else { |
| 7186 | init_sigmasks(); |
| 7187 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7188 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7189 | /* No job control compiled in, only prompt/line editing */ |
| 7190 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7191 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 7192 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7193 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7194 | G_interactive_fd = dup(STDIN_FILENO); |
| 7195 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7196 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7197 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7198 | } |
| 7199 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7200 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7201 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7202 | } |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7203 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7204 | #else |
| 7205 | /* We have interactiveness code disabled */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7206 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7207 | #endif |
| 7208 | /* bash: |
| 7209 | * if interactive but not a login shell, sources ~/.bashrc |
| 7210 | * (--norc turns this off, --rcfile <file> overrides) |
| 7211 | */ |
| 7212 | |
| 7213 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 7214 | /* note: ash and hush share this string */ |
| 7215 | printf("\n\n%s %s\n" |
| 7216 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 7217 | "\n", |
| 7218 | bb_banner, |
| 7219 | "hush - the humble shell" |
| 7220 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 7221 | } |
| 7222 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7223 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7224 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7225 | final_return: |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 7226 | #if ENABLE_FEATURE_CLEAN_UP |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7227 | if (G.cwd != bb_msg_unknown) |
| 7228 | free((char*)G.cwd); |
| 7229 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7230 | while (cur_var) { |
| 7231 | struct variable *tmp = cur_var; |
| 7232 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 7233 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7234 | cur_var = cur_var->next; |
| 7235 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 7236 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7237 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7238 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7239 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 7240 | |
| 7241 | |
| 7242 | #if ENABLE_LASH |
| 7243 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 7244 | int lash_main(int argc, char **argv) |
| 7245 | { |
Denys Vlasenko | 0793491 | 2009-08-20 03:40:04 +0200 | [diff] [blame] | 7246 | bb_error_msg("lash is deprecated, please use hush instead"); |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 7247 | return hush_main(argc, argv); |
| 7248 | } |
| 7249 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7250 | |
Denys Vlasenko | 1cc4b13 | 2009-08-21 00:05:51 +0200 | [diff] [blame] | 7251 | #if ENABLE_MSH |
| 7252 | int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 7253 | int msh_main(int argc, char **argv) |
| 7254 | { |
| 7255 | //bb_error_msg("msh is deprecated, please use hush instead"); |
| 7256 | return hush_main(argc, argv); |
| 7257 | } |
| 7258 | #endif |
| 7259 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7260 | |
| 7261 | /* |
| 7262 | * Built-ins |
| 7263 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7264 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7265 | { |
| 7266 | return 0; |
| 7267 | } |
| 7268 | |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 7269 | 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] | 7270 | { |
| 7271 | int argc = 0; |
| 7272 | while (*argv) { |
| 7273 | argc++; |
| 7274 | argv++; |
| 7275 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 7276 | return applet_main_func(argc, argv - argc); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 7277 | } |
| 7278 | |
| 7279 | static int FAST_FUNC builtin_test(char **argv) |
| 7280 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7281 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7282 | } |
| 7283 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7284 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7285 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7286 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7287 | } |
| 7288 | |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 7289 | #if ENABLE_PRINTF |
| 7290 | static int FAST_FUNC builtin_printf(char **argv) |
| 7291 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7292 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 7293 | } |
| 7294 | #endif |
| 7295 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7296 | static char **skip_dash_dash(char **argv) |
| 7297 | { |
| 7298 | argv++; |
| 7299 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 7300 | argv++; |
| 7301 | return argv; |
| 7302 | } |
| 7303 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7304 | static int FAST_FUNC builtin_eval(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7305 | { |
| 7306 | int rcode = EXIT_SUCCESS; |
| 7307 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7308 | argv = skip_dash_dash(argv); |
| 7309 | if (*argv) { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7310 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7311 | /* bash: |
| 7312 | * eval "echo Hi; done" ("done" is syntax error): |
| 7313 | * "echo Hi" will not execute too. |
| 7314 | */ |
| 7315 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7316 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7317 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7318 | } |
| 7319 | return rcode; |
| 7320 | } |
| 7321 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7322 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7323 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7324 | const char *newdir; |
| 7325 | |
| 7326 | argv = skip_dash_dash(argv); |
| 7327 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7328 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7329 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7330 | * bash says "bash: cd: HOME not set" and does nothing |
| 7331 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7332 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 7333 | const char *home = get_local_var_value("HOME"); |
| 7334 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7335 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7336 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7337 | /* Mimic bash message exactly */ |
| 7338 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7339 | return EXIT_FAILURE; |
| 7340 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7341 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 7342 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 7343 | * set it again, but do not export. bash does the same. |
| 7344 | */ |
| 7345 | set_pwd_var(/*exp:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7346 | return EXIT_SUCCESS; |
| 7347 | } |
| 7348 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7349 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7350 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7351 | argv = skip_dash_dash(argv); |
| 7352 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7353 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 7354 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 7355 | /* Careful: we can end up here after [v]fork. Do not restore |
| 7356 | * tty pgrp then, only top-level shell process does that */ |
| 7357 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 7358 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 7359 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 7360 | /* TODO: if exec fails, bash does NOT exit! We do. |
| 7361 | * We'll need to undo sigprocmask (it's inside execvp_or_die) |
| 7362 | * and tcsetpgrp, and this is inherently racy. |
| 7363 | */ |
| 7364 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7365 | } |
| 7366 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7367 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7368 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 7369 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 7370 | |
| 7371 | /* interactive bash: |
| 7372 | * # trap "echo EEE" EXIT |
| 7373 | * # exit |
| 7374 | * exit |
| 7375 | * There are stopped jobs. |
| 7376 | * (if there are _stopped_ jobs, running ones don't count) |
| 7377 | * # exit |
| 7378 | * exit |
| 7379 | # EEE (then bash exits) |
| 7380 | * |
| 7381 | * we can use G.exiting = -1 as indicator "last cmd was exit" |
| 7382 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 7383 | |
| 7384 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7385 | argv = skip_dash_dash(argv); |
| 7386 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7387 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7388 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 7389 | xfunc_error_retval = 255; |
| 7390 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7391 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7392 | } |
| 7393 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7394 | static void print_escaped(const char *s) |
| 7395 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7396 | if (*s == '\'') |
| 7397 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7398 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7399 | const char *p = strchrnul(s, '\''); |
| 7400 | /* print 'xxxx', possibly just '' */ |
| 7401 | printf("'%.*s'", (int)(p - s), s); |
| 7402 | if (*p == '\0') |
| 7403 | break; |
| 7404 | s = p; |
| 7405 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7406 | /* s points to '; print "'''...'''" */ |
| 7407 | putchar('"'); |
| 7408 | do putchar('\''); while (*++s == '\''); |
| 7409 | putchar('"'); |
| 7410 | } while (*s); |
| 7411 | } |
| 7412 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7413 | #if !ENABLE_HUSH_LOCAL |
| 7414 | #define helper_export_local(argv, exp, lvl) \ |
| 7415 | helper_export_local(argv, exp) |
| 7416 | #endif |
| 7417 | static void helper_export_local(char **argv, int exp, int lvl) |
| 7418 | { |
| 7419 | do { |
| 7420 | char *name = *argv; |
| 7421 | |
| 7422 | /* So far we do not check that name is valid (TODO?) */ |
| 7423 | |
| 7424 | if (strchr(name, '=') == NULL) { |
| 7425 | struct variable *var; |
| 7426 | |
| 7427 | var = get_local_var(name); |
| 7428 | if (exp == -1) { /* unexporting? */ |
| 7429 | /* export -n NAME (without =VALUE) */ |
| 7430 | if (var) { |
| 7431 | var->flg_export = 0; |
| 7432 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 7433 | unsetenv(name); |
| 7434 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 7435 | continue; |
| 7436 | } |
| 7437 | if (exp == 1) { /* exporting? */ |
| 7438 | /* export NAME (without =VALUE) */ |
| 7439 | if (var) { |
| 7440 | var->flg_export = 1; |
| 7441 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 7442 | putenv(var->varstr); |
| 7443 | continue; |
| 7444 | } |
| 7445 | } |
| 7446 | /* Exporting non-existing variable. |
| 7447 | * bash does not put it in environment, |
| 7448 | * but remembers that it is exported, |
| 7449 | * and does put it in env when it is set later. |
| 7450 | * We just set it to "" and export. */ |
| 7451 | /* Or, it's "local NAME" (without =VALUE). |
| 7452 | * bash sets the value to "". */ |
| 7453 | name = xasprintf("%s=", name); |
| 7454 | } else { |
| 7455 | /* (Un)exporting/making local NAME=VALUE */ |
| 7456 | name = xstrdup(name); |
| 7457 | } |
| 7458 | set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0); |
| 7459 | } while (*++argv); |
| 7460 | } |
| 7461 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7462 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7463 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 7464 | unsigned opt_unexport; |
| 7465 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 7466 | #if ENABLE_HUSH_EXPORT_N |
| 7467 | /* "!": do not abort on errors */ |
| 7468 | opt_unexport = getopt32(argv, "!n"); |
| 7469 | if (opt_unexport == (uint32_t)-1) |
| 7470 | return EXIT_FAILURE; |
| 7471 | argv += optind; |
| 7472 | #else |
| 7473 | opt_unexport = 0; |
| 7474 | argv++; |
| 7475 | #endif |
| 7476 | |
| 7477 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7478 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7479 | if (e) { |
| 7480 | while (*e) { |
| 7481 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7482 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7483 | #else |
| 7484 | /* ash emits: export VAR='VAL' |
| 7485 | * bash: declare -x VAR="VAL" |
| 7486 | * we follow ash example */ |
| 7487 | const char *s = *e++; |
| 7488 | const char *p = strchr(s, '='); |
| 7489 | |
| 7490 | if (!p) /* wtf? take next variable */ |
| 7491 | continue; |
| 7492 | /* export var= */ |
| 7493 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7494 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7495 | putchar('\n'); |
| 7496 | #endif |
| 7497 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 7498 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7499 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7500 | return EXIT_SUCCESS; |
| 7501 | } |
| 7502 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7503 | helper_export_local(argv, (opt_unexport ? -1 : 1), 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7504 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7505 | return EXIT_SUCCESS; |
| 7506 | } |
| 7507 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7508 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7509 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7510 | { |
| 7511 | if (G.func_nest_level == 0) { |
| 7512 | bb_error_msg("%s: not in a function", argv[0]); |
| 7513 | return EXIT_FAILURE; /* bash compat */ |
| 7514 | } |
| 7515 | helper_export_local(argv, 0, G.func_nest_level); |
| 7516 | return EXIT_SUCCESS; |
| 7517 | } |
| 7518 | #endif |
| 7519 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7520 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7521 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7522 | int sig; |
| 7523 | char *new_cmd; |
| 7524 | |
| 7525 | if (!G.traps) |
| 7526 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 7527 | |
| 7528 | argv++; |
| 7529 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7530 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7531 | /* No args: print all trapped */ |
| 7532 | for (i = 0; i < NSIG; ++i) { |
| 7533 | if (G.traps[i]) { |
| 7534 | printf("trap -- "); |
| 7535 | print_escaped(G.traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 7536 | /* note: bash adds "SIG", but only if invoked |
| 7537 | * as "bash". If called as "sh", or if set -o posix, |
| 7538 | * then it prints short signal names. |
| 7539 | * We are printing short names: */ |
| 7540 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7541 | } |
| 7542 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 7543 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7544 | return EXIT_SUCCESS; |
| 7545 | } |
| 7546 | |
| 7547 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7548 | /* If first arg is a number: reset all specified signals */ |
| 7549 | sig = bb_strtou(*argv, NULL, 10); |
| 7550 | if (errno == 0) { |
| 7551 | int ret; |
| 7552 | process_sig_list: |
| 7553 | ret = EXIT_SUCCESS; |
| 7554 | while (*argv) { |
| 7555 | sig = get_signum(*argv++); |
| 7556 | if (sig < 0 || sig >= NSIG) { |
| 7557 | ret = EXIT_FAILURE; |
| 7558 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7559 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7560 | continue; |
| 7561 | } |
| 7562 | |
| 7563 | free(G.traps[sig]); |
| 7564 | G.traps[sig] = xstrdup(new_cmd); |
| 7565 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7566 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7567 | get_signame(sig), sig, G.traps[sig]); |
| 7568 | |
| 7569 | /* There is no signal for 0 (EXIT) */ |
| 7570 | if (sig == 0) |
| 7571 | continue; |
| 7572 | |
| 7573 | if (new_cmd) { |
| 7574 | sigaddset(&G.blocked_set, sig); |
| 7575 | } else { |
| 7576 | /* There was a trap handler, we are removing it |
| 7577 | * (if sig has non-DFL handling, |
| 7578 | * we don't need to do anything) */ |
| 7579 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 7580 | continue; |
| 7581 | sigdelset(&G.blocked_set, sig); |
| 7582 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7583 | } |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7584 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7585 | return ret; |
| 7586 | } |
| 7587 | |
| 7588 | if (!argv[1]) { /* no second arg */ |
| 7589 | bb_error_msg("trap: invalid arguments"); |
| 7590 | return EXIT_FAILURE; |
| 7591 | } |
| 7592 | |
| 7593 | /* First arg is "-": reset all specified to default */ |
| 7594 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 7595 | /* Everything else: set arg as signal handler |
| 7596 | * (includes "" case, which ignores signal) */ |
| 7597 | if (argv[0][0] == '-') { |
| 7598 | if (argv[0][1] == '\0') { /* "-" */ |
| 7599 | /* new_cmd remains NULL: "reset these sigs" */ |
| 7600 | goto reset_traps; |
| 7601 | } |
| 7602 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 7603 | argv++; |
| 7604 | } |
| 7605 | /* else: "-something", no special meaning */ |
| 7606 | } |
| 7607 | new_cmd = *argv; |
| 7608 | reset_traps: |
| 7609 | argv++; |
| 7610 | goto process_sig_list; |
| 7611 | } |
| 7612 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7613 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7614 | static int FAST_FUNC builtin_type(char **argv) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7615 | { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7616 | int ret = EXIT_SUCCESS; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7617 | |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7618 | while (*++argv) { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7619 | const char *type; |
Denys Vlasenko | 171932d | 2009-05-28 17:07:22 +0200 | [diff] [blame] | 7620 | char *path = NULL; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7621 | |
| 7622 | if (0) {} /* make conditional compile easier below */ |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7623 | /*else if (find_alias(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7624 | type = "an alias";*/ |
| 7625 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7626 | else if (find_function(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7627 | type = "a function"; |
| 7628 | #endif |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7629 | else if (find_builtin(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7630 | type = "a shell builtin"; |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7631 | else if ((path = find_in_path(*argv)) != NULL) |
| 7632 | type = path; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7633 | else { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7634 | bb_error_msg("type: %s: not found", *argv); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7635 | ret = EXIT_FAILURE; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7636 | continue; |
| 7637 | } |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7638 | |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7639 | printf("%s is %s\n", *argv, type); |
| 7640 | free(path); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7641 | } |
| 7642 | |
| 7643 | return ret; |
| 7644 | } |
| 7645 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7646 | #if ENABLE_HUSH_JOB |
| 7647 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7648 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7649 | { |
| 7650 | int i, jobnum; |
| 7651 | struct pipe *pi; |
| 7652 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7653 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7654 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7655 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7656 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 7657 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7658 | for (pi = G.job_list; pi; pi = pi->next) { |
| 7659 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7660 | goto found; |
| 7661 | } |
| 7662 | } |
| 7663 | bb_error_msg("%s: no current job", argv[0]); |
| 7664 | return EXIT_FAILURE; |
| 7665 | } |
| 7666 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 7667 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 7668 | return EXIT_FAILURE; |
| 7669 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7670 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7671 | if (pi->jobid == jobnum) { |
| 7672 | goto found; |
| 7673 | } |
| 7674 | } |
| 7675 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 7676 | return EXIT_FAILURE; |
| 7677 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 7678 | /* TODO: bash prints a string representation |
| 7679 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7680 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7681 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7682 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7683 | } |
| 7684 | |
| 7685 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7686 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 7687 | for (i = 0; i < pi->num_cmds; i++) { |
| 7688 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 7689 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7690 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7691 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7692 | |
| 7693 | i = kill(- pi->pgrp, SIGCONT); |
| 7694 | if (i < 0) { |
| 7695 | if (errno == ESRCH) { |
| 7696 | delete_finished_bg_job(pi); |
| 7697 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7698 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7699 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7700 | } |
| 7701 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7702 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7703 | remove_bg_job(pi); |
| 7704 | return checkjobs_and_fg_shell(pi); |
| 7705 | } |
| 7706 | return EXIT_SUCCESS; |
| 7707 | } |
| 7708 | #endif |
| 7709 | |
| 7710 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7711 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7712 | { |
| 7713 | const struct built_in_command *x; |
| 7714 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7715 | printf( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7716 | "Built-in commands:\n" |
| 7717 | "------------------\n"); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7718 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7719 | if (x->b_descr) |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7720 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7721 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7722 | bb_putchar('\n'); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7723 | return EXIT_SUCCESS; |
| 7724 | } |
| 7725 | #endif |
| 7726 | |
| 7727 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7728 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7729 | { |
| 7730 | struct pipe *job; |
| 7731 | const char *status_string; |
| 7732 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7733 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7734 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7735 | status_string = "Stopped"; |
| 7736 | else |
| 7737 | status_string = "Running"; |
| 7738 | |
| 7739 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 7740 | } |
| 7741 | return EXIT_SUCCESS; |
| 7742 | } |
| 7743 | #endif |
| 7744 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7745 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7746 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7747 | { |
| 7748 | void *p; |
| 7749 | unsigned long l; |
| 7750 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7751 | # ifdef M_TRIM_THRESHOLD |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 7752 | /* Optional. Reduces probability of false positives */ |
| 7753 | malloc_trim(0); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7754 | # endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7755 | /* Crude attempt to find where "free memory" starts, |
| 7756 | * sans fragmentation. */ |
| 7757 | p = malloc(240); |
| 7758 | l = (unsigned long)p; |
| 7759 | free(p); |
| 7760 | p = malloc(3400); |
| 7761 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 7762 | free(p); |
| 7763 | |
| 7764 | if (!G.memleak_value) |
| 7765 | G.memleak_value = l; |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 7766 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7767 | l -= G.memleak_value; |
| 7768 | if ((long)l < 0) |
| 7769 | l = 0; |
| 7770 | l /= 1024; |
| 7771 | if (l > 127) |
| 7772 | l = 127; |
| 7773 | |
| 7774 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 7775 | return l; |
| 7776 | } |
| 7777 | #endif |
| 7778 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7779 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7780 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7781 | puts(get_cwd(0)); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7782 | return EXIT_SUCCESS; |
| 7783 | } |
| 7784 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7785 | static int FAST_FUNC builtin_read(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7786 | { |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 7787 | const char *r; |
| 7788 | char *opt_n = NULL; |
| 7789 | char *opt_p = NULL; |
| 7790 | char *opt_t = NULL; |
| 7791 | char *opt_u = NULL; |
| 7792 | int read_flags; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 7793 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 7794 | /* "!": do not abort on errors. |
| 7795 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 7796 | */ |
| 7797 | read_flags = getopt32(argv, "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u); |
| 7798 | if (read_flags == (uint32_t)-1) |
| 7799 | return EXIT_FAILURE; |
| 7800 | argv += optind; |
| 7801 | |
| 7802 | r = shell_builtin_read(set_local_var_from_halves, |
| 7803 | argv, |
| 7804 | get_local_var_value("IFS"), /* can be NULL */ |
| 7805 | read_flags, |
| 7806 | opt_n, |
| 7807 | opt_p, |
| 7808 | opt_t, |
| 7809 | opt_u |
| 7810 | ); |
| 7811 | |
| 7812 | if ((uintptr_t)r > 1) { |
| 7813 | bb_error_msg("%s", r); |
| 7814 | r = (char*)(uintptr_t)1; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 7815 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7816 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 7817 | return (uintptr_t)r; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7818 | } |
| 7819 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7820 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 7821 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7822 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7823 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 7824 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7825 | * set -- [argument...] |
| 7826 | * set -o |
| 7827 | * set +o |
| 7828 | * Implementations shall support the options in both their hyphen and |
| 7829 | * plus-sign forms. These options can also be specified as options to sh. |
| 7830 | * Examples: |
| 7831 | * Write out all variables and their values: set |
| 7832 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 7833 | * Turn on the -x and -v options: set -xv |
| 7834 | * Unset all positional parameters: set -- |
| 7835 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 7836 | * Set the positional parameters to the expansion of x, even if x expands |
| 7837 | * with a leading '-' or '+': set -- $x |
| 7838 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7839 | * 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] | 7840 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7841 | static int FAST_FUNC builtin_set(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7842 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7843 | int n; |
| 7844 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7845 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7846 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7847 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7848 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7849 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7850 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7851 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7852 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7853 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7854 | do { |
| 7855 | if (!strcmp(arg, "--")) { |
| 7856 | ++argv; |
| 7857 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7858 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 7859 | if (arg[0] != '+' && arg[0] != '-') |
| 7860 | break; |
| 7861 | for (n = 1; arg[n]; ++n) |
| 7862 | if (set_mode(arg[0], arg[n])) |
| 7863 | goto error; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7864 | } while ((arg = *++argv) != NULL); |
| 7865 | /* Now argv[0] is 1st argument */ |
| 7866 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7867 | if (arg == NULL) |
| 7868 | return EXIT_SUCCESS; |
| 7869 | set_argv: |
| 7870 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7871 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 7872 | g_argv = G.global_argv; |
| 7873 | if (G.global_args_malloced) { |
| 7874 | pp = g_argv; |
| 7875 | while (*++pp) |
| 7876 | free(*pp); |
| 7877 | g_argv[1] = NULL; |
| 7878 | } else { |
| 7879 | G.global_args_malloced = 1; |
| 7880 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 7881 | pp[0] = g_argv[0]; /* retain $0 */ |
| 7882 | g_argv = pp; |
| 7883 | } |
| 7884 | /* This realloc's G.global_argv */ |
| 7885 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 7886 | |
| 7887 | n = 1; |
| 7888 | while (*++pp) |
| 7889 | n++; |
| 7890 | G.global_argc = n; |
| 7891 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7892 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7893 | |
| 7894 | /* Nothing known, so abort */ |
| 7895 | error: |
| 7896 | bb_error_msg("set: %s: invalid option", arg); |
| 7897 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7898 | } |
| 7899 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7900 | static int FAST_FUNC builtin_shift(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7901 | { |
| 7902 | int n = 1; |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7903 | argv = skip_dash_dash(argv); |
| 7904 | if (argv[0]) { |
| 7905 | n = atoi(argv[0]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7906 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7907 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 7908 | if (G.global_args_malloced) { |
| 7909 | int m = 1; |
| 7910 | while (m <= n) |
| 7911 | free(G.global_argv[m++]); |
| 7912 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7913 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 7914 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 7915 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7916 | return EXIT_SUCCESS; |
| 7917 | } |
| 7918 | return EXIT_FAILURE; |
| 7919 | } |
| 7920 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7921 | static int FAST_FUNC builtin_source(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7922 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 7923 | char *arg_path, *filename; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7924 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7925 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7926 | #if ENABLE_HUSH_FUNCTIONS |
| 7927 | smallint sv_flg; |
| 7928 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7929 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7930 | argv = skip_dash_dash(argv); |
| 7931 | filename = argv[0]; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 7932 | if (!filename) { |
| 7933 | /* bash says: "bash: .: filename argument required" */ |
| 7934 | return 2; /* bash compat */ |
| 7935 | } |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7936 | arg_path = NULL; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 7937 | if (!strchr(filename, '/')) { |
| 7938 | arg_path = find_in_path(filename); |
| 7939 | if (arg_path) |
| 7940 | filename = arg_path; |
| 7941 | } |
| 7942 | input = fopen_or_warn(filename, "r"); |
| 7943 | free(arg_path); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7944 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7945 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7946 | return EXIT_FAILURE; |
| 7947 | } |
| 7948 | close_on_exec_on(fileno(input)); |
| 7949 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7950 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7951 | sv_flg = G.flag_return_in_progress; |
| 7952 | /* "we are inside sourced file, ok to use return" */ |
| 7953 | G.flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7954 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7955 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7956 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7957 | parse_and_run_file(input); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7958 | fclose(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7959 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7960 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7961 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7962 | G.flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7963 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7964 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7965 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7966 | } |
| 7967 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7968 | static int FAST_FUNC builtin_umask(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7969 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7970 | int rc; |
| 7971 | mode_t mask; |
| 7972 | |
| 7973 | mask = umask(0); |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7974 | argv = skip_dash_dash(argv); |
| 7975 | if (argv[0]) { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7976 | mode_t old_mask = mask; |
| 7977 | |
| 7978 | mask ^= 0777; |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7979 | rc = bb_parse_mode(argv[0], &mask); |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7980 | mask ^= 0777; |
| 7981 | if (rc == 0) { |
| 7982 | mask = old_mask; |
| 7983 | /* bash messages: |
| 7984 | * bash: umask: 'q': invalid symbolic mode operator |
| 7985 | * bash: umask: 999: octal number out of range |
| 7986 | */ |
Denys Vlasenko | 44c86ce | 2010-05-20 04:22:55 +0200 | [diff] [blame] | 7987 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7988 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7989 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7990 | rc = 1; |
| 7991 | /* Mimic bash */ |
| 7992 | printf("%04o\n", (unsigned) mask); |
| 7993 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7994 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7995 | umask(mask); |
| 7996 | |
| 7997 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7998 | } |
| 7999 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8000 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8001 | static int FAST_FUNC builtin_unset(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8002 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8003 | int ret; |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8004 | unsigned opts; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8005 | |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8006 | /* "!": do not abort on errors */ |
| 8007 | /* "+": stop at 1st non-option */ |
| 8008 | opts = getopt32(argv, "!+vf"); |
| 8009 | if (opts == (unsigned)-1) |
| 8010 | return EXIT_FAILURE; |
| 8011 | if (opts == 3) { |
| 8012 | bb_error_msg("unset: -v and -f are exclusive"); |
| 8013 | return EXIT_FAILURE; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8014 | } |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8015 | argv += optind; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8016 | |
| 8017 | ret = EXIT_SUCCESS; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8018 | while (*argv) { |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8019 | if (!(opts & 2)) { /* not -f */ |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8020 | if (unset_local_var(*argv)) { |
| 8021 | /* unset <nonexistent_var> doesn't fail. |
| 8022 | * Error is when one tries to unset RO var. |
| 8023 | * Message was printed by unset_local_var. */ |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8024 | ret = EXIT_FAILURE; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8025 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8026 | } |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8027 | #if ENABLE_HUSH_FUNCTIONS |
| 8028 | else { |
| 8029 | unset_func(*argv); |
| 8030 | } |
| 8031 | #endif |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8032 | argv++; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8033 | } |
| 8034 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8035 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8036 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8037 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8038 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8039 | { |
| 8040 | int ret = EXIT_SUCCESS; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8041 | int status, sig; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8042 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8043 | argv = skip_dash_dash(argv); |
| 8044 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8045 | /* Don't care about wait results */ |
| 8046 | /* Note 1: must wait until there are no more children */ |
| 8047 | /* Note 2: must be interruptible */ |
| 8048 | /* Examples: |
| 8049 | * $ sleep 3 & sleep 6 & wait |
| 8050 | * [1] 30934 sleep 3 |
| 8051 | * [2] 30935 sleep 6 |
| 8052 | * [1] Done sleep 3 |
| 8053 | * [2] Done sleep 6 |
| 8054 | * $ sleep 3 & sleep 6 & wait |
| 8055 | * [1] 30936 sleep 3 |
| 8056 | * [2] 30937 sleep 6 |
| 8057 | * [1] Done sleep 3 |
| 8058 | * ^C <-- after ~4 sec from keyboard |
| 8059 | * $ |
| 8060 | */ |
| 8061 | sigaddset(&G.blocked_set, SIGCHLD); |
| 8062 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 8063 | while (1) { |
| 8064 | checkjobs(NULL); |
| 8065 | if (errno == ECHILD) |
| 8066 | break; |
| 8067 | /* Wait for SIGCHLD or any other signal of interest */ |
| 8068 | /* sigtimedwait with infinite timeout: */ |
| 8069 | sig = sigwaitinfo(&G.blocked_set, NULL); |
| 8070 | if (sig > 0) { |
| 8071 | sig = check_and_run_traps(sig); |
| 8072 | if (sig && sig != SIGCHLD) { /* see note 2 */ |
| 8073 | ret = 128 + sig; |
| 8074 | break; |
| 8075 | } |
| 8076 | } |
| 8077 | } |
| 8078 | sigdelset(&G.blocked_set, SIGCHLD); |
| 8079 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 8080 | return ret; |
| 8081 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8082 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8083 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8084 | while (*argv) { |
| 8085 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 8086 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8087 | /* mimic bash message */ |
| 8088 | 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] | 8089 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8090 | } |
| 8091 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8092 | if (WIFSIGNALED(status)) |
| 8093 | ret = 128 + WTERMSIG(status); |
| 8094 | else if (WIFEXITED(status)) |
| 8095 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8096 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8097 | ret = EXIT_FAILURE; |
| 8098 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8099 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8100 | ret = 127; |
| 8101 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8102 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8103 | } |
| 8104 | |
| 8105 | return ret; |
| 8106 | } |
| 8107 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8108 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 8109 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 8110 | { |
| 8111 | if (argv[1]) { |
| 8112 | def = bb_strtou(argv[1], NULL, 10); |
| 8113 | if (errno || def < def_min || argv[2]) { |
| 8114 | bb_error_msg("%s: bad arguments", argv[0]); |
| 8115 | def = UINT_MAX; |
| 8116 | } |
| 8117 | } |
| 8118 | return def; |
| 8119 | } |
| 8120 | #endif |
| 8121 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 8122 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8123 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8124 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8125 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8126 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 8127 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 8128 | return EXIT_SUCCESS; /* bash compat */ |
| 8129 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8130 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8131 | |
| 8132 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 8133 | if (depth == UINT_MAX) |
| 8134 | G.flag_break_continue = BC_BREAK; |
| 8135 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8136 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8137 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8138 | return EXIT_SUCCESS; |
| 8139 | } |
| 8140 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8141 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8142 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 8143 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 8144 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8145 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 8146 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8147 | |
| 8148 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8149 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8150 | { |
| 8151 | int rc; |
| 8152 | |
| 8153 | if (G.flag_return_in_progress != -1) { |
| 8154 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 8155 | return EXIT_FAILURE; /* bash compat */ |
| 8156 | } |
| 8157 | |
| 8158 | G.flag_return_in_progress = 1; |
| 8159 | |
| 8160 | /* bash: |
| 8161 | * out of range: wraps around at 256, does not error out |
| 8162 | * non-numeric param: |
| 8163 | * f() { false; return qwe; }; f; echo $? |
| 8164 | * bash: return: qwe: numeric argument required <== we do this |
| 8165 | * 255 <== we also do this |
| 8166 | */ |
| 8167 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 8168 | return rc; |
| 8169 | } |
| 8170 | #endif |