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 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 42 | * <(list) and >(list) Process Substitution |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 43 | * Tilde Expansion |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 44 | * |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 45 | * Bash stuff (optionally enabled): |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 46 | * &> and >& redirection of stdout+stderr |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 47 | * Brace Expansion |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 48 | * reserved words: [[ ]] function select |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 49 | * substrings ${var:1:5} |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 50 | * let EXPR [EXPR...] |
| 51 | * Each EXPR is an arithmetic expression (ARITHMETIC EVALUATION) |
| 52 | * If the last arg evaluates to 0, let returns 1; 0 otherwise. |
| 53 | * NB: let `echo 'a=a + 1'` - error (IOW: multi-word expansion is used) |
| 54 | * ((EXPR)) |
| 55 | * The EXPR is evaluated according to ARITHMETIC EVALUATION. |
| 56 | * This is exactly equivalent to let "expression". |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 57 | * |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 58 | * TODOs: |
| 59 | * grep for "TODO" and fix (some of them are easy) |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 60 | * builtins: ulimit |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 61 | * special variables (done: PWD) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 62 | * follow IFS rules more precisely, including update semantics |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 63 | * export builtin should be special, its arguments are assignments |
| 64 | * and therefore expansion of them should be "one-word" expansion: |
| 65 | * $ export i=`echo 'a b'` # export has one arg: "i=a b" |
| 66 | * compare with: |
| 67 | * $ ls i=`echo 'a b'` # ls has two args: "i=a" and "b" |
| 68 | * ls: cannot access i=a: No such file or directory |
| 69 | * ls: cannot access b: No such file or directory |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 70 | * Note1: same applies to local builtin. |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 71 | * Note2: bash 3.2.33(1) does this only if export word itself |
| 72 | * is not quoted: |
| 73 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 74 | * aaa bbb |
| 75 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 76 | * aaa |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 77 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 78 | * 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] | 79 | */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 80 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 81 | #include <malloc.h> /* for malloc_trim */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 82 | #include <glob.h> |
| 83 | /* #include <dmalloc.h> */ |
| 84 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 85 | # include <fnmatch.h> |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 86 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 87 | #include "math.h" |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 88 | #include "match.h" |
Denys Vlasenko | cbe0b7f | 2009-10-09 22:00:58 +0200 | [diff] [blame] | 89 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 90 | # include "random.h" |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 91 | #else |
| 92 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 93 | #endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 94 | #ifndef PIPE_BUF |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 95 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 96 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 97 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 98 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 99 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 100 | #define LEAK_HUNTING 0 |
| 101 | #define BUILD_AS_NOMMU 0 |
| 102 | /* Enable/disable sanity checks. Ok to enable in production, |
| 103 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 104 | * Keeping 1 for now even in released versions. |
| 105 | */ |
| 106 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 107 | /* Slightly bigger (+200 bytes), but faster hush. |
| 108 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 109 | * which allows us to do fewer waitpid's. |
| 110 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 111 | * and therefore waitpid will return the same result as last time) |
| 112 | */ |
| 113 | #define ENABLE_HUSH_FAST 0 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 114 | |
| 115 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 116 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 117 | # undef BB_MMU |
| 118 | # undef USE_FOR_NOMMU |
| 119 | # undef USE_FOR_MMU |
| 120 | # define BB_MMU 0 |
| 121 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 122 | # define USE_FOR_MMU(...) |
| 123 | #endif |
| 124 | |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 125 | #if defined SINGLE_APPLET_MAIN |
| 126 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 127 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 128 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 129 | # undef IF_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 130 | # define IF_FEATURE_SH_STANDALONE(...) |
| 131 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denys Vlasenko | 673e945 | 2009-05-27 14:39:35 +0200 | [diff] [blame] | 132 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 133 | #endif |
| 134 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 135 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 136 | # undef ENABLE_FEATURE_EDITING |
| 137 | # define ENABLE_FEATURE_EDITING 0 |
| 138 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 139 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 140 | #endif |
| 141 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 142 | /* Do we support ANY keywords? */ |
| 143 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 144 | # define HAS_KEYWORDS 1 |
| 145 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 146 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 147 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 148 | # define HAS_KEYWORDS 0 |
| 149 | # define IF_HAS_KEYWORDS(...) |
| 150 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 151 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 152 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 153 | /* If you comment out one of these below, it will be #defined later |
| 154 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 155 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 156 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 157 | #define debug_printf_parse(...) do {} while (0) |
| 158 | #define debug_print_tree(a, b) do {} while (0) |
| 159 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 160 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 161 | #define debug_printf_jobs(...) do {} while (0) |
| 162 | #define debug_printf_expand(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 163 | #define debug_printf_glob(...) do {} while (0) |
| 164 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 165 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 166 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 167 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 168 | #define ERR_PTR ((void*)(long)1) |
| 169 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 170 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 171 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 172 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 173 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 174 | struct variable; |
| 175 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 176 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 177 | |
| 178 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 179 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 180 | */ |
| 181 | #if !BB_MMU |
| 182 | typedef struct nommu_save_t { |
| 183 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 184 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 185 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 186 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 187 | } nommu_save_t; |
| 188 | #endif |
| 189 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 190 | typedef enum reserved_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 191 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 192 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 193 | RES_IF , |
| 194 | RES_THEN , |
| 195 | RES_ELIF , |
| 196 | RES_ELSE , |
| 197 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 198 | #endif |
| 199 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 200 | RES_FOR , |
| 201 | RES_WHILE , |
| 202 | RES_UNTIL , |
| 203 | RES_DO , |
| 204 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 205 | #endif |
| 206 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 207 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 208 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 209 | #if ENABLE_HUSH_CASE |
| 210 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 211 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 212 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 213 | RES_MATCH , /* "word)" */ |
| 214 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 215 | RES_ESAC , |
| 216 | #endif |
| 217 | RES_XXXX , |
| 218 | RES_SNTX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 219 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 220 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 221 | typedef struct o_string { |
| 222 | char *data; |
| 223 | int length; /* position where data is appended */ |
| 224 | int maxlen; |
| 225 | /* Protect newly added chars against globbing |
| 226 | * (by prepending \ to *, ?, [, \) */ |
| 227 | smallint o_escape; |
| 228 | smallint o_glob; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 229 | /* At least some part of the string was inside '' or "", |
| 230 | * possibly empty one: word"", wo''rd etc. */ |
| 231 | smallint o_quoted; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 232 | smallint has_empty_slot; |
| 233 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 234 | } o_string; |
| 235 | enum { |
| 236 | MAYBE_ASSIGNMENT = 0, |
| 237 | DEFINITELY_ASSIGNMENT = 1, |
| 238 | NOT_ASSIGNMENT = 2, |
| 239 | WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */ |
| 240 | }; |
| 241 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 242 | #define NULL_O_STRING { NULL } |
| 243 | |
| 244 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
| 245 | * available? Where is it documented? */ |
| 246 | typedef struct in_str { |
| 247 | const char *p; |
| 248 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 249 | char eof_flag; /* meaningless if ->p == NULL */ |
| 250 | char peek_buf[2]; |
| 251 | #if ENABLE_HUSH_INTERACTIVE |
| 252 | smallint promptme; |
| 253 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 254 | #endif |
| 255 | FILE *file; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 256 | int (*get) (struct in_str *) FAST_FUNC; |
| 257 | int (*peek) (struct in_str *) FAST_FUNC; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 258 | } in_str; |
| 259 | #define i_getch(input) ((input)->get(input)) |
| 260 | #define i_peek(input) ((input)->peek(input)) |
| 261 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 262 | /* The descrip member of this structure is only used to make |
| 263 | * debugging output pretty */ |
| 264 | static const struct { |
| 265 | int mode; |
| 266 | signed char default_fd; |
| 267 | char descrip[3]; |
| 268 | } redir_table[] = { |
| 269 | { O_RDONLY, 0, "<" }, |
| 270 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 271 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 272 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 273 | { O_RDONLY, 0, "<<" }, |
| 274 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 275 | /* { O_RDONLY, 77, "<<" }, */ |
| 276 | }; |
| 277 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 278 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 279 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 280 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 281 | int rd_fd; /* fd to redirect */ |
| 282 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 283 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 284 | smallint rd_type; /* (enum redir_type) */ |
| 285 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 286 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 287 | * bit 0: do we need to trim leading tabs? |
| 288 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 289 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 290 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 291 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 292 | REDIRECT_INPUT = 0, |
| 293 | REDIRECT_OVERWRITE = 1, |
| 294 | REDIRECT_APPEND = 2, |
| 295 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 296 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 297 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 298 | |
| 299 | REDIRFD_CLOSE = -3, |
| 300 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 301 | REDIRFD_TO_FILE = -1, |
| 302 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 303 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 304 | HEREDOC_SKIPTABS = 1, |
| 305 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 306 | } redir_type; |
| 307 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 308 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 309 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 310 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 311 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 312 | smallint is_stopped; /* is the command currently running? */ |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 313 | smallint cmd_type; /* CMD_xxx */ |
| 314 | #define CMD_NORMAL 0 |
| 315 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 316 | |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 317 | /* used for "[[ EXPR ]]" */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 318 | #if ENABLE_HUSH_BASH_COMPAT |
| 319 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 320 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 321 | |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 322 | /* used for "export noglob=* glob* a=`echo a b`" */ |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 323 | //#define CMD_SINGLEWORD_NOGLOB_COND 3 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 324 | // It is hard to implement correctly, it adds significant amounts of tricky code, |
| 325 | // and all this is only useful for really obscure export statements |
| 326 | // almost nobody would use anyway. #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 327 | // guards the code which implements it, but I have doubts it works |
| 328 | // in all cases (especially with mixed globbed/non-globbed arguments) |
| 329 | |
| 330 | #if ENABLE_HUSH_FUNCTIONS |
| 331 | # define CMD_FUNCDEF 3 |
| 332 | #endif |
| 333 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 334 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 335 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 336 | #if !BB_MMU |
| 337 | char *group_as_string; |
| 338 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 339 | #if ENABLE_HUSH_FUNCTIONS |
| 340 | struct function *child_func; |
| 341 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 342 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 343 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 344 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 345 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 346 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 347 | * we put those fields back into cmd->xxx |
| 348 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 349 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 350 | * Without this trick, loop would execute a;b;b;b;... |
| 351 | * instead of correct sequence a;b;a;b;... |
| 352 | * When command is freed, it severs the link |
| 353 | * (sets ->child_func->parent_cmd to NULL). |
| 354 | */ |
| 355 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 356 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 357 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 358 | * and on execution these are substituted with their values. |
| 359 | * Substitution can make _several_ words out of one argv[n]! |
| 360 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 361 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 362 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 363 | struct redir_struct *redirects; /* I/O redirections */ |
| 364 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 365 | /* Is there anything in this command at all? */ |
| 366 | #define IS_NULL_CMD(cmd) \ |
| 367 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 368 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 369 | |
| 370 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 371 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 372 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 373 | int alive_cmds; /* number of commands running (not exited) */ |
| 374 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 375 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 376 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 377 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 378 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 379 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 380 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 381 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 382 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 383 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 384 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 385 | typedef enum pipe_style { |
| 386 | PIPE_SEQ = 1, |
| 387 | PIPE_AND = 2, |
| 388 | PIPE_OR = 3, |
| 389 | PIPE_BG = 4, |
| 390 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 391 | /* Is there anything in this pipe at all? */ |
| 392 | #define IS_NULL_PIPE(pi) \ |
| 393 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 394 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 395 | /* This holds pointers to the various results of parsing */ |
| 396 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 397 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 398 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 399 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 400 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 401 | /* last command in pipe (being constructed right now) */ |
| 402 | struct command *command; |
| 403 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 404 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 405 | #if !BB_MMU |
| 406 | o_string as_string; |
| 407 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 408 | #if HAS_KEYWORDS |
| 409 | smallint ctx_res_w; |
| 410 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 411 | #if ENABLE_HUSH_CASE |
| 412 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 413 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 414 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 415 | int old_flag; |
| 416 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 417 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 418 | * when we see "if" or "then", we malloc and copy current context, |
| 419 | * and make ->stack point to it. then we parse pipeN. |
| 420 | * when closing "then" / fi" / whatever is found, |
| 421 | * we move list_head into ->stack->command->group, |
| 422 | * copy ->stack into current context, and delete ->stack. |
| 423 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 424 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 425 | struct parse_context *stack; |
| 426 | #endif |
| 427 | }; |
| 428 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 429 | /* On program start, environ points to initial environment. |
| 430 | * putenv adds new pointers into it, unsetenv removes them. |
| 431 | * Neither of these (de)allocates the strings. |
| 432 | * setenv allocates new strings in malloc space and does putenv, |
| 433 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 434 | #define setenv(...) setenv_is_leaky_dont_use() |
| 435 | struct variable { |
| 436 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 437 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 438 | #if ENABLE_HUSH_LOCAL |
| 439 | unsigned func_nest_level; |
| 440 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 441 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 442 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 443 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 444 | }; |
| 445 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 446 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 447 | BC_BREAK = 1, |
| 448 | BC_CONTINUE = 2, |
| 449 | }; |
| 450 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 451 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 452 | struct function { |
| 453 | struct function *next; |
| 454 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 455 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 456 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 457 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 458 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 459 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 460 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 461 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 462 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 463 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 464 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 465 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 466 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 467 | /* interactive_fd != 0 means we are an interactive shell. |
| 468 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 469 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 470 | * job control still works, but terminal signals |
| 471 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 472 | * process groups can only be created with "cmd &". |
| 473 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 474 | * to give tty to the foreground process group, |
| 475 | * and will take it back when the group is stopped (^Z) |
| 476 | * or killed (^C). |
| 477 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 478 | #if ENABLE_HUSH_INTERACTIVE |
| 479 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 480 | * _AND_ if we decided to act interactively */ |
| 481 | int interactive_fd; |
| 482 | const char *PS1; |
| 483 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 484 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 485 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 486 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 487 | #endif |
| 488 | #if ENABLE_FEATURE_EDITING |
| 489 | line_input_t *line_input_state; |
| 490 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 491 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 492 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 493 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 494 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 495 | random_t random_gen; |
| 496 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 497 | #if ENABLE_HUSH_JOB |
| 498 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 499 | int last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 500 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 501 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 502 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 503 | #else |
| 504 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 505 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 506 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 507 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 508 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 509 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 510 | #if ENABLE_HUSH_FUNCTIONS |
| 511 | /* 0: outside of a function (or sourced file) |
| 512 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 513 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 514 | */ |
| 515 | smallint flag_return_in_progress; |
| 516 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 517 | smallint fake_mode; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 518 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 519 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 520 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 521 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 522 | smalluint global_args_malloced; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 523 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 524 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 525 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 526 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 527 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 528 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 529 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 530 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 531 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 532 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 533 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 534 | const char *cwd; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 535 | struct variable *top_var; /* = &G.shell_ver (set in main()) */ |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 536 | struct variable shell_ver; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 537 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 538 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 539 | # if ENABLE_HUSH_LOCAL |
| 540 | struct variable **shadowed_vars_pp; |
| 541 | unsigned func_nest_level; |
| 542 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 543 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 544 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 545 | #if ENABLE_HUSH_FAST |
| 546 | unsigned count_SIGCHLD; |
| 547 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 548 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 549 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 550 | /* which signals have non-DFL handler (even with no traps set)? */ |
| 551 | unsigned non_DFL_mask; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 552 | char **traps; /* char *traps[NSIG] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 553 | sigset_t blocked_set; |
| 554 | sigset_t inherited_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 555 | #if HUSH_DEBUG |
| 556 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 557 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 558 | #endif |
Denys Vlasenko | aaa22d2 | 2009-10-19 16:34:39 +0200 | [diff] [blame] | 559 | 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] | 560 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 561 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 562 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 563 | * (too many defines). Also, I actually prefer to see when a variable |
| 564 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 565 | #define INIT_G() do { \ |
| 566 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 567 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 568 | |
| 569 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 570 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 571 | static int builtin_cd(char **argv) FAST_FUNC; |
| 572 | static int builtin_echo(char **argv) FAST_FUNC; |
| 573 | static int builtin_eval(char **argv) FAST_FUNC; |
| 574 | static int builtin_exec(char **argv) FAST_FUNC; |
| 575 | static int builtin_exit(char **argv) FAST_FUNC; |
| 576 | static int builtin_export(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 577 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 578 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 579 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 580 | #endif |
| 581 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 582 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 583 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 584 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 585 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 586 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 587 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 588 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 589 | #endif |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 590 | #if ENABLE_PRINTF |
| 591 | static int builtin_printf(char **argv) FAST_FUNC; |
| 592 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 593 | static int builtin_pwd(char **argv) FAST_FUNC; |
| 594 | static int builtin_read(char **argv) FAST_FUNC; |
| 595 | static int builtin_set(char **argv) FAST_FUNC; |
| 596 | static int builtin_shift(char **argv) FAST_FUNC; |
| 597 | static int builtin_source(char **argv) FAST_FUNC; |
| 598 | static int builtin_test(char **argv) FAST_FUNC; |
| 599 | static int builtin_trap(char **argv) FAST_FUNC; |
| 600 | static int builtin_type(char **argv) FAST_FUNC; |
| 601 | static int builtin_true(char **argv) FAST_FUNC; |
| 602 | static int builtin_umask(char **argv) FAST_FUNC; |
| 603 | static int builtin_unset(char **argv) FAST_FUNC; |
| 604 | static int builtin_wait(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 605 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 606 | static int builtin_break(char **argv) FAST_FUNC; |
| 607 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 608 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 609 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 610 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 611 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 612 | |
| 613 | /* Table of built-in functions. They can be forked or not, depending on |
| 614 | * context: within pipes, they fork. As simple commands, they do not. |
| 615 | * When used in non-forking context, they can change global variables |
| 616 | * in the parent shell process. If forked, of course they cannot. |
| 617 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 618 | * still be set at the end. */ |
| 619 | struct built_in_command { |
| 620 | const char *cmd; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 621 | int (*function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 622 | #if ENABLE_HUSH_HELP |
| 623 | const char *descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 624 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 625 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 626 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 627 | #endif |
| 628 | }; |
| 629 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 630 | static const struct built_in_command bltins1[] = { |
| 631 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 632 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 633 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 634 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 635 | #endif |
| 636 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 637 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 638 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 639 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 640 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 641 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 642 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 643 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 644 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 645 | BLTIN("exit" , builtin_exit , "Exit"), |
| 646 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 647 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 648 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 649 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 650 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 651 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 652 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 653 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 654 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 655 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 656 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 657 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 658 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 659 | #if HUSH_DEBUG |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 660 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 661 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 662 | BLTIN("read" , builtin_read , "Input into variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 663 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 664 | BLTIN("return" , builtin_return , "Return from a function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 665 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 666 | BLTIN("set" , builtin_set , "Set/unset positional parameters"), |
| 667 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
| 668 | BLTIN("trap" , builtin_trap , "Trap signals"), |
| 669 | BLTIN("type" , builtin_type , "Write a description of command type"), |
| 670 | // BLTIN("ulimit" , builtin_ulimit , "Control resource limits"), |
| 671 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 672 | BLTIN("unset" , builtin_unset , "Unset variables"), |
| 673 | BLTIN("wait" , builtin_wait , "Wait for process"), |
| 674 | }; |
| 675 | /* For now, echo and test are unconditionally enabled. |
| 676 | * Maybe make it configurable? */ |
| 677 | static const struct built_in_command bltins2[] = { |
| 678 | BLTIN("[" , builtin_test , NULL), |
| 679 | BLTIN("echo" , builtin_echo , NULL), |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 680 | #if ENABLE_PRINTF |
| 681 | BLTIN("printf" , builtin_printf , NULL), |
| 682 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 683 | BLTIN("pwd" , builtin_pwd , NULL), |
| 684 | BLTIN("test" , builtin_test , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 685 | }; |
| 686 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 687 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 688 | /* Debug printouts. |
| 689 | */ |
| 690 | #if HUSH_DEBUG |
| 691 | /* prevent disasters with G.debug_indent < 0 */ |
| 692 | # define indent() fprintf(stderr, "%*s", (G.debug_indent * 2) & 0xff, "") |
| 693 | # define debug_enter() (G.debug_indent++) |
| 694 | # define debug_leave() (G.debug_indent--) |
| 695 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 696 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 697 | # define debug_enter() ((void)0) |
| 698 | # define debug_leave() ((void)0) |
| 699 | #endif |
| 700 | |
| 701 | #ifndef debug_printf |
| 702 | # define debug_printf(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 703 | #endif |
| 704 | |
| 705 | #ifndef debug_printf_parse |
| 706 | # define debug_printf_parse(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 707 | #endif |
| 708 | |
| 709 | #ifndef debug_printf_exec |
| 710 | #define debug_printf_exec(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 711 | #endif |
| 712 | |
| 713 | #ifndef debug_printf_env |
| 714 | # define debug_printf_env(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 715 | #endif |
| 716 | |
| 717 | #ifndef debug_printf_jobs |
| 718 | # define debug_printf_jobs(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 719 | # define DEBUG_JOBS 1 |
| 720 | #else |
| 721 | # define DEBUG_JOBS 0 |
| 722 | #endif |
| 723 | |
| 724 | #ifndef debug_printf_expand |
| 725 | # define debug_printf_expand(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 726 | # define DEBUG_EXPAND 1 |
| 727 | #else |
| 728 | # define DEBUG_EXPAND 0 |
| 729 | #endif |
| 730 | |
| 731 | #ifndef debug_printf_glob |
| 732 | # define debug_printf_glob(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 733 | # define DEBUG_GLOB 1 |
| 734 | #else |
| 735 | # define DEBUG_GLOB 0 |
| 736 | #endif |
| 737 | |
| 738 | #ifndef debug_printf_list |
| 739 | # define debug_printf_list(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 740 | #endif |
| 741 | |
| 742 | #ifndef debug_printf_subst |
| 743 | # define debug_printf_subst(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 744 | #endif |
| 745 | |
| 746 | #ifndef debug_printf_clean |
| 747 | # define debug_printf_clean(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 748 | # define DEBUG_CLEAN 1 |
| 749 | #else |
| 750 | # define DEBUG_CLEAN 0 |
| 751 | #endif |
| 752 | |
| 753 | #if DEBUG_EXPAND |
| 754 | static void debug_print_strings(const char *prefix, char **vv) |
| 755 | { |
| 756 | indent(); |
| 757 | fprintf(stderr, "%s:\n", prefix); |
| 758 | while (*vv) |
| 759 | fprintf(stderr, " '%s'\n", *vv++); |
| 760 | } |
| 761 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 762 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 763 | #endif |
| 764 | |
| 765 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 766 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 767 | */ |
| 768 | #if LEAK_HUNTING |
| 769 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 770 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 771 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 772 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 773 | return ptr; |
| 774 | } |
| 775 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 776 | { |
| 777 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 778 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 779 | return ptr; |
| 780 | } |
| 781 | static char *xxstrdup(int lineno, const char *str) |
| 782 | { |
| 783 | char *ptr = xstrdup(str); |
| 784 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 785 | return ptr; |
| 786 | } |
| 787 | static void xxfree(void *ptr) |
| 788 | { |
| 789 | fdprintf(2, "free %p\n", ptr); |
| 790 | free(ptr); |
| 791 | } |
| 792 | #define xmalloc(s) xxmalloc(__LINE__, s) |
| 793 | #define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 794 | #define xstrdup(s) xxstrdup(__LINE__, s) |
| 795 | #define free(p) xxfree(p) |
| 796 | #endif |
| 797 | |
| 798 | |
| 799 | /* Syntax and runtime errors. They always abort scripts. |
| 800 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 801 | * and return to the prompt. |
| 802 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 803 | */ |
| 804 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 805 | # define die_if_script(lineno, ...) die_if_script(__VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 806 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 807 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 808 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 809 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 810 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 811 | #endif |
| 812 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 813 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 814 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 815 | va_list p; |
| 816 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 817 | #if HUSH_DEBUG >= 2 |
| 818 | bb_error_msg("hush.c:%u", lineno); |
| 819 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 820 | va_start(p, fmt); |
| 821 | bb_verror_msg(fmt, p, NULL); |
| 822 | va_end(p); |
| 823 | if (!G_interactive_fd) |
| 824 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 825 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 826 | |
| 827 | static void syntax_error(unsigned lineno, const char *msg) |
| 828 | { |
| 829 | if (msg) |
| 830 | die_if_script(lineno, "syntax error: %s", msg); |
| 831 | else |
| 832 | die_if_script(lineno, "syntax error", NULL); |
| 833 | } |
| 834 | |
| 835 | static void syntax_error_at(unsigned lineno, const char *msg) |
| 836 | { |
| 837 | die_if_script(lineno, "syntax error at '%s'", msg); |
| 838 | } |
| 839 | |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 840 | static void syntax_error_unterm_str(unsigned lineno, const char *s) |
| 841 | { |
| 842 | die_if_script(lineno, "syntax error: unterminated %s", s); |
| 843 | } |
| 844 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 845 | /* It so happens that all such cases are totally fatal |
| 846 | * even if shell is interactive: EOF while looking for closing |
| 847 | * delimiter. There is nowhere to read stuff from after that, |
| 848 | * it's EOF! The only choice is to terminate. |
| 849 | */ |
| 850 | static void syntax_error_unterm_ch(unsigned lineno, char ch) NORETURN; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 851 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 852 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 853 | char msg[2] = { ch, '\0' }; |
| 854 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 855 | xfunc_die(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 858 | static void syntax_error_unexpected_ch(unsigned lineno, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 859 | { |
| 860 | char msg[2]; |
| 861 | msg[0] = ch; |
| 862 | msg[1] = '\0'; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 863 | die_if_script(lineno, "syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 866 | #if HUSH_DEBUG < 2 |
| 867 | # undef die_if_script |
| 868 | # undef syntax_error |
| 869 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 870 | # undef syntax_error_unterm_ch |
| 871 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 872 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 873 | #else |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 874 | # define die_if_script(...) die_if_script(__LINE__, __VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 875 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 876 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 877 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 878 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 879 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 880 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 881 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 882 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 883 | #if ENABLE_HUSH_INTERACTIVE |
| 884 | static void cmdedit_update_prompt(void); |
| 885 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 886 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 887 | #endif |
| 888 | |
| 889 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 890 | /* Utility functions |
| 891 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 892 | static int glob_needed(const char *s) |
| 893 | { |
| 894 | while (*s) { |
| 895 | if (*s == '\\') |
| 896 | s++; |
| 897 | if (*s == '*' || *s == '[' || *s == '?') |
| 898 | return 1; |
| 899 | s++; |
| 900 | } |
| 901 | return 0; |
| 902 | } |
| 903 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 904 | static int is_well_formed_var_name(const char *s, char terminator) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 905 | { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 906 | if (!s || !(isalpha(*s) || *s == '_')) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 907 | return 0; |
| 908 | s++; |
| 909 | while (isalnum(*s) || *s == '_') |
| 910 | s++; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 911 | return *s == terminator; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 914 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 915 | static char *unbackslash(char *src) |
| 916 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 917 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 918 | while (1) { |
| 919 | if (*src == '\\') |
| 920 | src++; |
| 921 | if ((*dst++ = *src++) == '\0') |
| 922 | break; |
| 923 | } |
| 924 | return dst; |
| 925 | } |
| 926 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 927 | 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] | 928 | { |
| 929 | int i; |
| 930 | unsigned count1; |
| 931 | unsigned count2; |
| 932 | char **v; |
| 933 | |
| 934 | v = strings; |
| 935 | count1 = 0; |
| 936 | if (v) { |
| 937 | while (*v) { |
| 938 | count1++; |
| 939 | v++; |
| 940 | } |
| 941 | } |
| 942 | count2 = 0; |
| 943 | v = add; |
| 944 | while (*v) { |
| 945 | count2++; |
| 946 | v++; |
| 947 | } |
| 948 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 949 | v[count1 + count2] = NULL; |
| 950 | i = count2; |
| 951 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 952 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 953 | return v; |
| 954 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 955 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 956 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 957 | { |
| 958 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 959 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 960 | return ptr; |
| 961 | } |
| 962 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 963 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 964 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 965 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 966 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 967 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 968 | { |
| 969 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 970 | v[0] = add; |
| 971 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 972 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 973 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 974 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 975 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 976 | { |
| 977 | char **ptr = add_string_to_strings(strings, add); |
| 978 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 979 | return ptr; |
| 980 | } |
| 981 | #define add_string_to_strings(strings, add) \ |
| 982 | xx_add_string_to_strings(__LINE__, strings, add) |
| 983 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 984 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 985 | static void free_strings(char **strings) |
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 | char **v; |
| 988 | |
| 989 | if (!strings) |
| 990 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 991 | v = strings; |
| 992 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 993 | free(*v); |
| 994 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 995 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 996 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 999 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1000 | /* Helpers for setting new $n and restoring them back |
| 1001 | */ |
| 1002 | typedef struct save_arg_t { |
| 1003 | char *sv_argv0; |
| 1004 | char **sv_g_argv; |
| 1005 | int sv_g_argc; |
| 1006 | smallint sv_g_malloced; |
| 1007 | } save_arg_t; |
| 1008 | |
| 1009 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1010 | { |
| 1011 | int n; |
| 1012 | |
| 1013 | sv->sv_argv0 = argv[0]; |
| 1014 | sv->sv_g_argv = G.global_argv; |
| 1015 | sv->sv_g_argc = G.global_argc; |
| 1016 | sv->sv_g_malloced = G.global_args_malloced; |
| 1017 | |
| 1018 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1019 | G.global_argv = argv; |
| 1020 | G.global_args_malloced = 0; |
| 1021 | |
| 1022 | n = 1; |
| 1023 | while (*++argv) |
| 1024 | n++; |
| 1025 | G.global_argc = n; |
| 1026 | } |
| 1027 | |
| 1028 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1029 | { |
| 1030 | char **pp; |
| 1031 | |
| 1032 | if (G.global_args_malloced) { |
| 1033 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 1034 | pp = G.global_argv; |
| 1035 | while (*++pp) /* note: does not free $0 */ |
| 1036 | free(*pp); |
| 1037 | free(G.global_argv); |
| 1038 | } |
| 1039 | argv[0] = sv->sv_argv0; |
| 1040 | G.global_argv = sv->sv_g_argv; |
| 1041 | G.global_argc = sv->sv_g_argc; |
| 1042 | G.global_args_malloced = sv->sv_g_malloced; |
| 1043 | } |
| 1044 | |
| 1045 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1046 | /* Basic theory of signal handling in shell |
| 1047 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1048 | * This does not describe what hush does, rather, it is current understanding |
| 1049 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1050 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1051 | * |
| 1052 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1053 | * is finished or backgrounded. It is the same in interactive and |
| 1054 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1055 | * 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] | 1056 | * ^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] | 1057 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1058 | * pipe. |
| 1059 | * |
| 1060 | * Wait builtin in interruptible by signals for which user trap is set |
| 1061 | * or by SIGINT in interactive shell. |
| 1062 | * |
| 1063 | * Trap handlers will execute even within trap handlers. (right?) |
| 1064 | * |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1065 | * User trap handlers are forgotten when subshell ("(cmd)") is entered. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1066 | * |
| 1067 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1068 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1069 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1070 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1071 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1072 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1073 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1074 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1075 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1076 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1077 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1078 | * |
| 1079 | * SIGQUIT: ignore |
| 1080 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1081 | * SIGHUP (interactive): |
| 1082 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1083 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1084 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1085 | * that all pipe members are stopped. Try this in bash: |
| 1086 | * while :; do :; done - ^Z does not background it |
| 1087 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1088 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1089 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1090 | * to interactive shell while shell is waiting for a pipe, |
| 1091 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1092 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1093 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1094 | * Example 2: this does not wait and does not execute ls: |
| 1095 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1096 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1097 | * "sleep 5; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1098 | * |
| 1099 | * (What happens to signals which are IGN on shell start?) |
| 1100 | * (What happens with signal mask on shell start?) |
| 1101 | * |
| 1102 | * Implementation in hush |
| 1103 | * ====================== |
| 1104 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1105 | * We block all signals which we don't want to take action immediately, |
| 1106 | * i.e. we block all signals which need to have special handling as described |
| 1107 | * above, and all signals which have traps set. |
| 1108 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1109 | * and act on them. |
| 1110 | * |
| 1111 | * unsigned non_DFL_mask: a mask of such "special" signals |
| 1112 | * sigset_t blocked_set: current blocked signal set |
| 1113 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1114 | * "trap - SIGxxx": |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1115 | * 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] | 1116 | * "trap 'cmd' SIGxxx": |
| 1117 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1118 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1119 | * unblock signals with special interactive handling |
| 1120 | * (child shell is not interactive), |
| 1121 | * unset all traps (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1122 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1123 | * 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] | 1124 | * 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] | 1125 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1126 | * 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] | 1127 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1128 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1129 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1130 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1131 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1132 | * are set to the default actions". bash interprets it so that traps which |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1133 | * 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] | 1134 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1135 | enum { |
| 1136 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1137 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1138 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1139 | | (1 << SIGHUP) |
| 1140 | , |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1141 | SPECIAL_JOB_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1142 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1143 | | (1 << SIGTTIN) |
| 1144 | | (1 << SIGTTOU) |
| 1145 | | (1 << SIGTSTP) |
| 1146 | #endif |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1147 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1148 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1149 | #if ENABLE_HUSH_FAST |
| 1150 | static void SIGCHLD_handler(int sig UNUSED_PARAM) |
| 1151 | { |
| 1152 | G.count_SIGCHLD++; |
| 1153 | //bb_error_msg("[%d] SIGCHLD_handler: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1154 | } |
| 1155 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1156 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1157 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1158 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1159 | /* After [v]fork, in child: do not restore tty pgrp on xfunc death */ |
| 1160 | #define disable_restore_tty_pgrp_on_exit() (die_sleep = 0) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1161 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1162 | #define enable_restore_tty_pgrp_on_exit() (die_sleep = -1) |
| 1163 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1164 | /* Restores tty foreground process group, and exits. |
| 1165 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1166 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1167 | * or called directly with -EXITCODE. |
| 1168 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1169 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1170 | static void sigexit(int sig) |
| 1171 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1172 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 1173 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1174 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1175 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1176 | * tty pgrp then, only top-level shell process does that */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1177 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 1178 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1179 | |
| 1180 | /* Not a signal, just exit */ |
| 1181 | if (sig <= 0) |
| 1182 | _exit(- sig); |
| 1183 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1184 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1185 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1186 | #else |
| 1187 | |
| 1188 | #define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1189 | #define enable_restore_tty_pgrp_on_exit() ((void)0) |
| 1190 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1191 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1192 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1193 | /* Restores tty foreground process group, and exits. */ |
| 1194 | static void hush_exit(int exitcode) NORETURN; |
| 1195 | static void hush_exit(int exitcode) |
| 1196 | { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1197 | if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) { |
| 1198 | /* Prevent recursion: |
| 1199 | * trap "echo Hi; exit" EXIT; exit |
| 1200 | */ |
| 1201 | char *argv[] = { NULL, G.traps[0], NULL }; |
| 1202 | G.traps[0] = NULL; |
| 1203 | G.exiting = 1; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1204 | builtin_eval(argv); |
| 1205 | free(argv[1]); |
| 1206 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1207 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1208 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1209 | fflush_all(); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1210 | sigexit(- (exitcode & 0xff)); |
| 1211 | #else |
| 1212 | exit(exitcode); |
| 1213 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1216 | static int check_and_run_traps(int sig) |
| 1217 | { |
| 1218 | static const struct timespec zero_timespec = { 0, 0 }; |
| 1219 | smalluint save_rcode; |
| 1220 | int last_sig = 0; |
| 1221 | |
| 1222 | if (sig) |
| 1223 | goto jump_in; |
| 1224 | while (1) { |
| 1225 | sig = sigtimedwait(&G.blocked_set, NULL, &zero_timespec); |
| 1226 | if (sig <= 0) |
| 1227 | break; |
| 1228 | jump_in: |
| 1229 | last_sig = sig; |
| 1230 | if (G.traps && G.traps[sig]) { |
| 1231 | if (G.traps[sig][0]) { |
| 1232 | /* We have user-defined handler */ |
| 1233 | char *argv[] = { NULL, xstrdup(G.traps[sig]), NULL }; |
| 1234 | save_rcode = G.last_exitcode; |
| 1235 | builtin_eval(argv); |
| 1236 | free(argv[1]); |
| 1237 | G.last_exitcode = save_rcode; |
| 1238 | } /* else: "" trap, ignoring signal */ |
| 1239 | continue; |
| 1240 | } |
| 1241 | /* not a trap: special action */ |
| 1242 | switch (sig) { |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1243 | #if ENABLE_HUSH_FAST |
| 1244 | case SIGCHLD: |
| 1245 | G.count_SIGCHLD++; |
| 1246 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1247 | break; |
| 1248 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1249 | case SIGINT: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1250 | /* Builtin was ^C'ed, make it look prettier: */ |
| 1251 | bb_putchar('\n'); |
| 1252 | G.flag_SIGINT = 1; |
| 1253 | break; |
| 1254 | #if ENABLE_HUSH_JOB |
| 1255 | case SIGHUP: { |
| 1256 | struct pipe *job; |
| 1257 | /* bash is observed to signal whole process groups, |
| 1258 | * not individual processes */ |
| 1259 | for (job = G.job_list; job; job = job->next) { |
| 1260 | if (job->pgrp <= 0) |
| 1261 | continue; |
| 1262 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 1263 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 1264 | kill(- job->pgrp, SIGCONT); |
| 1265 | } |
| 1266 | sigexit(SIGHUP); |
| 1267 | } |
| 1268 | #endif |
| 1269 | default: /* ignored: */ |
| 1270 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
| 1271 | break; |
| 1272 | } |
| 1273 | } |
| 1274 | return last_sig; |
| 1275 | } |
| 1276 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1277 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1278 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1279 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1280 | if (force || G.cwd == NULL) { |
| 1281 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1282 | * we must not try to free(bb_msg_unknown) */ |
| 1283 | if (G.cwd == bb_msg_unknown) |
| 1284 | G.cwd = NULL; |
| 1285 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1286 | if (!G.cwd) |
| 1287 | G.cwd = bb_msg_unknown; |
| 1288 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1289 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1292 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1293 | /* |
| 1294 | * Shell and environment variable support |
| 1295 | */ |
| 1296 | static struct variable **get_ptr_to_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1297 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1298 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1299 | struct variable *cur; |
| 1300 | int len; |
| 1301 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1302 | len = strlen(name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1303 | pp = &G.top_var; |
| 1304 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1305 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1306 | return pp; |
| 1307 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1308 | } |
| 1309 | return NULL; |
| 1310 | } |
| 1311 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1312 | static struct variable *get_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1313 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1314 | struct variable **pp = get_ptr_to_local_var(name); |
| 1315 | if (pp) |
| 1316 | return *pp; |
| 1317 | return NULL; |
| 1318 | } |
| 1319 | |
| 1320 | static const char *get_local_var_value(const char *name) |
| 1321 | { |
| 1322 | struct variable **pp = get_ptr_to_local_var(name); |
| 1323 | if (pp) |
| 1324 | return strchr((*pp)->varstr, '=') + 1; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 1325 | if (strcmp(name, "PPID") == 0) |
| 1326 | return utoa(G.root_ppid); |
| 1327 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1328 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 8c66a9d | 2009-10-11 02:15:49 +0200 | [diff] [blame] | 1329 | if (strcmp(name, "RANDOM") == 0) { |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1330 | return utoa(next_random(&G.random_gen)); |
Denys Vlasenko | 8c66a9d | 2009-10-11 02:15:49 +0200 | [diff] [blame] | 1331 | } |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1332 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1333 | return NULL; |
| 1334 | } |
| 1335 | |
| 1336 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1337 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1338 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1339 | * 0: do not change export flag |
| 1340 | * (if creating new variable, flag will be 0) |
| 1341 | * 1: set export flag and putenv the variable |
| 1342 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1343 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1344 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1345 | #if !BB_MMU && ENABLE_HUSH_LOCAL |
| 1346 | /* all params are used */ |
| 1347 | #elif BB_MMU && ENABLE_HUSH_LOCAL |
| 1348 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1349 | set_local_var(str, flg_export, local_lvl) |
| 1350 | #elif BB_MMU && !ENABLE_HUSH_LOCAL |
| 1351 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1352 | set_local_var(str, flg_export) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1353 | #elif !BB_MMU && !ENABLE_HUSH_LOCAL |
| 1354 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1355 | set_local_var(str, flg_export, flg_read_only) |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1356 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1357 | 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] | 1358 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1359 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1360 | struct variable *cur; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1361 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1362 | int name_len; |
| 1363 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1364 | eq_sign = strchr(str, '='); |
| 1365 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1366 | free(str); |
| 1367 | return -1; |
| 1368 | } |
| 1369 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1370 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1371 | var_pp = &G.top_var; |
| 1372 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1373 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1374 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1375 | continue; |
| 1376 | } |
| 1377 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1378 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1379 | #if !BB_MMU |
| 1380 | if (!flg_read_only) |
| 1381 | #endif |
| 1382 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1383 | free(str); |
| 1384 | return -1; |
| 1385 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1386 | if (flg_export == -1) { // "&& cur->flg_export" ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1387 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1388 | *eq_sign = '\0'; |
| 1389 | unsetenv(str); |
| 1390 | *eq_sign = '='; |
| 1391 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1392 | #if ENABLE_HUSH_LOCAL |
| 1393 | if (cur->func_nest_level < local_lvl) { |
| 1394 | /* New variable is declared as local, |
| 1395 | * and existing one is global, or local |
| 1396 | * from enclosing function. |
| 1397 | * Remove and save old one: */ |
| 1398 | *var_pp = cur->next; |
| 1399 | cur->next = *G.shadowed_vars_pp; |
| 1400 | *G.shadowed_vars_pp = cur; |
| 1401 | /* bash 3.2.33(1) and exported vars: |
| 1402 | * # export z=z |
| 1403 | * # f() { local z=a; env | grep ^z; } |
| 1404 | * # f |
| 1405 | * z=a |
| 1406 | * # env | grep ^z |
| 1407 | * z=z |
| 1408 | */ |
| 1409 | if (cur->flg_export) |
| 1410 | flg_export = 1; |
| 1411 | break; |
| 1412 | } |
| 1413 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1414 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1415 | free_and_exp: |
| 1416 | free(str); |
| 1417 | goto exp; |
| 1418 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1419 | if (cur->max_len != 0) { |
| 1420 | if (cur->max_len >= strlen(str)) { |
| 1421 | /* This one is from startup env, reuse space */ |
| 1422 | strcpy(cur->varstr, str); |
| 1423 | goto free_and_exp; |
| 1424 | } |
| 1425 | } else { |
| 1426 | /* max_len == 0 signifies "malloced" var, which we can |
| 1427 | * (and has to) free */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1428 | free(cur->varstr); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1429 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1430 | cur->max_len = 0; |
| 1431 | goto set_str_and_exp; |
| 1432 | } |
| 1433 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1434 | /* Not found - create new variable struct */ |
| 1435 | cur = xzalloc(sizeof(*cur)); |
| 1436 | #if ENABLE_HUSH_LOCAL |
| 1437 | cur->func_nest_level = local_lvl; |
| 1438 | #endif |
| 1439 | cur->next = *var_pp; |
| 1440 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1441 | |
| 1442 | set_str_and_exp: |
| 1443 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1444 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1445 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1446 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1447 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1448 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1449 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1450 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1451 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1452 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1453 | if (flg_export == -1) { |
| 1454 | cur->flg_export = 0; |
| 1455 | /* unsetenv was already done */ |
| 1456 | } else { |
| 1457 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1458 | return putenv(cur->varstr); |
| 1459 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1460 | } |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1464 | /* Used at startup and after each cd */ |
| 1465 | static void set_pwd_var(int exp) |
| 1466 | { |
| 1467 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), |
| 1468 | /*exp:*/ exp, /*lvl:*/ 0, /*ro:*/ 0); |
| 1469 | } |
| 1470 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1471 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1472 | { |
| 1473 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1474 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1475 | |
| 1476 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1477 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1478 | var_pp = &G.top_var; |
| 1479 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1480 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1481 | if (cur->flg_read_only) { |
| 1482 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1483 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1484 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1485 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1486 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1487 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1488 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1489 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1490 | if (!cur->max_len) |
| 1491 | free(cur->varstr); |
| 1492 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1493 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1494 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1495 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1496 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1497 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1500 | static int unset_local_var(const char *name) |
| 1501 | { |
| 1502 | return unset_local_var_len(name, strlen(name)); |
| 1503 | } |
| 1504 | |
| 1505 | static void unset_vars(char **strings) |
| 1506 | { |
| 1507 | char **v; |
| 1508 | |
| 1509 | if (!strings) |
| 1510 | return; |
| 1511 | v = strings; |
| 1512 | while (*v) { |
| 1513 | const char *eq = strchrnul(*v, '='); |
| 1514 | unset_local_var_len(*v, (int)(eq - *v)); |
| 1515 | v++; |
| 1516 | } |
| 1517 | free(strings); |
| 1518 | } |
| 1519 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1520 | #if ENABLE_SH_MATH_SUPPORT |
| 1521 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1522 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 1523 | static char *endofname(const char *name) |
| 1524 | { |
| 1525 | char *p; |
| 1526 | |
| 1527 | p = (char *) name; |
| 1528 | if (!is_name(*p)) |
| 1529 | return p; |
| 1530 | while (*++p) { |
| 1531 | if (!is_in_name(*p)) |
| 1532 | break; |
| 1533 | } |
| 1534 | return p; |
| 1535 | } |
| 1536 | |
| 1537 | static void arith_set_local_var(const char *name, const char *val, int flags) |
| 1538 | { |
| 1539 | /* arith code doesnt malloc space, so do it for it */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1540 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1541 | set_local_var(var, flags, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1542 | } |
| 1543 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1544 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1545 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1546 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1547 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 1548 | */ |
| 1549 | static void add_vars(struct variable *var) |
| 1550 | { |
| 1551 | struct variable *next; |
| 1552 | |
| 1553 | while (var) { |
| 1554 | next = var->next; |
| 1555 | var->next = G.top_var; |
| 1556 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1557 | if (var->flg_export) { |
| 1558 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1559 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1560 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1561 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1562 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1563 | var = next; |
| 1564 | } |
| 1565 | } |
| 1566 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1567 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1568 | { |
| 1569 | char **s; |
| 1570 | struct variable *old = NULL; |
| 1571 | |
| 1572 | if (!strings) |
| 1573 | return old; |
| 1574 | s = strings; |
| 1575 | while (*s) { |
| 1576 | struct variable *var_p; |
| 1577 | struct variable **var_pp; |
| 1578 | char *eq; |
| 1579 | |
| 1580 | eq = strchr(*s, '='); |
| 1581 | if (eq) { |
| 1582 | *eq = '\0'; |
| 1583 | var_pp = get_ptr_to_local_var(*s); |
| 1584 | *eq = '='; |
| 1585 | if (var_pp) { |
| 1586 | /* Remove variable from global linked list */ |
| 1587 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1588 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1589 | *var_pp = var_p->next; |
| 1590 | /* Add it to returned list */ |
| 1591 | var_p->next = old; |
| 1592 | old = var_p; |
| 1593 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1594 | set_local_var(*s, /*exp:*/ 1, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1595 | } |
| 1596 | s++; |
| 1597 | } |
| 1598 | return old; |
| 1599 | } |
| 1600 | |
| 1601 | |
| 1602 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1603 | * in_str support |
| 1604 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1605 | static int FAST_FUNC static_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1606 | { |
| 1607 | int ch = *i->p++; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1608 | if (ch != '\0') |
| 1609 | return ch; |
| 1610 | i->p--; |
| 1611 | return EOF; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1614 | static int FAST_FUNC static_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1615 | { |
| 1616 | return *i->p; |
| 1617 | } |
| 1618 | |
| 1619 | #if ENABLE_HUSH_INTERACTIVE |
| 1620 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1621 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1622 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1623 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1624 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1625 | if (G.PS1 == NULL) |
| 1626 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1627 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1628 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1629 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1630 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1631 | if (G.PS2 == NULL) |
| 1632 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1633 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1634 | |
| 1635 | static const char* setup_prompt_string(int promptmode) |
| 1636 | { |
| 1637 | const char *prompt_str; |
| 1638 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1639 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1640 | /* Set up the prompt */ |
| 1641 | if (promptmode == 0) { /* PS1 */ |
| 1642 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1643 | /* bash uses $PWD value, even if it is set by user. |
| 1644 | * It uses current dir only if PWD is unset. |
| 1645 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1646 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1647 | prompt_str = G.PS1; |
| 1648 | } else |
| 1649 | prompt_str = G.PS2; |
| 1650 | } else |
| 1651 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1652 | debug_printf("result '%s'\n", prompt_str); |
| 1653 | return prompt_str; |
| 1654 | } |
| 1655 | |
| 1656 | static void get_user_input(struct in_str *i) |
| 1657 | { |
| 1658 | int r; |
| 1659 | const char *prompt_str; |
| 1660 | |
| 1661 | prompt_str = setup_prompt_string(i->promptmode); |
| 1662 | #if ENABLE_FEATURE_EDITING |
| 1663 | /* Enable command line editing only while a command line |
| 1664 | * is actually being read */ |
| 1665 | do { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1666 | G.flag_SIGINT = 0; |
| 1667 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 1668 | * only after <Enter>. (^C will work) */ |
Denys Vlasenko | aaa22d2 | 2009-10-19 16:34:39 +0200 | [diff] [blame] | 1669 | 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] | 1670 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1671 | check_and_run_traps(0); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1672 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1673 | i->eof_flag = (r < 0); |
| 1674 | if (i->eof_flag) { /* EOF/error detected */ |
| 1675 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1676 | G.user_input_buf[1] = '\0'; |
| 1677 | } |
| 1678 | #else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1679 | do { |
| 1680 | G.flag_SIGINT = 0; |
| 1681 | fputs(prompt_str, stdout); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1682 | fflush_all(); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1683 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1684 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1685 | //do we need check_and_run_traps(0)? (maybe only if stdin) |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1686 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1687 | i->eof_flag = (r == EOF); |
| 1688 | #endif |
| 1689 | i->p = G.user_input_buf; |
| 1690 | } |
| 1691 | |
| 1692 | #endif /* INTERACTIVE */ |
| 1693 | |
| 1694 | /* This is the magic location that prints prompts |
| 1695 | * and gets data back from the user */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1696 | static int FAST_FUNC file_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1697 | { |
| 1698 | int ch; |
| 1699 | |
| 1700 | /* If there is data waiting, eat it up */ |
| 1701 | if (i->p && *i->p) { |
| 1702 | #if ENABLE_HUSH_INTERACTIVE |
| 1703 | take_cached: |
| 1704 | #endif |
| 1705 | ch = *i->p++; |
| 1706 | if (i->eof_flag && !*i->p) |
| 1707 | ch = EOF; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1708 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1709 | } else { |
| 1710 | /* need to double check i->file because we might be doing something |
| 1711 | * more complicated by now, like sourcing or substituting. */ |
| 1712 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 1713 | if (G_interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1714 | do { |
| 1715 | get_user_input(i); |
| 1716 | } while (!*i->p); /* need non-empty line */ |
| 1717 | i->promptmode = 1; /* PS2 */ |
| 1718 | i->promptme = 0; |
| 1719 | goto take_cached; |
| 1720 | } |
| 1721 | #endif |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1722 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1723 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1724 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1725 | #if ENABLE_HUSH_INTERACTIVE |
| 1726 | if (ch == '\n') |
| 1727 | i->promptme = 1; |
| 1728 | #endif |
| 1729 | return ch; |
| 1730 | } |
| 1731 | |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1732 | /* All callers guarantee this routine will never |
| 1733 | * be used right after a newline, so prompting is not needed. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1734 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1735 | static int FAST_FUNC file_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1736 | { |
| 1737 | int ch; |
| 1738 | if (i->p && *i->p) { |
| 1739 | if (i->eof_flag && !i->p[1]) |
| 1740 | return EOF; |
| 1741 | return *i->p; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1742 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1743 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1744 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1745 | i->eof_flag = (ch == EOF); |
| 1746 | i->peek_buf[0] = ch; |
| 1747 | i->peek_buf[1] = '\0'; |
| 1748 | i->p = i->peek_buf; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1749 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1750 | return ch; |
| 1751 | } |
| 1752 | |
| 1753 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1754 | { |
| 1755 | i->peek = file_peek; |
| 1756 | i->get = file_get; |
| 1757 | #if ENABLE_HUSH_INTERACTIVE |
| 1758 | i->promptme = 1; |
| 1759 | i->promptmode = 0; /* PS1 */ |
| 1760 | #endif |
| 1761 | i->file = f; |
| 1762 | i->p = NULL; |
| 1763 | } |
| 1764 | |
| 1765 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1766 | { |
| 1767 | i->peek = static_peek; |
| 1768 | i->get = static_get; |
| 1769 | #if ENABLE_HUSH_INTERACTIVE |
| 1770 | i->promptme = 1; |
| 1771 | i->promptmode = 0; /* PS1 */ |
| 1772 | #endif |
| 1773 | i->p = s; |
| 1774 | i->eof_flag = 0; |
| 1775 | } |
| 1776 | |
| 1777 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1778 | /* |
| 1779 | * o_string support |
| 1780 | */ |
| 1781 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1782 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1783 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1784 | { |
| 1785 | o->length = 0; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 1786 | o->o_quoted = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1787 | if (o->data) |
| 1788 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1791 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1792 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1793 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1794 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1797 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 1798 | { |
| 1799 | free(o->data); |
| 1800 | } |
| 1801 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1802 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1803 | { |
| 1804 | if (o->length + len > o->maxlen) { |
| 1805 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 1806 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 1807 | } |
| 1808 | } |
| 1809 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1810 | static void o_addchr(o_string *o, int ch) |
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 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 1813 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1814 | o->data[o->length] = ch; |
| 1815 | o->length++; |
| 1816 | o->data[o->length] = '\0'; |
| 1817 | } |
| 1818 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1819 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1820 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1821 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1822 | memcpy(&o->data[o->length], str, len); |
| 1823 | o->length += len; |
| 1824 | o->data[o->length] = '\0'; |
| 1825 | } |
| 1826 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1827 | #if !BB_MMU |
| 1828 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1829 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1830 | o_addblock(o, str, strlen(str)); |
| 1831 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1832 | static void nommu_addchr(o_string *o, int ch) |
| 1833 | { |
| 1834 | if (o) |
| 1835 | o_addchr(o, ch); |
| 1836 | } |
| 1837 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1838 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1839 | #endif |
| 1840 | |
| 1841 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 1842 | { |
| 1843 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1846 | 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] | 1847 | { |
| 1848 | while (len) { |
| 1849 | o_addchr(o, *str); |
| 1850 | if (*str++ == '\\' |
| 1851 | && (*str != '*' && *str != '?' && *str != '[') |
| 1852 | ) { |
| 1853 | o_addchr(o, '\\'); |
| 1854 | } |
| 1855 | len--; |
| 1856 | } |
| 1857 | } |
| 1858 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1859 | /* My analysis of quoting semantics tells me that state information |
| 1860 | * is associated with a destination, not a source. |
| 1861 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1862 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1863 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1864 | int sz = 1; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1865 | char *found = strchr("*?[\\", ch); |
| 1866 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1867 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1868 | o_grow_by(o, sz); |
| 1869 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1870 | o->data[o->length] = '\\'; |
| 1871 | o->length++; |
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 | o->data[o->length] = ch; |
| 1874 | o->length++; |
| 1875 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1878 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1879 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1880 | int sz = 1; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1881 | if (o->o_escape && strchr("*?[\\", ch)) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1882 | sz++; |
| 1883 | o->data[o->length] = '\\'; |
| 1884 | o->length++; |
| 1885 | } |
| 1886 | o_grow_by(o, sz); |
| 1887 | o->data[o->length] = ch; |
| 1888 | o->length++; |
| 1889 | o->data[o->length] = '\0'; |
| 1890 | } |
| 1891 | |
| 1892 | static void o_addQstr(o_string *o, const char *str, int len) |
| 1893 | { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1894 | if (!o->o_escape) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1895 | o_addblock(o, str, len); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1896 | return; |
| 1897 | } |
| 1898 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1899 | char ch; |
| 1900 | int sz; |
| 1901 | int ordinary_cnt = strcspn(str, "*?[\\"); |
| 1902 | if (ordinary_cnt > len) /* paranoia */ |
| 1903 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1904 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1905 | if (ordinary_cnt == len) |
| 1906 | return; |
| 1907 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1908 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1909 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1910 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1911 | sz = 1; |
| 1912 | if (ch) { /* it is necessarily one of "*?[\\" */ |
| 1913 | sz++; |
| 1914 | o->data[o->length] = '\\'; |
| 1915 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1916 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1917 | o_grow_by(o, sz); |
| 1918 | o->data[o->length] = ch; |
| 1919 | o->length++; |
| 1920 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1924 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1925 | * 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] | 1926 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1927 | * list[i] contains an INDEX (int!) into this string data. |
| 1928 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1929 | * but list[i]'s need not be modified. |
| 1930 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1931 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1932 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1933 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1934 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1935 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1936 | { |
| 1937 | char **list = (char**)o->data; |
| 1938 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1939 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1940 | |
| 1941 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1942 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1943 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1944 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1945 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1946 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1947 | o->data + (int)list[i] + string_start, |
| 1948 | o->data + (int)list[i] + string_start); |
| 1949 | i++; |
| 1950 | } |
| 1951 | if (n) { |
| 1952 | const char *p = o->data + (int)list[n - 1] + string_start; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1953 | indent(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1954 | 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] | 1955 | } |
| 1956 | } |
| 1957 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1958 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1959 | #endif |
| 1960 | |
| 1961 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1962 | * in list[n] so that it points past last stored byte so far. |
| 1963 | * It returns n+1. */ |
| 1964 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1965 | { |
| 1966 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1967 | int string_start; |
| 1968 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1969 | |
| 1970 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1971 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1972 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1973 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1974 | 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] | 1975 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1976 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1977 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 1978 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1979 | memmove(list + n + 0x10, list + n, string_len); |
| 1980 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1981 | } else { |
| 1982 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 1983 | n, string_len, string_start); |
| 1984 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1985 | } else { |
| 1986 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1987 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1988 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1989 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 1990 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1991 | o->has_empty_slot = 0; |
| 1992 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1993 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1994 | return n + 1; |
| 1995 | } |
| 1996 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1997 | /* "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] | 1998 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1999 | { |
| 2000 | char **list = (char**)o->data; |
| 2001 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2002 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 2003 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2004 | } |
| 2005 | |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame^] | 2006 | #undef HUSH_BRACE_EXP |
| 2007 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 2008 | * first, it processes even {a} (no commas), second, |
| 2009 | * I didn't manage to make it return strings when they don't match |
| 2010 | # existing files. Need to re-implement it. |
| 2011 | * |
| 2012 | * This code needs corresponding quoting on variable expansion side. |
| 2013 | * Currently, "a='{q,w}'; echo $a" erroneously expands braces in $a |
| 2014 | */ |
| 2015 | #ifdef HUSH_BRACE_EXP |
| 2016 | /* Return pointer to next closing brace or to comma */ |
| 2017 | static const char *next_brace_sub(const char *cp) |
| 2018 | { |
| 2019 | unsigned depth = 0; |
| 2020 | cp++; |
| 2021 | while (*cp != '\0') { |
| 2022 | if (*cp == '\\') { |
| 2023 | if (*++cp == '\0') |
| 2024 | break; |
| 2025 | cp++; |
| 2026 | continue; |
| 2027 | } |
| 2028 | /*{*/ if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
| 2029 | break; |
| 2030 | if (*cp++ == '{') /*}*/ |
| 2031 | depth++; |
| 2032 | } |
| 2033 | |
| 2034 | return *cp != '\0' ? cp : NULL; |
| 2035 | } |
| 2036 | static int glob_brace(const char *pattern, int flags, glob_t *pglob) |
| 2037 | { |
| 2038 | const char *begin; |
| 2039 | char *alt_start; |
| 2040 | const char *p; |
| 2041 | const char *next; |
| 2042 | const char *rest; |
| 2043 | size_t rest_len; |
| 2044 | char *onealt; |
| 2045 | |
| 2046 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 2047 | |
| 2048 | begin = pattern; |
| 2049 | while (1) { |
| 2050 | if (*begin == '\0') |
| 2051 | goto do_glob; |
| 2052 | if (*begin == '{') /*}*/ { |
| 2053 | /* Find the first sub-pattern and at the same time |
| 2054 | * find the rest after the closing brace */ |
| 2055 | next = next_brace_sub(begin); |
| 2056 | if (next == NULL) { |
| 2057 | /* An illegal expression */ |
| 2058 | goto do_glob; |
| 2059 | } |
| 2060 | /*{*/ if (*next == '}') { |
| 2061 | /* "{abc}" with no commas - illegal |
| 2062 | * brace expr, disregard and skip it */ |
| 2063 | begin = next + 1; |
| 2064 | continue; |
| 2065 | } |
| 2066 | break; |
| 2067 | } |
| 2068 | if (*begin == '\\' && begin[1] != '\0') |
| 2069 | begin++; |
| 2070 | begin++; |
| 2071 | } |
| 2072 | debug_printf_glob("begin:%s\n", begin); |
| 2073 | debug_printf_glob("next:%s\n", next); |
| 2074 | |
| 2075 | /* Now find the end of the whole brace expression */ |
| 2076 | rest = next; |
| 2077 | /*{*/ while (*rest != '}') { |
| 2078 | rest = next_brace_sub(rest); |
| 2079 | if (rest == NULL) { |
| 2080 | /* An illegal expression */ |
| 2081 | goto do_glob; |
| 2082 | } |
| 2083 | debug_printf_glob("rest:%s\n", rest); |
| 2084 | } |
| 2085 | rest_len = strlen(++rest) + 1; |
| 2086 | |
| 2087 | /* We are sure the brace expression is well-formed */ |
| 2088 | |
| 2089 | /* Allocate working buffer large enough for our work */ |
| 2090 | onealt = alloca(strlen(pattern)); |
| 2091 | /* We know the prefix for all sub-patterns */ |
| 2092 | alt_start = mempcpy(onealt, pattern, begin - pattern); |
| 2093 | |
| 2094 | /* We have a brace expression. BEGIN points to the opening {, |
| 2095 | * NEXT points past the terminator of the first element, and REST |
| 2096 | * points past the final }. We will accumulate result names from |
| 2097 | * recursive runs for each brace alternative in the buffer using |
| 2098 | * GLOB_APPEND. */ |
| 2099 | |
| 2100 | p = begin + 1; |
| 2101 | while (1) { |
| 2102 | int result; |
| 2103 | |
| 2104 | /* Construct the new glob expression */ |
| 2105 | memcpy(mempcpy(alt_start, p, next - p), rest, rest_len); |
| 2106 | |
| 2107 | result = glob_brace(onealt, flags, pglob); |
| 2108 | /* If we got an error, return it */ |
| 2109 | if (result && result != GLOB_NOMATCH) |
| 2110 | return result; |
| 2111 | |
| 2112 | flags |= GLOB_APPEND; |
| 2113 | |
| 2114 | /*{*/ if (*next == '}') { |
| 2115 | /* We saw the last entry */ |
| 2116 | break; |
| 2117 | } |
| 2118 | p = next + 1; |
| 2119 | next = next_brace_sub(next); |
| 2120 | } |
| 2121 | |
| 2122 | /* We found some entries */ |
| 2123 | return 0; |
| 2124 | |
| 2125 | do_glob: |
| 2126 | return glob(pattern, flags, NULL, pglob); |
| 2127 | } |
| 2128 | #endif |
| 2129 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2130 | /* o_glob performs globbing on last list[], saving each result |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2131 | * as a new list[]. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2132 | static int o_glob(o_string *o, int n) |
| 2133 | { |
| 2134 | glob_t globdata; |
| 2135 | int gr; |
| 2136 | char *pattern; |
| 2137 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2138 | 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] | 2139 | if (!o->data) |
| 2140 | return o_save_ptr_helper(o, n); |
| 2141 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2142 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2143 | if (!glob_needed(pattern)) { |
| 2144 | literal: |
| 2145 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2146 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2147 | return o_save_ptr_helper(o, n); |
| 2148 | } |
| 2149 | |
| 2150 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame^] | 2151 | #ifdef HUSH_BRACE_EXP |
| 2152 | gr = glob_brace(pattern, GLOB_NOCHECK, &globdata); |
| 2153 | debug_printf_glob("glob_brace('%s'):%d\n", pattern, gr); |
| 2154 | #else |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2155 | gr = glob(pattern, 0, NULL, &globdata); |
| 2156 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame^] | 2157 | #endif |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2158 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame^] | 2159 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2160 | if (gr == GLOB_NOMATCH) { |
| 2161 | globfree(&globdata); |
| 2162 | goto literal; |
| 2163 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame^] | 2164 | if (gr != 0) { |
| 2165 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2166 | * but we didn't specify it. Paranoia again. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2167 | bb_error_msg("glob(3) error %d on '%s'", gr, pattern); |
| 2168 | } |
| 2169 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2170 | char **argv = globdata.gl_pathv; |
| 2171 | o->length = pattern - o->data; /* "forget" pattern */ |
| 2172 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2173 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2174 | n = o_save_ptr_helper(o, n); |
| 2175 | argv++; |
| 2176 | if (!*argv) |
| 2177 | break; |
| 2178 | } |
| 2179 | } |
| 2180 | globfree(&globdata); |
| 2181 | if (DEBUG_GLOB) |
| 2182 | debug_print_list("o_glob returning", o, n); |
| 2183 | return n; |
| 2184 | } |
| 2185 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2186 | /* If o->o_glob == 1, glob the string so far remembered. |
| 2187 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2188 | static int o_save_ptr(o_string *o, int n) |
| 2189 | { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2190 | if (o->o_glob) { /* if globbing is requested */ |
| 2191 | /* If o->has_empty_slot, list[n] was already globbed |
| 2192 | * (if it was requested back then when it was filled) |
| 2193 | * so don't do that again! */ |
| 2194 | if (!o->has_empty_slot) |
| 2195 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 2196 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2197 | return o_save_ptr_helper(o, n); |
| 2198 | } |
| 2199 | |
| 2200 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2201 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2202 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2203 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2204 | int string_start; |
| 2205 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2206 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 2207 | if (DEBUG_EXPAND) |
| 2208 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2209 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2210 | list = (char**)o->data; |
| 2211 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2212 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2213 | while (n) { |
| 2214 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 2215 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2216 | } |
| 2217 | return list; |
| 2218 | } |
| 2219 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2220 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2221 | /* Expansion can recurse */ |
| 2222 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2223 | static int process_command_subs(o_string *dest, const char *s); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2224 | #endif |
| 2225 | static char *expand_string_to_string(const char *str); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2226 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2227 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2228 | parse_stream_dquoted(dest, input, dquote_end) |
| 2229 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2230 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2231 | o_string *dest, |
| 2232 | struct in_str *input, |
| 2233 | int dquote_end); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2234 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2235 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 2236 | * all variable references within and returns a pointer to |
| 2237 | * a list of expanded strings, possibly with larger number |
| 2238 | * of strings. (Think VAR="a b"; echo $VAR). |
| 2239 | * This new list is allocated as a single malloc block. |
| 2240 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 2241 | * followed by strings themself. |
| 2242 | * Caller can deallocate entire list by single free(list). */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2243 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2244 | /* Store given string, finalizing the word and starting new one whenever |
| 2245 | * we encounter IFS char(s). This is used for expanding variable values. |
| 2246 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 2247 | 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] | 2248 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2249 | while (1) { |
| 2250 | int word_len = strcspn(str, G.ifs); |
| 2251 | if (word_len) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2252 | if (output->o_escape || !output->o_glob) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2253 | o_addQstr(output, str, word_len); |
| 2254 | else /* protect backslashes against globbing up :) */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2255 | o_addblock_duplicate_backslash(output, str, word_len); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2256 | str += word_len; |
| 2257 | } |
| 2258 | if (!*str) /* EOL - do not finalize word */ |
| 2259 | break; |
| 2260 | o_addchr(output, '\0'); |
| 2261 | debug_print_list("expand_on_ifs", output, n); |
| 2262 | n = o_save_ptr(output, n); |
| 2263 | str += strspn(str, G.ifs); /* skip ifs chars */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2264 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2265 | debug_print_list("expand_on_ifs[1]", output, n); |
| 2266 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2269 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 2270 | * they are in double quotes, with the exception that they are not :). |
| 2271 | * Just the rules are similar: "expand only $var and `cmd`" |
| 2272 | * |
| 2273 | * Returns malloced string. |
| 2274 | * As an optimization, we return NULL if expansion is not needed. |
| 2275 | */ |
| 2276 | static char *expand_pseudo_dquoted(const char *str) |
| 2277 | { |
| 2278 | char *exp_str; |
| 2279 | struct in_str input; |
| 2280 | o_string dest = NULL_O_STRING; |
| 2281 | |
| 2282 | if (strchr(str, '$') == NULL |
| 2283 | #if ENABLE_HUSH_TICK |
| 2284 | && strchr(str, '`') == NULL |
| 2285 | #endif |
| 2286 | ) { |
| 2287 | return NULL; |
| 2288 | } |
| 2289 | |
| 2290 | /* We need to expand. Example: |
| 2291 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 2292 | */ |
| 2293 | setup_string_in_str(&input, str); |
| 2294 | parse_stream_dquoted(NULL, &dest, &input, EOF); |
| 2295 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
| 2296 | exp_str = expand_string_to_string(dest.data); |
| 2297 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 2298 | o_free_unsafe(&dest); |
| 2299 | return exp_str; |
| 2300 | } |
| 2301 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2302 | /* Expand all variable references in given string, adding words to list[] |
| 2303 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 2304 | * to be filled). This routine is extremely tricky: has to deal with |
| 2305 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 2306 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | a7bb3c1 | 2009-10-08 12:28:08 +0200 | [diff] [blame] | 2307 | 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] | 2308 | { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 2309 | /* or_mask is either 0 (normal case) or 0x80 - |
| 2310 | * expansion of right-hand side of assignment == 1-element expand. |
| 2311 | * It will also do no globbing, and thus we must not backslash-quote! |
| 2312 | */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2313 | char ored_ch; |
| 2314 | char *p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2315 | |
| 2316 | ored_ch = 0; |
| 2317 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 2318 | 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] | 2319 | debug_print_list("expand_vars_to_list", output, n); |
| 2320 | n = o_save_ptr(output, n); |
| 2321 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 2322 | |
| 2323 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2324 | char first_ch; |
| 2325 | int i; |
| 2326 | char *dyn_val = NULL; |
| 2327 | const char *val = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2328 | #if ENABLE_HUSH_TICK |
| 2329 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2330 | #endif |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2331 | #if ENABLE_SH_MATH_SUPPORT |
| 2332 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 2333 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2334 | o_addblock(output, arg, p - arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2335 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 2336 | arg = ++p; |
| 2337 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 2338 | |
| 2339 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
| 2340 | /* "$@" is special. Even if quoted, it can still |
| 2341 | * expand to nothing (not even an empty string) */ |
| 2342 | if ((first_ch & 0x7f) != '@') |
| 2343 | ored_ch |= first_ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2344 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2345 | switch (first_ch & 0x7f) { |
| 2346 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 2347 | case '$': /* pid */ |
| 2348 | val = utoa(G.root_pid); |
| 2349 | break; |
| 2350 | case '!': /* bg pid */ |
| 2351 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
| 2352 | break; |
| 2353 | case '?': /* exitcode */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 2354 | val = utoa(G.last_exitcode); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2355 | break; |
| 2356 | case '#': /* argc */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2357 | if (arg[1] != SPECIAL_VAR_SYMBOL) |
| 2358 | /* actually, it's a ${#var} */ |
| 2359 | goto case_default; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2360 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 2361 | break; |
| 2362 | case '*': |
| 2363 | case '@': |
| 2364 | i = 1; |
| 2365 | if (!G.global_argv[i]) |
| 2366 | break; |
| 2367 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
| 2368 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2369 | smallint sv = output->o_escape; |
| 2370 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2371 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2372 | while (G.global_argv[i]) { |
| 2373 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 2374 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 2375 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 2376 | /* this argv[] is not empty and not last: |
| 2377 | * put terminating NUL, start new word */ |
| 2378 | o_addchr(output, '\0'); |
| 2379 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 2380 | n = o_save_ptr(output, n); |
| 2381 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 2382 | } |
| 2383 | } |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2384 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2385 | } else |
| 2386 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
| 2387 | * and in this case should treat it like '$*' - see 'else...' below */ |
| 2388 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
| 2389 | while (1) { |
| 2390 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2391 | if (++i >= G.global_argc) |
| 2392 | break; |
| 2393 | o_addchr(output, '\0'); |
| 2394 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 2395 | n = o_save_ptr(output, n); |
| 2396 | } |
| 2397 | } else { /* quoted $*: add as one word */ |
| 2398 | while (1) { |
| 2399 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2400 | if (!G.global_argv[++i]) |
| 2401 | break; |
| 2402 | if (G.ifs[0]) |
| 2403 | o_addchr(output, G.ifs[0]); |
| 2404 | } |
| 2405 | } |
| 2406 | break; |
| 2407 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 2408 | /* "Empty variable", used to make "" etc to not disappear */ |
| 2409 | arg++; |
| 2410 | ored_ch = 0x80; |
| 2411 | break; |
| 2412 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2413 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2414 | *p = '\0'; |
| 2415 | arg++; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2416 | /* Can't just stuff it into output o_string, |
| 2417 | * expanded result may need to be globbed |
| 2418 | * and $IFS-splitted */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2419 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 2420 | G.last_exitcode = process_command_subs(&subst_result, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2421 | debug_printf_subst("SUBST RES '%s'\n", subst_result.data); |
| 2422 | val = subst_result.data; |
| 2423 | goto store_val; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2424 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2425 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2426 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2427 | arith_eval_hooks_t hooks; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2428 | arith_t res; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2429 | int errcode; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2430 | char *exp_str; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2431 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2432 | arg++; /* skip '+' */ |
| 2433 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2434 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2435 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2436 | exp_str = expand_pseudo_dquoted(arg); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 2437 | hooks.lookupvar = get_local_var_value; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2438 | hooks.setvar = arith_set_local_var; |
| 2439 | hooks.endofname = endofname; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2440 | res = arith(exp_str ? exp_str : arg, &errcode, &hooks); |
| 2441 | free(exp_str); |
| 2442 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2443 | if (errcode < 0) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2444 | const char *msg = "error in arithmetic"; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2445 | switch (errcode) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2446 | case -3: |
| 2447 | msg = "exponent less than 0"; |
| 2448 | break; |
| 2449 | case -2: |
| 2450 | msg = "divide by 0"; |
| 2451 | break; |
| 2452 | case -5: |
| 2453 | msg = "expression recursion loop detected"; |
| 2454 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2455 | } |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2456 | die_if_script(msg); |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2457 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2458 | debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2459 | sprintf(arith_buf, arith_t_fmt, res); |
| 2460 | val = arith_buf; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2461 | break; |
| 2462 | } |
| 2463 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2464 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2465 | case_default: { |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2466 | bool exp_len = false; |
| 2467 | bool exp_null = false; |
| 2468 | char *var = arg; |
| 2469 | char exp_save = exp_save; /* for compiler */ |
| 2470 | char exp_op = exp_op; /* for compiler */ |
| 2471 | char *exp_word = exp_word; /* for compiler */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2472 | size_t exp_off = 0; |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2473 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2474 | *p = '\0'; |
| 2475 | arg[0] = first_ch & 0x7f; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2476 | |
| 2477 | /* prepare for expansions */ |
| 2478 | if (var[0] == '#') { |
| 2479 | /* handle length expansion ${#var} */ |
| 2480 | exp_len = true; |
| 2481 | ++var; |
| 2482 | } else { |
| 2483 | /* maybe handle parameter expansion */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2484 | exp_off = strcspn(var, ":-=+?%#"); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2485 | if (!var[exp_off]) |
| 2486 | exp_off = 0; |
| 2487 | if (exp_off) { |
| 2488 | exp_save = var[exp_off]; |
| 2489 | exp_null = exp_save == ':'; |
| 2490 | exp_word = var + exp_off; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2491 | if (exp_null) |
| 2492 | ++exp_word; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2493 | exp_op = *exp_word++; |
| 2494 | var[exp_off] = '\0'; |
| 2495 | } |
| 2496 | } |
| 2497 | |
| 2498 | /* lookup the variable in question */ |
| 2499 | if (isdigit(var[0])) { |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 2500 | /* handle_dollar() should have vetted var for us */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2501 | i = xatoi_u(var); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2502 | if (i < G.global_argc) |
| 2503 | val = G.global_argv[i]; |
| 2504 | /* else val remains NULL: $N with too big N */ |
| 2505 | } else |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 2506 | val = get_local_var_value(var); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2507 | |
| 2508 | /* handle any expansions */ |
| 2509 | if (exp_len) { |
| 2510 | debug_printf_expand("expand: length of '%s' = ", val); |
| 2511 | val = utoa(val ? strlen(val) : 0); |
| 2512 | debug_printf_expand("%s\n", val); |
| 2513 | } else if (exp_off) { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2514 | if (exp_op == '%' || exp_op == '#') { |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2515 | if (val) { |
| 2516 | /* we need to do a pattern match */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2517 | bool match_at_left; |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2518 | char *loc; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2519 | scan_t scan = pick_scan(exp_op, *exp_word, &match_at_left); |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2520 | if (exp_op == *exp_word) /* ## or %% */ |
| 2521 | ++exp_word; |
| 2522 | val = dyn_val = xstrdup(val); |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2523 | loc = scan(dyn_val, exp_word, match_at_left); |
| 2524 | if (match_at_left) /* # or ## */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2525 | val = loc; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2526 | else if (loc) /* % or %% and match was found */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2527 | *loc = '\0'; |
| 2528 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2529 | } else { |
| 2530 | /* we need to do an expansion */ |
| 2531 | int exp_test = (!val || (exp_null && !val[0])); |
| 2532 | if (exp_op == '+') |
| 2533 | exp_test = !exp_test; |
| 2534 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 2535 | exp_null ? "true" : "false", exp_test); |
| 2536 | if (exp_test) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2537 | if (exp_op == '?') { |
| 2538 | //TODO: how interactive bash aborts expansion mid-command? |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2539 | /* ${var?[error_msg_if_unset]} */ |
| 2540 | /* ${var:?[error_msg_if_unset_or_null]} */ |
| 2541 | /* mimic bash message */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2542 | die_if_script("%s: %s", |
| 2543 | var, |
| 2544 | exp_word[0] ? exp_word : "parameter null or not set" |
| 2545 | ); |
| 2546 | } else { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2547 | val = exp_word; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2548 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2549 | |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2550 | if (exp_op == '=') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2551 | /* ${var=[word]} or ${var:=[word]} */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2552 | if (isdigit(var[0]) || var[0] == '#') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2553 | /* mimic bash message */ |
| 2554 | die_if_script("$%s: cannot assign in this way", var); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2555 | val = NULL; |
| 2556 | } else { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2557 | char *new_var = xasprintf("%s=%s", var, val); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2558 | set_local_var(new_var, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2559 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2560 | } |
| 2561 | } |
| 2562 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2563 | |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2564 | var[exp_off] = exp_save; |
| 2565 | } |
| 2566 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2567 | arg[0] = first_ch; |
| 2568 | #if ENABLE_HUSH_TICK |
| 2569 | store_val: |
| 2570 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2571 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2572 | 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] | 2573 | if (val) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2574 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2575 | smallint sv = output->o_escape; |
| 2576 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2577 | n = expand_on_ifs(output, n, val); |
| 2578 | val = NULL; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2579 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2580 | } |
| 2581 | } else { /* quoted $VAR, val will be appended below */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2582 | 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] | 2583 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2584 | } /* default: */ |
| 2585 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2586 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2587 | if (val) { |
| 2588 | o_addQstr(output, val, strlen(val)); |
| 2589 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2590 | free(dyn_val); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2591 | /* Do the check to avoid writing to a const string */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2592 | if (*p != SPECIAL_VAR_SYMBOL) |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2593 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2594 | |
| 2595 | #if ENABLE_HUSH_TICK |
| 2596 | o_free(&subst_result); |
| 2597 | #endif |
| 2598 | arg = ++p; |
| 2599 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 2600 | |
| 2601 | if (arg[0]) { |
| 2602 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 2603 | /* this part is literal, and it was already pre-quoted |
| 2604 | * if needed (much earlier), do not use o_addQstr here! */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2605 | o_addstr_with_NUL(output, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2606 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 2607 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 2608 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 2609 | ) { |
| 2610 | n--; |
| 2611 | /* allow to reuse list[n] later without re-growth */ |
| 2612 | output->has_empty_slot = 1; |
| 2613 | } else { |
| 2614 | o_addchr(output, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2615 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2616 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2619 | static char **expand_variables(char **argv, int or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2620 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2621 | int n; |
| 2622 | char **list; |
| 2623 | char **v; |
| 2624 | o_string output = NULL_O_STRING; |
| 2625 | |
| 2626 | if (or_mask & 0x100) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2627 | output.o_escape = 1; /* protect against globbing for "$var" */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2628 | /* (unquoted $var will temporarily switch it off) */ |
| 2629 | output.o_glob = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2630 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2631 | |
| 2632 | n = 0; |
| 2633 | v = argv; |
| 2634 | while (*v) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2635 | n = expand_vars_to_list(&output, n, *v, (unsigned char)or_mask); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2636 | v++; |
| 2637 | } |
| 2638 | debug_print_list("expand_variables", &output, n); |
| 2639 | |
| 2640 | /* output.data (malloced in one block) gets returned in "list" */ |
| 2641 | list = o_finalize_list(&output, n); |
| 2642 | debug_print_strings("expand_variables[1]", list); |
| 2643 | return list; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2644 | } |
| 2645 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2646 | static char **expand_strvec_to_strvec(char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2647 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2648 | return expand_variables(argv, 0x100); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 2651 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2652 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 2653 | { |
| 2654 | return expand_variables(argv, 0x80); |
| 2655 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 2656 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2657 | |
| 2658 | #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 2659 | static char **expand_strvec_to_strvec_singleword_noglob_cond(char **argv) |
| 2660 | { |
| 2661 | int n; |
| 2662 | char **list; |
| 2663 | char **v; |
| 2664 | o_string output = NULL_O_STRING; |
| 2665 | |
| 2666 | n = 0; |
| 2667 | v = argv; |
| 2668 | while (*v) { |
| 2669 | int is_var = is_well_formed_var_name(*v, '='); |
| 2670 | /* is_var * 0x80: singleword expansion for vars */ |
| 2671 | n = expand_vars_to_list(&output, n, *v, is_var * 0x80); |
| 2672 | |
| 2673 | /* Subtle! expand_vars_to_list did not glob last word yet. |
| 2674 | * It does this only when fed with further data. |
| 2675 | * Therefore we set globbing flags AFTER it, not before: |
| 2676 | */ |
| 2677 | |
| 2678 | /* if it is not recognizably abc=...; then: */ |
| 2679 | output.o_escape = !is_var; /* protect against globbing for "$var" */ |
| 2680 | /* (unquoted $var will temporarily switch it off) */ |
| 2681 | output.o_glob = !is_var; /* and indeed do globbing */ |
| 2682 | v++; |
| 2683 | } |
| 2684 | debug_print_list("expand_cond", &output, n); |
| 2685 | |
| 2686 | /* output.data (malloced in one block) gets returned in "list" */ |
| 2687 | list = o_finalize_list(&output, n); |
| 2688 | debug_print_strings("expand_cond[1]", list); |
| 2689 | return list; |
| 2690 | } |
| 2691 | #endif |
| 2692 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2693 | /* Used for expansion of right hand of assignments */ |
| 2694 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 2695 | * "v=/bin/c*" */ |
| 2696 | static char *expand_string_to_string(const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2697 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2698 | char *argv[2], **list; |
| 2699 | |
| 2700 | argv[0] = (char*)str; |
| 2701 | argv[1] = NULL; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2702 | list = expand_variables(argv, 0x80); /* 0x80: singleword expansion */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2703 | if (HUSH_DEBUG) |
| 2704 | if (!list[0] || list[1]) |
| 2705 | bb_error_msg_and_die("BUG in varexp2"); |
| 2706 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 2707 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 2708 | unbackslash((char*)list); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2709 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 2710 | return (char*)list; |
| 2711 | } |
| 2712 | |
| 2713 | /* Used for "eval" builtin */ |
| 2714 | static char* expand_strvec_to_string(char **argv) |
| 2715 | { |
| 2716 | char **list; |
| 2717 | |
| 2718 | list = expand_variables(argv, 0x80); |
| 2719 | /* Convert all NULs to spaces */ |
| 2720 | if (list[0]) { |
| 2721 | int n = 1; |
| 2722 | while (list[n]) { |
| 2723 | if (HUSH_DEBUG) |
| 2724 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 2725 | bb_error_msg_and_die("BUG in varexp3"); |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 2726 | /* bash uses ' ' regardless of $IFS contents */ |
| 2727 | list[n][-1] = ' '; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2728 | n++; |
| 2729 | } |
| 2730 | } |
| 2731 | overlapping_strcpy((char*)list, list[0]); |
| 2732 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 2733 | return (char*)list; |
| 2734 | } |
| 2735 | |
| 2736 | static char **expand_assignments(char **argv, int count) |
| 2737 | { |
| 2738 | int i; |
| 2739 | char **p = NULL; |
| 2740 | /* Expand assignments into one string each */ |
| 2741 | for (i = 0; i < count; i++) { |
| 2742 | p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
| 2743 | } |
| 2744 | return p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2747 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2748 | #if BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2749 | /* never called */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2750 | void re_execute_shell(char ***to_free, const char *s, |
| 2751 | char *g_argv0, char **g_argv, |
| 2752 | char **builtin_argv) NORETURN; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2753 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2754 | static void reset_traps_to_defaults(void) |
| 2755 | { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2756 | /* This function is always called in a child shell |
| 2757 | * after fork (not vfork, NOMMU doesn't use this function). |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2758 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2759 | unsigned sig; |
| 2760 | unsigned mask; |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2761 | |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2762 | /* Child shells are not interactive. |
| 2763 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 2764 | * Testcase: (while :; do :; done) + ^Z should background. |
| 2765 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 2766 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2767 | if (!G.traps && !(G.non_DFL_mask & SPECIAL_INTERACTIVE_SIGS)) |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2768 | return; /* already no traps and no SPECIAL_INTERACTIVE_SIGS */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2769 | |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2770 | /* Switching off SPECIAL_INTERACTIVE_SIGS. |
| 2771 | * Stupid. It can be done with *single* &= op, but we can't use |
| 2772 | * the fact that G.blocked_set is implemented as a bitmask |
| 2773 | * in libc... */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2774 | mask = (SPECIAL_INTERACTIVE_SIGS >> 1); |
| 2775 | sig = 1; |
| 2776 | while (1) { |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2777 | if (mask & 1) { |
| 2778 | /* Careful. Only if no trap or trap is not "" */ |
| 2779 | if (!G.traps || !G.traps[sig] || G.traps[sig][0]) |
| 2780 | sigdelset(&G.blocked_set, sig); |
| 2781 | } |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2782 | mask >>= 1; |
| 2783 | if (!mask) |
| 2784 | break; |
| 2785 | sig++; |
| 2786 | } |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2787 | /* Our homegrown sig mask is saner to work with :) */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2788 | G.non_DFL_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2789 | |
| 2790 | /* Resetting all traps to default except empty ones */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2791 | mask = G.non_DFL_mask; |
| 2792 | if (G.traps) for (sig = 0; sig < NSIG; sig++, mask >>= 1) { |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2793 | if (!G.traps[sig] || !G.traps[sig][0]) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2794 | continue; |
| 2795 | free(G.traps[sig]); |
| 2796 | G.traps[sig] = NULL; |
| 2797 | /* There is no signal for 0 (EXIT) */ |
| 2798 | if (sig == 0) |
| 2799 | continue; |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2800 | /* There was a trap handler, we just removed it. |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2801 | * But if sig still has non-DFL handling, |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 2802 | * we should not unblock the sig. */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2803 | if (mask & 1) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2804 | continue; |
| 2805 | sigdelset(&G.blocked_set, sig); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2806 | } |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2807 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | #else /* !BB_MMU */ |
| 2811 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2812 | static void re_execute_shell(char ***to_free, const char *s, |
| 2813 | char *g_argv0, char **g_argv, |
| 2814 | char **builtin_argv) NORETURN; |
| 2815 | static void re_execute_shell(char ***to_free, const char *s, |
| 2816 | char *g_argv0, char **g_argv, |
| 2817 | char **builtin_argv) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2818 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2819 | char param_buf[sizeof("-$%x:%x:%x:%x:%x") + sizeof(unsigned) * 2]; |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2820 | char *heredoc_argv[4]; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2821 | struct variable *cur; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2822 | # if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2823 | struct function *funcp; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2824 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2825 | char **argv, **pp; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2826 | unsigned cnt; |
| 2827 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2828 | if (!g_argv0) { /* heredoc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2829 | argv = heredoc_argv; |
| 2830 | argv[0] = (char *) G.argv0_for_re_execing; |
| 2831 | argv[1] = (char *) "-<"; |
| 2832 | argv[2] = (char *) s; |
| 2833 | argv[3] = NULL; |
Denis Vlasenko | f50caac | 2009-04-09 01:40:15 +0000 | [diff] [blame] | 2834 | pp = &argv[3]; /* used as pointer to empty environment */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2835 | goto do_exec; |
| 2836 | } |
| 2837 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2838 | cnt = 0; |
| 2839 | pp = builtin_argv; |
| 2840 | if (pp) while (*pp++) |
| 2841 | cnt++; |
| 2842 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 2843 | sprintf(param_buf, "-$%x:%x:%x:%x:%x" IF_HUSH_LOOPS(":%x") |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2844 | , (unsigned) G.root_pid |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 2845 | , (unsigned) G.root_ppid |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2846 | , (unsigned) G.last_bg_pid |
| 2847 | , (unsigned) G.last_exitcode |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2848 | , cnt |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 2849 | IF_HUSH_LOOPS(, G.depth_of_loop) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2850 | ); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2851 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<depth> <vars...> <funcs...> |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2852 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2853 | */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2854 | cnt += 6; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2855 | for (cur = G.top_var; cur; cur = cur->next) { |
| 2856 | if (!cur->flg_export || cur->flg_read_only) |
| 2857 | cnt += 2; |
| 2858 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2859 | # if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2860 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 2861 | cnt += 3; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2862 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2863 | pp = g_argv; |
| 2864 | while (*pp++) |
| 2865 | cnt++; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2866 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2867 | *pp++ = (char *) G.argv0_for_re_execing; |
| 2868 | *pp++ = param_buf; |
| 2869 | for (cur = G.top_var; cur; cur = cur->next) { |
| 2870 | if (cur->varstr == hush_version_str) |
| 2871 | continue; |
| 2872 | if (cur->flg_read_only) { |
| 2873 | *pp++ = (char *) "-R"; |
| 2874 | *pp++ = cur->varstr; |
| 2875 | } else if (!cur->flg_export) { |
| 2876 | *pp++ = (char *) "-V"; |
| 2877 | *pp++ = cur->varstr; |
| 2878 | } |
| 2879 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2880 | # if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2881 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 2882 | *pp++ = (char *) "-F"; |
| 2883 | *pp++ = funcp->name; |
| 2884 | *pp++ = funcp->body_as_string; |
| 2885 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2886 | # endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2887 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 2888 | * |
| 2889 | * However, POSIX says that subshells reset signals with traps |
| 2890 | * to SIG_DFL. |
| 2891 | * I tested bash-3.2 and it not only does that with true subshells |
| 2892 | * of the form ( list ), but with any forked children shells. |
| 2893 | * I set trap "echo W" WINCH; and then tried: |
| 2894 | * |
| 2895 | * { echo 1; sleep 20; echo 2; } & |
| 2896 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 2897 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 2898 | * |
| 2899 | * In all these cases sending SIGWINCH to the child shell |
| 2900 | * did not run the trap. If I add trap "echo V" WINCH; |
| 2901 | * _inside_ group (just before echo 1), it works. |
| 2902 | * |
| 2903 | * I conclude it means we don't need to pass active traps here. |
| 2904 | * exec syscall below resets them to SIG_DFL for us. |
| 2905 | */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2906 | *pp++ = (char *) "-c"; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2907 | *pp++ = (char *) s; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2908 | if (builtin_argv) { |
| 2909 | while (*++builtin_argv) |
| 2910 | *pp++ = *builtin_argv; |
| 2911 | *pp++ = (char *) ""; |
| 2912 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2913 | *pp++ = g_argv0; |
| 2914 | while (*g_argv) |
| 2915 | *pp++ = *g_argv++; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2916 | /* *pp = NULL; - is already there */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2917 | pp = environ; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2918 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2919 | do_exec: |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2920 | debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s); |
| 2921 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2922 | execve(bb_busybox_exec_path, argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2923 | /* Fallback. Useful for init=/bin/hush usage etc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2924 | if (argv[0][0] == '/') |
| 2925 | execve(argv[0], argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2926 | xfunc_error_retval = 127; |
| 2927 | bb_error_msg_and_die("can't re-execute the shell"); |
| 2928 | } |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2929 | #endif /* !BB_MMU */ |
| 2930 | |
| 2931 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2932 | static void setup_heredoc(struct redir_struct *redir) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2933 | { |
| 2934 | struct fd_pair pair; |
| 2935 | pid_t pid; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2936 | int len, written; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2937 | /* the _body_ of heredoc (misleading field name) */ |
| 2938 | const char *heredoc = redir->rd_filename; |
| 2939 | char *expanded; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2940 | #if !BB_MMU |
| 2941 | char **to_free; |
| 2942 | #endif |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2943 | |
| 2944 | expanded = NULL; |
| 2945 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
| 2946 | expanded = expand_pseudo_dquoted(heredoc); |
| 2947 | if (expanded) |
| 2948 | heredoc = expanded; |
| 2949 | } |
| 2950 | len = strlen(heredoc); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2951 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2952 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2953 | xpiped_pair(pair); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2954 | xmove_fd(pair.rd, redir->rd_fd); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2955 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2956 | /* Try writing without forking. Newer kernels have |
| 2957 | * dynamically growing pipes. Must use non-blocking write! */ |
| 2958 | ndelay_on(pair.wr); |
| 2959 | while (1) { |
| 2960 | written = write(pair.wr, heredoc, len); |
| 2961 | if (written <= 0) |
| 2962 | break; |
| 2963 | len -= written; |
| 2964 | if (len == 0) { |
| 2965 | close(pair.wr); |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 2966 | free(expanded); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2967 | return; |
| 2968 | } |
| 2969 | heredoc += written; |
| 2970 | } |
| 2971 | ndelay_off(pair.wr); |
| 2972 | |
| 2973 | /* Okay, pipe buffer was not big enough */ |
| 2974 | /* Note: we must not create a stray child (bastard? :) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2975 | * for the unsuspecting parent process. Child creates a grandchild |
| 2976 | * and exits before parent execs the process which consumes heredoc |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2977 | * (that exec happens after we return from this function) */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 2978 | #if !BB_MMU |
| 2979 | to_free = NULL; |
| 2980 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2981 | pid = vfork(); |
| 2982 | if (pid < 0) |
| 2983 | bb_perror_msg_and_die("vfork"); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2984 | if (pid == 0) { |
| 2985 | /* child */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 2986 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2987 | pid = BB_MMU ? fork() : vfork(); |
| 2988 | if (pid < 0) |
| 2989 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
| 2990 | if (pid != 0) |
| 2991 | _exit(0); |
| 2992 | /* grandchild */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2993 | close(redir->rd_fd); /* read side of the pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2994 | #if BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2995 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2996 | _exit(0); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2997 | #else |
| 2998 | /* Delegate blocking writes to another process */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2999 | xmove_fd(pair.wr, STDOUT_FILENO); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3000 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3001 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3002 | } |
| 3003 | /* parent */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3004 | #if ENABLE_HUSH_FAST |
| 3005 | G.count_SIGCHLD++; |
| 3006 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 3007 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3008 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3009 | #if !BB_MMU |
| 3010 | free(to_free); |
| 3011 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3012 | close(pair.wr); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3013 | free(expanded); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3014 | wait(NULL); /* wait till child has died */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3017 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 3018 | * and stderr if they are redirected. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3019 | static int setup_redirects(struct command *prog, int squirrel[]) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3020 | { |
| 3021 | int openfd, mode; |
| 3022 | struct redir_struct *redir; |
| 3023 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3024 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3025 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3026 | /* rd_fd<<HERE case */ |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 3027 | if (squirrel && redir->rd_fd < 3 |
| 3028 | && squirrel[redir->rd_fd] < 0 |
| 3029 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3030 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3031 | } |
| 3032 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 3033 | * of the heredoc */ |
| 3034 | debug_printf_parse("set heredoc '%s'\n", |
| 3035 | redir->rd_filename); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3036 | setup_heredoc(redir); |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 3037 | continue; |
| 3038 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3039 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3040 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
| 3041 | /* rd_fd<*>file case (<*> is <,>,>>,<>) */ |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 3042 | char *p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3043 | if (redir->rd_filename == NULL) { |
| 3044 | /* Something went wrong in the parse. |
| 3045 | * Pretend it didn't happen */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3046 | bb_error_msg("bug in redirect parse"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3047 | continue; |
| 3048 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3049 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3050 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 3051 | openfd = open_or_warn(p, mode); |
| 3052 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3053 | if (openfd < 0) { |
| 3054 | /* this could get lost if stderr has been redirected, but |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3055 | * bash and ash both lose it as well (though zsh doesn't!) */ |
| 3056 | //what the above comment tries to say? |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3057 | return 1; |
| 3058 | } |
| 3059 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3060 | /* rd_fd<*>rd_dup or rd_fd<*>- cases */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3061 | openfd = redir->rd_dup; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3064 | if (openfd != redir->rd_fd) { |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 3065 | if (squirrel && redir->rd_fd < 3 |
| 3066 | && squirrel[redir->rd_fd] < 0 |
| 3067 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3068 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3069 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3070 | if (openfd == REDIRFD_CLOSE) { |
| 3071 | /* "n>-" means "close me" */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3072 | close(redir->rd_fd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3073 | } else { |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3074 | xdup2(openfd, redir->rd_fd); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3075 | if (redir->rd_dup == REDIRFD_TO_FILE) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3076 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3077 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3078 | } |
| 3079 | } |
| 3080 | return 0; |
| 3081 | } |
| 3082 | |
| 3083 | static void restore_redirects(int squirrel[]) |
| 3084 | { |
| 3085 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3086 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3087 | fd = squirrel[i]; |
| 3088 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3089 | /* We simply die on error */ |
| 3090 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3091 | } |
| 3092 | } |
| 3093 | } |
| 3094 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3095 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3096 | static void free_pipe_list(struct pipe *head); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3097 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3098 | /* Return code is the exit status of the pipe */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3099 | static void free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3100 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3101 | char **p; |
| 3102 | struct command *command; |
| 3103 | struct redir_struct *r, *rnext; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3104 | int a, i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3105 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3106 | if (pi->stopped_cmds > 0) /* why? */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3107 | return; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3108 | debug_printf_clean("run pipe: (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3109 | for (i = 0; i < pi->num_cmds; i++) { |
| 3110 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3111 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3112 | if (command->argv) { |
| 3113 | for (a = 0, p = command->argv; *p; a++, p++) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3114 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3115 | } |
| 3116 | free_strings(command->argv); |
| 3117 | command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3118 | } |
| 3119 | /* not "else if": on syntax error, we may have both! */ |
| 3120 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3121 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 3122 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3123 | free_pipe_list(command->group); |
| 3124 | debug_printf_clean(" end group\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3125 | command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3126 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3127 | /* else is crucial here. |
| 3128 | * If group != NULL, child_func is meaningless */ |
| 3129 | #if ENABLE_HUSH_FUNCTIONS |
| 3130 | else if (command->child_func) { |
| 3131 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 3132 | command->child_func->parent_cmd = NULL; |
| 3133 | } |
| 3134 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3135 | #if !BB_MMU |
| 3136 | free(command->group_as_string); |
| 3137 | command->group_as_string = NULL; |
| 3138 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3139 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3140 | debug_printf_clean(" redirect %d%s", |
| 3141 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3142 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3143 | if (r->rd_filename) { |
| 3144 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3145 | free(r->rd_filename); |
| 3146 | r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3147 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3148 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3149 | rnext = r->next; |
| 3150 | free(r); |
| 3151 | } |
| 3152 | command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3153 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3154 | free(pi->cmds); /* children are an array, they get freed all at once */ |
| 3155 | pi->cmds = NULL; |
| 3156 | #if ENABLE_HUSH_JOB |
| 3157 | free(pi->cmdtext); |
| 3158 | pi->cmdtext = NULL; |
| 3159 | #endif |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3160 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3161 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3162 | static void free_pipe_list(struct pipe *head) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3163 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3164 | struct pipe *pi, *next; |
| 3165 | |
| 3166 | for (pi = head; pi; pi = next) { |
| 3167 | #if HAS_KEYWORDS |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3168 | debug_printf_clean(" pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3169 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3170 | free_pipe(pi); |
| 3171 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3172 | next = pi->next; |
| 3173 | /*pi->next = NULL;*/ |
| 3174 | free(pi); |
| 3175 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3179 | static int run_list(struct pipe *pi); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3180 | #if BB_MMU |
| 3181 | #define parse_stream(pstring, input, end_trigger) \ |
| 3182 | parse_stream(input, end_trigger) |
| 3183 | #endif |
| 3184 | static struct pipe *parse_stream(char **pstring, |
| 3185 | struct in_str *input, |
| 3186 | int end_trigger); |
| 3187 | static void parse_and_run_string(const char *s); |
| 3188 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3189 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 3190 | static char *find_in_path(const char *arg) |
| 3191 | { |
| 3192 | char *ret = NULL; |
| 3193 | const char *PATH = get_local_var_value("PATH"); |
| 3194 | |
| 3195 | if (!PATH) |
| 3196 | return NULL; |
| 3197 | |
| 3198 | while (1) { |
| 3199 | const char *end = strchrnul(PATH, ':'); |
| 3200 | int sz = end - PATH; /* must be int! */ |
| 3201 | |
| 3202 | free(ret); |
| 3203 | if (sz != 0) { |
| 3204 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 3205 | } else { |
| 3206 | /* We have xxx::yyyy in $PATH, |
| 3207 | * it means "use current dir" */ |
| 3208 | ret = xstrdup(arg); |
| 3209 | } |
| 3210 | if (access(ret, F_OK) == 0) |
| 3211 | break; |
| 3212 | |
| 3213 | if (*end == '\0') { |
| 3214 | free(ret); |
| 3215 | return NULL; |
| 3216 | } |
| 3217 | PATH = end + 1; |
| 3218 | } |
| 3219 | |
| 3220 | return ret; |
| 3221 | } |
| 3222 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3223 | static const struct built_in_command* find_builtin_helper(const char *name, |
| 3224 | const struct built_in_command *x, |
| 3225 | const struct built_in_command *end) |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3226 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3227 | while (x != end) { |
| 3228 | if (strcmp(name, x->cmd) != 0) { |
| 3229 | x++; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3230 | continue; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3231 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3232 | debug_printf_exec("found builtin '%s'\n", name); |
| 3233 | return x; |
| 3234 | } |
| 3235 | return NULL; |
| 3236 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3237 | static const struct built_in_command* find_builtin1(const char *name) |
| 3238 | { |
| 3239 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 3240 | } |
| 3241 | static const struct built_in_command* find_builtin(const char *name) |
| 3242 | { |
| 3243 | const struct built_in_command *x = find_builtin1(name); |
| 3244 | if (x) |
| 3245 | return x; |
| 3246 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 3247 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3248 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3249 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3250 | static struct function **find_function_slot(const char *name) |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3251 | { |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3252 | struct function **funcpp = &G.top_func; |
| 3253 | while (*funcpp) { |
| 3254 | if (strcmp(name, (*funcpp)->name) == 0) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3255 | break; |
| 3256 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3257 | funcpp = &(*funcpp)->next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3258 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3259 | return funcpp; |
| 3260 | } |
| 3261 | |
| 3262 | static const struct function *find_function(const char *name) |
| 3263 | { |
| 3264 | const struct function *funcp = *find_function_slot(name); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3265 | if (funcp) |
| 3266 | debug_printf_exec("found function '%s'\n", name); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3267 | return funcp; |
| 3268 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3269 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3270 | /* Note: takes ownership on name ptr */ |
| 3271 | static struct function *new_function(char *name) |
| 3272 | { |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3273 | struct function **funcpp = find_function_slot(name); |
| 3274 | struct function *funcp = *funcpp; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3275 | |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3276 | if (funcp != NULL) { |
| 3277 | struct command *cmd = funcp->parent_cmd; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3278 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 3279 | if (!cmd) { |
| 3280 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 3281 | free(funcp->name); |
| 3282 | /* Note: if !funcp->body, do not free body_as_string! |
| 3283 | * This is a special case of "-F name body" function: |
| 3284 | * body_as_string was not malloced! */ |
| 3285 | if (funcp->body) { |
| 3286 | free_pipe_list(funcp->body); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3287 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3288 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3289 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3290 | } |
| 3291 | } else { |
| 3292 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 3293 | cmd->argv[0] = funcp->name; |
| 3294 | cmd->group = funcp->body; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3295 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3296 | cmd->group_as_string = funcp->body_as_string; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3297 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3298 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3299 | } else { |
| 3300 | debug_printf_exec("remembering new function '%s'\n", name); |
| 3301 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 3302 | /*funcp->next = NULL;*/ |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3303 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3304 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3305 | funcp->name = name; |
| 3306 | return funcp; |
| 3307 | } |
| 3308 | |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3309 | static void unset_func(const char *name) |
| 3310 | { |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3311 | struct function **funcpp = find_function_slot(name); |
| 3312 | struct function *funcp = *funcpp; |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3313 | |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3314 | if (funcp != NULL) { |
| 3315 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 3316 | *funcpp = funcp->next; |
| 3317 | /* funcp is unlinked now, deleting it. |
| 3318 | * Note: if !funcp->body, the function was created by |
| 3319 | * "-F name body", do not free ->body_as_string |
| 3320 | * and ->name as they were not malloced. */ |
| 3321 | if (funcp->body) { |
| 3322 | free_pipe_list(funcp->body); |
| 3323 | free(funcp->name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3324 | # if !BB_MMU |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3325 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3326 | # endif |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3327 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3328 | free(funcp); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3329 | } |
| 3330 | } |
| 3331 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3332 | # if BB_MMU |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3333 | #define exec_function(to_free, funcp, argv) \ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3334 | exec_function(funcp, argv) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3335 | # endif |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3336 | static void exec_function(char ***to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3337 | const struct function *funcp, |
| 3338 | char **argv) NORETURN; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3339 | static void exec_function(char ***to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3340 | const struct function *funcp, |
| 3341 | char **argv) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3342 | { |
| 3343 | # if BB_MMU |
| 3344 | int n = 1; |
| 3345 | |
| 3346 | argv[0] = G.global_argv[0]; |
| 3347 | G.global_argv = argv; |
| 3348 | while (*++argv) |
| 3349 | n++; |
| 3350 | G.global_argc = n; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3351 | /* On MMU, funcp->body is always non-NULL */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3352 | n = run_list(funcp->body); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 3353 | fflush_all(); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3354 | _exit(n); |
| 3355 | # else |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3356 | re_execute_shell(to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3357 | funcp->body_as_string, |
| 3358 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3359 | argv + 1, |
| 3360 | NULL); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3361 | # endif |
| 3362 | } |
| 3363 | |
| 3364 | static int run_function(const struct function *funcp, char **argv) |
| 3365 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3366 | int rc; |
| 3367 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 3368 | smallint sv_flg; |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3369 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3370 | save_and_replace_G_args(&sv, argv); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3371 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3372 | /* "we are in function, ok to use return" */ |
| 3373 | sv_flg = G.flag_return_in_progress; |
| 3374 | G.flag_return_in_progress = -1; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3375 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3376 | G.func_nest_level++; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3377 | # endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3378 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3379 | /* On MMU, funcp->body is always non-NULL */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3380 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3381 | if (!funcp->body) { |
| 3382 | /* Function defined by -F */ |
| 3383 | parse_and_run_string(funcp->body_as_string); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3384 | rc = G.last_exitcode; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3385 | } else |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3386 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3387 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3388 | rc = run_list(funcp->body); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3389 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3390 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3391 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3392 | { |
| 3393 | struct variable *var; |
| 3394 | struct variable **var_pp; |
| 3395 | |
| 3396 | var_pp = &G.top_var; |
| 3397 | while ((var = *var_pp) != NULL) { |
| 3398 | if (var->func_nest_level < G.func_nest_level) { |
| 3399 | var_pp = &var->next; |
| 3400 | continue; |
| 3401 | } |
| 3402 | /* Unexport */ |
| 3403 | if (var->flg_export) |
| 3404 | bb_unsetenv(var->varstr); |
| 3405 | /* Remove from global list */ |
| 3406 | *var_pp = var->next; |
| 3407 | /* Free */ |
| 3408 | if (!var->max_len) |
| 3409 | free(var->varstr); |
| 3410 | free(var); |
| 3411 | } |
| 3412 | G.func_nest_level--; |
| 3413 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3414 | # endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3415 | G.flag_return_in_progress = sv_flg; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3416 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3417 | restore_G_args(&sv, argv); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3418 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3419 | return rc; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3420 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3421 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3422 | |
| 3423 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3424 | #if BB_MMU |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3425 | #define exec_builtin(to_free, x, argv) \ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3426 | exec_builtin(x, argv) |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3427 | #else |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3428 | #define exec_builtin(to_free, x, argv) \ |
| 3429 | exec_builtin(to_free, argv) |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3430 | #endif |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3431 | static void exec_builtin(char ***to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3432 | const struct built_in_command *x, |
| 3433 | char **argv) NORETURN; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3434 | static void exec_builtin(char ***to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3435 | const struct built_in_command *x, |
| 3436 | char **argv) |
| 3437 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3438 | #if BB_MMU |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3439 | int rcode = x->function(argv); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 3440 | fflush_all(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3441 | _exit(rcode); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3442 | #else |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3443 | /* On NOMMU, we must never block! |
| 3444 | * Example: { sleep 99 | read line; } & echo Ok |
| 3445 | */ |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3446 | re_execute_shell(to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3447 | argv[0], |
| 3448 | G.global_argv[0], |
| 3449 | G.global_argv + 1, |
| 3450 | argv); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3451 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3452 | } |
| 3453 | |
| 3454 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 3455 | static void execvp_or_die(char **argv) NORETURN; |
| 3456 | static void execvp_or_die(char **argv) |
| 3457 | { |
| 3458 | debug_printf_exec("execing '%s'\n", argv[0]); |
| 3459 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 3460 | execvp(argv[0], argv); |
| 3461 | bb_perror_msg("can't execute '%s'", argv[0]); |
| 3462 | _exit(127); /* bash compat */ |
| 3463 | } |
| 3464 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 3465 | #if BB_MMU |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3466 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 3467 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 3468 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 3469 | pseudo_exec(command, argv_expanded) |
| 3470 | #endif |
| 3471 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3472 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3473 | * Never returns. |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3474 | * Don't exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3475 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3476 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3477 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 3478 | char **argv, int assignment_cnt, |
| 3479 | char **argv_expanded) NORETURN; |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 3480 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3481 | char **argv, int assignment_cnt, |
| 3482 | char **argv_expanded) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3483 | { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3484 | char **new_env; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3485 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3486 | /* Case when we are here: ... | var=val | ... */ |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3487 | if (!argv[assignment_cnt]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3488 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3489 | |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 3490 | new_env = expand_assignments(argv, assignment_cnt); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3491 | #if BB_MMU |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3492 | set_vars_and_save_old(new_env); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3493 | free(new_env); /* optional */ |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3494 | /* we can also destroy set_vars_and_save_old's return value, |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3495 | * to save memory */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3496 | #else |
| 3497 | nommu_save->new_env = new_env; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3498 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3499 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3500 | if (argv_expanded) { |
| 3501 | argv = argv_expanded; |
| 3502 | } else { |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 3503 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3504 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3505 | nommu_save->argv = argv; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3506 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3507 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 3508 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3509 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 3510 | if (strchr(argv[0], '/') != NULL) |
| 3511 | goto skip; |
| 3512 | #endif |
| 3513 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3514 | /* Check if the command matches any of the builtins. |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3515 | * Depending on context, this might be redundant. But it's |
| 3516 | * easier to waste a few CPU cycles than it is to figure out |
| 3517 | * if this is one of those cases. |
| 3518 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3519 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3520 | /* On NOMMU, it is more expensive to re-execute shell |
| 3521 | * just in order to run echo or test builtin. |
| 3522 | * It's better to skip it here and run corresponding |
| 3523 | * non-builtin later. */ |
| 3524 | const struct built_in_command *x; |
| 3525 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3526 | if (x) { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3527 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3528 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3529 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3530 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3531 | /* Check if the command matches any functions */ |
| 3532 | { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3533 | const struct function *funcp = find_function(argv[0]); |
| 3534 | if (funcp) { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3535 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3536 | } |
| 3537 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3538 | #endif |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3539 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 3540 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3541 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3542 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3543 | int a = find_applet_by_name(argv[0]); |
| 3544 | if (a >= 0) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3545 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3546 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3547 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3548 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3549 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3550 | # endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3551 | /* Re-exec ourselves */ |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3552 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3553 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 3554 | execv(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3555 | /* If they called chroot or otherwise made the binary no longer |
| 3556 | * executable, fall through */ |
| 3557 | } |
| 3558 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 3559 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3560 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3561 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3562 | skip: |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3563 | #endif |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 3564 | execvp_or_die(argv); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3565 | } |
| 3566 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3567 | /* Called after [v]fork() in run_pipe |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3568 | */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3569 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3570 | struct command *command, |
| 3571 | char **argv_expanded) NORETURN; |
| 3572 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3573 | struct command *command, |
| 3574 | char **argv_expanded) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3575 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3576 | if (command->argv) { |
| 3577 | pseudo_exec_argv(nommu_save, command->argv, |
| 3578 | command->assignment_cnt, argv_expanded); |
| 3579 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3580 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3581 | if (command->group) { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3582 | /* Cases when we are here: |
| 3583 | * ( list ) |
| 3584 | * { list } & |
| 3585 | * ... | ( list ) | ... |
| 3586 | * ... | { list } | ... |
| 3587 | */ |
| 3588 | #if BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 3589 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3590 | debug_printf_exec("pseudo_exec: run_list\n"); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3591 | reset_traps_to_defaults(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3592 | rcode = run_list(command->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 3593 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3594 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3595 | _exit(rcode); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3596 | #else |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3597 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 3598 | command->group_as_string, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3599 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3600 | G.global_argv + 1, |
| 3601 | NULL); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3602 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3603 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3604 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3605 | /* Case when we are here: ... | >file */ |
| 3606 | debug_printf_exec("pseudo_exec'ed null command\n"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3607 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3610 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3611 | static const char *get_cmdtext(struct pipe *pi) |
| 3612 | { |
| 3613 | char **argv; |
| 3614 | char *p; |
| 3615 | int len; |
| 3616 | |
| 3617 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 3618 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3619 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3620 | if (pi->cmdtext) |
| 3621 | return pi->cmdtext; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3622 | argv = pi->cmds[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3623 | if (!argv || !argv[0]) { |
| 3624 | pi->cmdtext = xzalloc(1); |
| 3625 | return pi->cmdtext; |
| 3626 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3627 | |
| 3628 | len = 0; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3629 | do { |
| 3630 | len += strlen(*argv) + 1; |
| 3631 | } while (*++argv); |
| 3632 | p = xmalloc(len); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3633 | pi->cmdtext = p; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3634 | argv = pi->cmds[0].argv; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3635 | do { |
| 3636 | len = strlen(*argv); |
| 3637 | memcpy(p, *argv, len); |
| 3638 | p += len; |
| 3639 | *p++ = ' '; |
| 3640 | } while (*++argv); |
| 3641 | p[-1] = '\0'; |
| 3642 | return pi->cmdtext; |
| 3643 | } |
| 3644 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3645 | static void insert_bg_job(struct pipe *pi) |
| 3646 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3647 | struct pipe *job, **jobp; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3648 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3649 | |
| 3650 | /* Linear search for the ID of the job to use */ |
| 3651 | pi->jobid = 1; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3652 | for (job = G.job_list; job; job = job->next) |
| 3653 | if (job->jobid >= pi->jobid) |
| 3654 | pi->jobid = job->jobid + 1; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3655 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3656 | /* Add job to the list of running jobs */ |
| 3657 | jobp = &G.job_list; |
| 3658 | while ((job = *jobp) != NULL) |
| 3659 | jobp = &job->next; |
| 3660 | job = *jobp = xmalloc(sizeof(*job)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3661 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3662 | *job = *pi; /* physical copy */ |
| 3663 | job->next = NULL; |
| 3664 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 3665 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3666 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3667 | job->cmds[i].pid = pi->cmds[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3668 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3669 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3670 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3671 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3672 | if (G_interactive_fd) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3673 | printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext); |
| 3674 | /* Last command's pid goes to $! */ |
| 3675 | G.last_bg_pid = job->cmds[job->num_cmds - 1].pid; |
| 3676 | G.last_jobid = job->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3679 | static void remove_bg_job(struct pipe *pi) |
| 3680 | { |
| 3681 | struct pipe *prev_pipe; |
| 3682 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3683 | if (pi == G.job_list) { |
| 3684 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3685 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3686 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3687 | while (prev_pipe->next != pi) |
| 3688 | prev_pipe = prev_pipe->next; |
| 3689 | prev_pipe->next = pi->next; |
| 3690 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3691 | if (G.job_list) |
| 3692 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3693 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3694 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3695 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3696 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3697 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3698 | static void delete_finished_bg_job(struct pipe *pi) |
| 3699 | { |
| 3700 | remove_bg_job(pi); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3701 | pi->stopped_cmds = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3702 | free_pipe(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3703 | free(pi); |
| 3704 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3705 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3706 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3707 | /* Check to see if any processes have exited -- if they |
| 3708 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3709 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3710 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3711 | int attributes; |
| 3712 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3713 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3714 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3715 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3716 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3717 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3718 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3719 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 3720 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3721 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3722 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3723 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3724 | |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3725 | errno = 0; |
| 3726 | #if ENABLE_HUSH_FAST |
| 3727 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 3728 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 3729 | //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] | 3730 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 3731 | /* Avoid doing waitpid syscall if possible */ |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3732 | if (!G.we_have_children) { |
| 3733 | errno = ECHILD; |
| 3734 | return -1; |
| 3735 | } |
| 3736 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 3737 | /* We have children, but they did not exit |
| 3738 | * or stop yet (we saw no SIGCHLD) */ |
| 3739 | return 0; |
| 3740 | } |
| 3741 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 3742 | } |
| 3743 | #endif |
| 3744 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3745 | /* Do we do this right? |
| 3746 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3747 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3748 | * [3]+ Stopped sleep 20 | false |
| 3749 | * bash-3.00# echo $? |
| 3750 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3751 | * [hush 1.14.0: yes we do it right] |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3752 | */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3753 | wait_more: |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3754 | while (1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3755 | int i; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3756 | int dead; |
| 3757 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3758 | #if ENABLE_HUSH_FAST |
| 3759 | i = G.count_SIGCHLD; |
| 3760 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3761 | childpid = waitpid(-1, &status, attributes); |
| 3762 | if (childpid <= 0) { |
| 3763 | if (childpid && errno != ECHILD) |
| 3764 | bb_perror_msg("waitpid"); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3765 | #if ENABLE_HUSH_FAST |
| 3766 | else { /* Until next SIGCHLD, waitpid's are useless */ |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3767 | G.we_have_children = (childpid == 0); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3768 | G.handled_SIGCHLD = i; |
| 3769 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 3770 | } |
| 3771 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3772 | break; |
| 3773 | } |
| 3774 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 3775 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3776 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3777 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3778 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3779 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 3780 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3781 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3782 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 3783 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3784 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3785 | childpid, WEXITSTATUS(status)); |
| 3786 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3787 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3788 | if (fg_pipe) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3789 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 3790 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 3791 | if (fg_pipe->cmds[i].pid != childpid) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3792 | continue; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3793 | if (dead) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3794 | fg_pipe->cmds[i].pid = 0; |
| 3795 | fg_pipe->alive_cmds--; |
| 3796 | if (i == fg_pipe->num_cmds - 1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3797 | /* last process gives overall exitstatus */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3798 | /* Note: is WIFSIGNALED, WEXITSTATUS = sig + 128 */ |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3799 | rcode = WEXITSTATUS(status); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3800 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 3801 | /* bash prints killer signal's name for *last* |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3802 | * process in pipe (prints just newline for SIGINT). |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 3803 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3804 | */ |
| 3805 | if (WIFSIGNALED(status)) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3806 | int sig = WTERMSIG(status); |
| 3807 | printf("%s\n", sig == SIGINT ? "" : get_signame(sig)); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3808 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3809 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3810 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3811 | fg_pipe->cmds[i].is_stopped = 1; |
| 3812 | fg_pipe->stopped_cmds++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3813 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3814 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 3815 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 3816 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3817 | /* All processes in fg pipe have exited or stopped */ |
| 3818 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 3819 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 3820 | * and "killall -STOP cat" */ |
| 3821 | if (G_interactive_fd) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3822 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3823 | if (fg_pipe->alive_cmds) |
| 3824 | insert_bg_job(fg_pipe); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3825 | #endif |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3826 | return rcode; |
| 3827 | } |
Denys Vlasenko | 6f22624 | 2009-06-03 14:37:30 +0200 | [diff] [blame] | 3828 | if (!fg_pipe->alive_cmds) |
| 3829 | return rcode; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3830 | } |
| 3831 | /* There are still running processes in the fg pipe */ |
| 3832 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3833 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3834 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3835 | } |
| 3836 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3837 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3838 | /* We asked to wait for bg or orphaned children */ |
| 3839 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3840 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3841 | for (i = 0; i < pi->num_cmds; i++) { |
| 3842 | if (pi->cmds[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3843 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3844 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3845 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3846 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 3847 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3848 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 3849 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3850 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3851 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3852 | /* child exited */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3853 | pi->cmds[i].pid = 0; |
| 3854 | pi->alive_cmds--; |
| 3855 | if (!pi->alive_cmds) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3856 | if (G_interactive_fd) |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 3857 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 3858 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3859 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3860 | } |
| 3861 | } else { |
| 3862 | /* child stopped */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3863 | pi->cmds[i].is_stopped = 1; |
| 3864 | pi->stopped_cmds++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3865 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3866 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3867 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3868 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3869 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3872 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3873 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 3874 | { |
| 3875 | pid_t p; |
| 3876 | int rcode = checkjobs(fg_pipe); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 3877 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3878 | /* Job finished, move the shell to the foreground */ |
| 3879 | p = getpgrp(); /* our process group id */ |
| 3880 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 3881 | tcsetpgrp(G_interactive_fd, p); |
| 3882 | } |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3883 | return rcode; |
| 3884 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3885 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3886 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3887 | /* Start all the jobs, but don't wait for anything to finish. |
| 3888 | * See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3889 | * |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3890 | * 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] | 3891 | * to finish to determine the exit status of the pipe. If the pipe |
| 3892 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3893 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3894 | * return value. |
| 3895 | * |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3896 | * Returns -1 only if started some children. IOW: we have to |
| 3897 | * mask out retvals of builtins etc with 0xff! |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3898 | * |
| 3899 | * The only case when we do not need to [v]fork is when the pipe |
| 3900 | * is single, non-backgrounded, non-subshell command. Examples: |
| 3901 | * cmd ; ... { list } ; ... |
| 3902 | * cmd && ... { list } && ... |
| 3903 | * cmd || ... { list } || ... |
| 3904 | * If it is, then we can run cmd as a builtin, NOFORK [do we do this?], |
| 3905 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3906 | * 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] | 3907 | * |
| 3908 | * Cases when we must fork: |
| 3909 | * non-single: cmd | cmd |
| 3910 | * backgrounded: cmd & { list } & |
| 3911 | * subshell: ( list ) [&] |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3912 | */ |
Denys Vlasenko | a7bb3c1 | 2009-10-08 12:28:08 +0200 | [diff] [blame] | 3913 | static NOINLINE int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3914 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3915 | static const char *const null_ptr = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3916 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3917 | int nextin; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3918 | struct command *command; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3919 | char **argv_expanded; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3920 | char **argv; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3921 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3922 | /* it is not always needed, but we aim to smaller code */ |
| 3923 | int squirrel[] = { -1, -1, -1 }; |
| 3924 | int rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3925 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3926 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3927 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 3928 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 3929 | IF_HUSH_JOB(pi->pgrp = -1;) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3930 | pi->stopped_cmds = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3931 | command = &(pi->cmds[0]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3932 | argv_expanded = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3933 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3934 | if (pi->num_cmds != 1 |
| 3935 | || pi->followup == PIPE_BG |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3936 | || command->cmd_type == CMD_SUBSHELL |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3937 | ) { |
| 3938 | goto must_fork; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3939 | } |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3940 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3941 | pi->alive_cmds = 1; |
| 3942 | |
| 3943 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 3944 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 3945 | |
| 3946 | if (command->group) { |
| 3947 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3948 | if (command->cmd_type == CMD_FUNCDEF) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3949 | /* "executing" func () { list } */ |
| 3950 | struct function *funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3951 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3952 | funcp = new_function(command->argv[0]); |
| 3953 | /* funcp->name is already set to argv[0] */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3954 | funcp->body = command->group; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3955 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3956 | funcp->body_as_string = command->group_as_string; |
| 3957 | command->group_as_string = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3958 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3959 | command->group = NULL; |
| 3960 | command->argv[0] = NULL; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3961 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 3962 | funcp->parent_cmd = command; |
| 3963 | command->child_func = funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3964 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3965 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 3966 | debug_leave(); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3967 | return EXIT_SUCCESS; |
| 3968 | } |
| 3969 | #endif |
| 3970 | /* { list } */ |
| 3971 | debug_printf("non-subshell group\n"); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3972 | rcode = 1; /* exitcode if redir failed */ |
| 3973 | if (setup_redirects(command, squirrel) == 0) { |
| 3974 | debug_printf_exec(": run_list\n"); |
| 3975 | rcode = run_list(command->group) & 0xff; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3976 | } |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 3977 | restore_redirects(squirrel); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3978 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3979 | debug_leave(); |
| 3980 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3981 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3982 | } |
| 3983 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3984 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 3985 | { |
| 3986 | const struct built_in_command *x; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3987 | #if ENABLE_HUSH_FUNCTIONS |
| 3988 | const struct function *funcp; |
| 3989 | #else |
| 3990 | enum { funcp = 0 }; |
| 3991 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3992 | char **new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3993 | struct variable *old_vars = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3994 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3995 | if (argv[command->assignment_cnt] == NULL) { |
| 3996 | /* Assignments, but no command */ |
| 3997 | /* Ensure redirects take effect. Try "a=t >file" */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3998 | rcode = setup_redirects(command, squirrel); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3999 | restore_redirects(squirrel); |
| 4000 | /* Set shell variables */ |
| 4001 | while (*argv) { |
| 4002 | p = expand_string_to_string(*argv); |
| 4003 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 4004 | *argv, p); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4005 | set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4006 | argv++; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 4007 | } |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4008 | /* Do we need to flag set_local_var() errors? |
| 4009 | * "assignment to readonly var" and "putenv error" |
| 4010 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4011 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4012 | debug_leave(); |
| 4013 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4014 | return rcode; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 4015 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4016 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4017 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4018 | if (0) {} |
| 4019 | #if ENABLE_HUSH_BASH_COMPAT |
| 4020 | else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4021 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
| 4022 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4023 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4024 | #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 4025 | else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB_COND) { |
| 4026 | argv_expanded = expand_strvec_to_strvec_singleword_noglob_cond(argv + command->assignment_cnt); |
| 4027 | |
| 4028 | } |
| 4029 | #endif |
| 4030 | else { |
| 4031 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
| 4032 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4033 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4034 | /* if someone gives us an empty string: `cmd with empty output` */ |
Mike Frysinger | 28736c3 | 2009-10-18 01:11:45 -0400 | [diff] [blame] | 4035 | if (!argv_expanded[0]) { |
| 4036 | debug_leave(); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 4037 | return G.last_exitcode; |
Mike Frysinger | 28736c3 | 2009-10-18 01:11:45 -0400 | [diff] [blame] | 4038 | } |
| 4039 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4040 | x = find_builtin(argv_expanded[0]); |
| 4041 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4042 | funcp = NULL; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4043 | if (!x) |
| 4044 | funcp = find_function(argv_expanded[0]); |
| 4045 | #endif |
| 4046 | if (x || funcp) { |
| 4047 | if (!funcp) { |
| 4048 | if (x->function == builtin_exec && argv_expanded[1] == NULL) { |
| 4049 | debug_printf("exec with redirects only\n"); |
| 4050 | rcode = setup_redirects(command, NULL); |
| 4051 | goto clean_up_and_ret1; |
| 4052 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4053 | } |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 4054 | /* setup_redirects acts on file descriptors, not FILEs. |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4055 | * This is perfect for work that comes after exec(). |
| 4056 | * Is it really safe for inline use? Experimentally, |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 4057 | * things seem to work. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4058 | rcode = setup_redirects(command, squirrel); |
| 4059 | if (rcode == 0) { |
| 4060 | new_env = expand_assignments(argv, command->assignment_cnt); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 4061 | old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4062 | if (!funcp) { |
| 4063 | debug_printf_exec(": builtin '%s' '%s'...\n", |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4064 | x->cmd, argv_expanded[1]); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4065 | rcode = x->function(argv_expanded) & 0xff; |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 4066 | fflush_all(); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4067 | } |
| 4068 | #if ENABLE_HUSH_FUNCTIONS |
| 4069 | else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4070 | # if ENABLE_HUSH_LOCAL |
| 4071 | struct variable **sv; |
| 4072 | sv = G.shadowed_vars_pp; |
| 4073 | G.shadowed_vars_pp = &old_vars; |
| 4074 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4075 | debug_printf_exec(": function '%s' '%s'...\n", |
| 4076 | funcp->name, argv_expanded[1]); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 4077 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4078 | # if ENABLE_HUSH_LOCAL |
| 4079 | G.shadowed_vars_pp = sv; |
| 4080 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4081 | } |
| 4082 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4083 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4084 | #if ENABLE_FEATURE_SH_STANDALONE |
| 4085 | clean_up_and_ret: |
| 4086 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4087 | restore_redirects(squirrel); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 4088 | unset_vars(new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4089 | add_vars(old_vars); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4090 | clean_up_and_ret1: |
| 4091 | free(argv_expanded); |
| 4092 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4093 | debug_leave(); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4094 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 4095 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4096 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4097 | |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 4098 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4099 | i = find_applet_by_name(argv_expanded[0]); |
| 4100 | if (i >= 0 && APPLET_IS_NOFORK(i)) { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4101 | rcode = setup_redirects(command, squirrel); |
| 4102 | if (rcode == 0) { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4103 | new_env = expand_assignments(argv, command->assignment_cnt); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 4104 | old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4105 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4106 | argv_expanded[0], argv_expanded[1]); |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4107 | rcode = run_nofork_applet(i, argv_expanded); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4108 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4109 | goto clean_up_and_ret; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 4110 | } |
| 4111 | #endif |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4112 | /* It is neither builtin nor applet. We must fork. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4113 | } |
| 4114 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4115 | must_fork: |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4116 | /* NB: argv_expanded may already be created, and that |
| 4117 | * might include `cmd` runs! Do not rerun it! We *must* |
| 4118 | * use argv_expanded if it's non-NULL */ |
| 4119 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4120 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4121 | pi->alive_cmds = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4122 | nextin = 0; |
| 4123 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4124 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4125 | struct fd_pair pipefds; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 4126 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4127 | volatile nommu_save_t nommu_save; |
| 4128 | nommu_save.new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4129 | nommu_save.old_vars = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4130 | nommu_save.argv = NULL; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4131 | nommu_save.argv_from_re_execing = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 4132 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4133 | command = &(pi->cmds[i]); |
| 4134 | if (command->argv) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4135 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 4136 | command->argv[0], command->argv[1]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4137 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4138 | debug_printf_exec(": pipe member with no argv\n"); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4139 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4140 | |
| 4141 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4142 | pipefds.rd = 0; |
| 4143 | pipefds.wr = 1; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4144 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4145 | xpiped_pair(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4146 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4147 | command->pid = BB_MMU ? fork() : vfork(); |
| 4148 | if (!command->pid) { /* child */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4149 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4150 | disable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4151 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4152 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4153 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4154 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4155 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4156 | pid_t pgrp; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4157 | pgrp = pi->pgrp; |
| 4158 | if (pgrp < 0) /* true for 1st process only */ |
| 4159 | pgrp = getpid(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 4160 | if (setpgid(0, pgrp) == 0 |
| 4161 | && pi->followup != PIPE_BG |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 4162 | && G_saved_tty_pgrp /* we have ctty */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 4163 | ) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4164 | /* We do it in *every* child, not just first, |
| 4165 | * to avoid races */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4166 | tcsetpgrp(G_interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4167 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4168 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4169 | #endif |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4170 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 4171 | /* 1st cmd in backgrounded pipe |
| 4172 | * should have its stdin /dev/null'ed */ |
| 4173 | close(0); |
| 4174 | if (open(bb_dev_null, O_RDONLY)) |
| 4175 | xopen("/", O_RDONLY); |
| 4176 | } else { |
| 4177 | xmove_fd(nextin, 0); |
| 4178 | } |
| 4179 | xmove_fd(pipefds.wr, 1); |
| 4180 | if (pipefds.rd > 1) |
| 4181 | close(pipefds.rd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4182 | /* Like bash, explicit redirects override pipes, |
| 4183 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4184 | if (setup_redirects(command, NULL)) |
| 4185 | _exit(1); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4186 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4187 | /* Restore default handlers just prior to exec */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 4188 | /*signal(SIGCHLD, SIG_DFL); - so far we don't have any handlers */ |
| 4189 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4190 | /* Stores to nommu_save list of env vars putenv'ed |
| 4191 | * (NOMMU, on MMU we don't need that) */ |
| 4192 | /* cast away volatility... */ |
| 4193 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4194 | /* pseudo_exec() does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4195 | } |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 4196 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 4197 | /* parent or error */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 4198 | #if ENABLE_HUSH_FAST |
| 4199 | G.count_SIGCHLD++; |
| 4200 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 4201 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4202 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4203 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4204 | /* Clean up after vforked child */ |
| 4205 | free(nommu_save.argv); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4206 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 4207 | unset_vars(nommu_save.new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4208 | add_vars(nommu_save.old_vars); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4209 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4210 | free(argv_expanded); |
| 4211 | argv_expanded = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4212 | if (command->pid < 0) { /* [v]fork failed */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4213 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 4214 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4215 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4216 | pi->alive_cmds++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4217 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4218 | /* Second and next children need to know pid of first one */ |
| 4219 | if (pi->pgrp < 0) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4220 | pi->pgrp = command->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4221 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4222 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4223 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4224 | if (i) |
| 4225 | close(nextin); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4226 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4227 | close(pipefds.wr); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4228 | /* Pass read (output) pipe end to next iteration */ |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4229 | nextin = pipefds.rd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4230 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4231 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4232 | if (!pi->alive_cmds) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4233 | debug_leave(); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4234 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 4235 | return 1; |
| 4236 | } |
| 4237 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4238 | debug_leave(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4239 | 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] | 4240 | return -1; |
| 4241 | } |
| 4242 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 4243 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4244 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 4245 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4246 | static const char *const PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4247 | [PIPE_SEQ] = "SEQ", |
| 4248 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4249 | [PIPE_OR ] = "OR" , |
| 4250 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4251 | }; |
| 4252 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4253 | [RES_NONE ] = "NONE" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4254 | # if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4255 | [RES_IF ] = "IF" , |
| 4256 | [RES_THEN ] = "THEN" , |
| 4257 | [RES_ELIF ] = "ELIF" , |
| 4258 | [RES_ELSE ] = "ELSE" , |
| 4259 | [RES_FI ] = "FI" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4260 | # endif |
| 4261 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4262 | [RES_FOR ] = "FOR" , |
| 4263 | [RES_WHILE] = "WHILE", |
| 4264 | [RES_UNTIL] = "UNTIL", |
| 4265 | [RES_DO ] = "DO" , |
| 4266 | [RES_DONE ] = "DONE" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4267 | # endif |
| 4268 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4269 | [RES_IN ] = "IN" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4270 | # endif |
| 4271 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4272 | [RES_CASE ] = "CASE" , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4273 | [RES_CASE_IN ] = "CASE_IN" , |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4274 | [RES_MATCH] = "MATCH", |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4275 | [RES_CASE_BODY] = "CASE_BODY", |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4276 | [RES_ESAC ] = "ESAC" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4277 | # endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4278 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4279 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4280 | }; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4281 | static const char *const CMDTYPE[] = { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4282 | "{}", |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 4283 | "()", |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4284 | "[noglob]", |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4285 | # if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4286 | "func()", |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4287 | # endif |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4288 | }; |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4289 | |
| 4290 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4291 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4292 | pin = 0; |
| 4293 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4294 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 4295 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4296 | prn = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4297 | while (prn < pi->num_cmds) { |
| 4298 | struct command *command = &pi->cmds[prn]; |
| 4299 | char **argv = command->argv; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4300 | |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4301 | fprintf(stderr, "%*s cmd %d assignment_cnt:%d", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4302 | lvl*2, "", prn, |
| 4303 | command->assignment_cnt); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4304 | if (command->group) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4305 | fprintf(stderr, " group %s: (argv=%p)\n", |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4306 | CMDTYPE[command->cmd_type], |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4307 | argv); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4308 | debug_print_tree(command->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4309 | prn++; |
| 4310 | continue; |
| 4311 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4312 | if (argv) while (*argv) { |
| 4313 | fprintf(stderr, " '%s'", *argv); |
| 4314 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 4315 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4316 | fprintf(stderr, "\n"); |
| 4317 | prn++; |
| 4318 | } |
| 4319 | pi = pi->next; |
| 4320 | pin++; |
| 4321 | } |
| 4322 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4323 | #endif /* debug_print_tree */ |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4324 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 4325 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 4326 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4327 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4328 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4329 | #if ENABLE_HUSH_CASE |
| 4330 | char *case_word = NULL; |
| 4331 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4332 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4333 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4334 | char **for_lcur = NULL; |
| 4335 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4336 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4337 | smallint last_followup; |
| 4338 | smalluint rcode; |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 4339 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4340 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4341 | #else |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4342 | enum { cond_code = 0 }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4343 | #endif |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4344 | #if HAS_KEYWORDS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4345 | smallint rword; /* enum reserved_style */ |
| 4346 | smallint last_rword; /* ditto */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4347 | #endif |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4348 | |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4349 | 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] | 4350 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4351 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4352 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4353 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4354 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 4355 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4356 | continue; |
| 4357 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4358 | if (cpipe->next == NULL) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4359 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4360 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4361 | 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] | 4362 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4363 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4364 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4365 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4366 | continue; |
| 4367 | /* 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] | 4368 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 4369 | || 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] | 4370 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4371 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4372 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4373 | 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] | 4374 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4375 | } |
| 4376 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4377 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4378 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4379 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 4380 | * in order to return, no direct "return" statements please. |
| 4381 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4382 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4383 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4384 | G.run_list_level++; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4385 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4386 | |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4387 | #if HAS_KEYWORDS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4388 | rword = RES_NONE; |
| 4389 | last_rword = RES_XXXX; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4390 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4391 | last_followup = PIPE_SEQ; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4392 | rcode = G.last_exitcode; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4393 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4394 | /* Go through list of pipes, (maybe) executing them. */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4395 | 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] | 4396 | if (G.flag_SIGINT) |
| 4397 | break; |
| 4398 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4399 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4400 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 4401 | rword, cond_code, last_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4402 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 4403 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4404 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 4405 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4406 | /* start of a loop: remember where loop starts */ |
| 4407 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4408 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4409 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4410 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4411 | /* Still in the same "if...", "then..." or "do..." branch? */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4412 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4413 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 4414 | || (rcode != 0 && last_followup == PIPE_AND) |
| 4415 | ) { |
| 4416 | /* It is "<true> || CMD" or "<false> && CMD" |
| 4417 | * and we should not execute CMD */ |
| 4418 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 4419 | last_followup = pi->followup; |
| 4420 | continue; |
| 4421 | } |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4422 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4423 | last_followup = pi->followup; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4424 | IF_HAS_KEYWORDS(last_rword = rword;) |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4425 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4426 | if (cond_code) { |
| 4427 | if (rword == RES_THEN) { |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4428 | /* if false; then ... fi has exitcode 0! */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4429 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4430 | /* "if <false> THEN cmd": skip cmd */ |
| 4431 | continue; |
| 4432 | } |
| 4433 | } else { |
| 4434 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 4435 | /* "if <true> then ... ELSE/ELIF cmd": |
| 4436 | * skip cmd and all following ones */ |
| 4437 | break; |
| 4438 | } |
| 4439 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4440 | #endif |
| 4441 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4442 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4443 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 4444 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4445 | |
| 4446 | static const char encoded_dollar_at[] ALIGN1 = { |
| 4447 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 4448 | }; /* encoded representation of "$@" */ |
| 4449 | static const char *const encoded_dollar_at_argv[] = { |
| 4450 | encoded_dollar_at, NULL |
| 4451 | }; /* argv list with one element: "$@" */ |
| 4452 | char **vals; |
| 4453 | |
| 4454 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4455 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4456 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4457 | if (!pi->next->cmds[0].argv) { |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4458 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4459 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4460 | break; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4461 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4462 | vals = pi->next->cmds[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4463 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4464 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4465 | debug_print_strings("for_list made from", vals); |
| 4466 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4467 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4468 | debug_print_strings("for_list", for_list); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4469 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4470 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 4471 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4472 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4473 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4474 | for_lcur = NULL; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4475 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4476 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4477 | /* Insert next value from for_lcur */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4478 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4479 | 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] | 4480 | continue; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4481 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4482 | if (rword == RES_IN) { |
| 4483 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 4484 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4485 | if (rword == RES_DONE) { |
| 4486 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4487 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4488 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4489 | #if ENABLE_HUSH_CASE |
| 4490 | if (rword == RES_CASE) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4491 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4492 | continue; |
| 4493 | } |
| 4494 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4495 | char **argv; |
| 4496 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4497 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 4498 | break; |
| 4499 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4500 | argv = pi->cmds->argv; |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4501 | while (*argv) { |
| 4502 | char *pattern = expand_string_to_string(*argv); |
| 4503 | /* TODO: which FNM_xxx flags to use? */ |
| 4504 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 4505 | free(pattern); |
| 4506 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 4507 | free(case_word); /* make future "word)" stop */ |
| 4508 | case_word = NULL; |
| 4509 | break; |
| 4510 | } |
| 4511 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4512 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4513 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4514 | } |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4515 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4516 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4517 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4518 | } |
| 4519 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4520 | /* Just pressing <enter> in shell should check for jobs. |
| 4521 | * OTOH, in non-interactive shell this is useless |
| 4522 | * and only leads to extra job checks */ |
| 4523 | if (pi->num_cmds == 0) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4524 | if (G_interactive_fd) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4525 | goto check_jobs_and_continue; |
| 4526 | continue; |
| 4527 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4528 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4529 | /* After analyzing all keywords and conditions, we decided |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4530 | * to execute this pipe. NB: have to do checkjobs(NULL) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4531 | * after run_pipe to collect any background children, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4532 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4533 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4534 | { |
| 4535 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4536 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4537 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4538 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4539 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 4540 | if (r != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4541 | /* We ran a builtin, function, or group. |
| 4542 | * rcode is already known |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4543 | * and we don't need to wait for anything. */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4544 | G.last_exitcode = rcode; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4545 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4546 | check_and_run_traps(0); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4547 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4548 | /* Was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4549 | if (G.flag_break_continue) { |
| 4550 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4551 | /* We might fall into outer *loop*, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4552 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4553 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4554 | G.depth_break_continue--; |
| 4555 | if (G.depth_break_continue == 0) |
| 4556 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4557 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 4558 | } /* 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] | 4559 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4560 | goto check_jobs_and_break; |
| 4561 | /* "continue": simulate end of loop */ |
| 4562 | rword = RES_DONE; |
| 4563 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4564 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4565 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4566 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 673e945 | 2009-05-27 14:39:35 +0200 | [diff] [blame] | 4567 | if (G.flag_return_in_progress == 1) { |
| 4568 | /* same as "goto check_jobs_and_break" */ |
| 4569 | checkjobs(NULL); |
| 4570 | break; |
| 4571 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4572 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4573 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4574 | /* What does bash do with attempts to background builtins? */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4575 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4576 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 4577 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4578 | check_and_run_traps(0); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4579 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4580 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4581 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4582 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4583 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4584 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4585 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4586 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4587 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4588 | /* Waits for completion, then fg's main shell */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4589 | rcode = checkjobs_and_fg_shell(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4590 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4591 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4592 | } else |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4593 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4594 | { /* This one just waits for completion */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4595 | rcode = checkjobs(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4596 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4597 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4598 | } |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4599 | G.last_exitcode = rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4600 | } |
| 4601 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4602 | |
| 4603 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4604 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4605 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4606 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4607 | #endif |
| 4608 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4609 | /* Beware of "while false; true; do ..."! */ |
| 4610 | if (pi->next && pi->next->res_word == RES_DO) { |
| 4611 | if (rword == RES_WHILE) { |
| 4612 | if (rcode) { |
| 4613 | /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4614 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4615 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
| 4616 | goto check_jobs_and_break; |
| 4617 | } |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 4618 | } |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4619 | if (rword == RES_UNTIL) { |
| 4620 | if (!rcode) { |
| 4621 | debug_printf_exec(": until expr is true: breaking\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4622 | check_jobs_and_break: |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4623 | checkjobs(NULL); |
| 4624 | break; |
| 4625 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4626 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4627 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4628 | #endif |
Mike Frysinger | 8ec1c9d | 2009-03-29 00:45:26 +0000 | [diff] [blame] | 4629 | |
| 4630 | check_jobs_and_continue: |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 4631 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4632 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4633 | |
| 4634 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4635 | G.run_list_level--; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4636 | #endif |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4637 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4638 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4639 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4640 | free(for_list); |
| 4641 | #endif |
| 4642 | #if ENABLE_HUSH_CASE |
| 4643 | free(case_word); |
| 4644 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4645 | debug_leave(); |
| 4646 | 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] | 4647 | return rcode; |
| 4648 | } |
| 4649 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4650 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4651 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4652 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4653 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4654 | debug_printf_exec("run_and_free_list entered\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4655 | if (!G.fake_mode) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4656 | 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] | 4657 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4658 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4659 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4660 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4661 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4662 | free_pipe_list(pi); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 4663 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4664 | return rcode; |
| 4665 | } |
| 4666 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4667 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 4668 | static struct pipe *new_pipe(void) |
| 4669 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4670 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4671 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4672 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4673 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4674 | return pi; |
| 4675 | } |
| 4676 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4677 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 4678 | * if ctx->command is NULL. |
| 4679 | * No errors possible here. |
| 4680 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4681 | static int done_command(struct parse_context *ctx) |
| 4682 | { |
| 4683 | /* The command is really already in the pipe structure, so |
| 4684 | * advance the pipe counter and make a new, null command. */ |
| 4685 | struct pipe *pi = ctx->pipe; |
| 4686 | struct command *command = ctx->command; |
| 4687 | |
| 4688 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4689 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4690 | 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] | 4691 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4692 | } |
| 4693 | pi->num_cmds++; |
| 4694 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4695 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4696 | } else { |
| 4697 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 4698 | } |
| 4699 | |
| 4700 | /* Only real trickiness here is that the uncommitted |
| 4701 | * command structure is not counted in pi->num_cmds. */ |
| 4702 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4703 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 4704 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4705 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4706 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 4707 | } |
| 4708 | |
| 4709 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 4710 | { |
| 4711 | int not_null; |
| 4712 | |
| 4713 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 4714 | /* Close previous command */ |
| 4715 | not_null = done_command(ctx); |
| 4716 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4717 | #if HAS_KEYWORDS |
| 4718 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 4719 | ctx->ctx_inverted = 0; |
| 4720 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 4721 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4722 | |
| 4723 | /* Without this check, even just <enter> on command line generates |
| 4724 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4725 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4726 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4727 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4728 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4729 | #endif |
| 4730 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4731 | || ctx->ctx_res_w == RES_DONE |
| 4732 | || ctx->ctx_res_w == RES_FOR |
| 4733 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4734 | #endif |
| 4735 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4736 | || ctx->ctx_res_w == RES_ESAC |
| 4737 | #endif |
| 4738 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4739 | struct pipe *new_p; |
| 4740 | debug_printf_parse("done_pipe: adding new pipe: " |
| 4741 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 4742 | not_null, ctx->ctx_res_w); |
| 4743 | new_p = new_pipe(); |
| 4744 | ctx->pipe->next = new_p; |
| 4745 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4746 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4747 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4748 | * This is used to control execution. |
| 4749 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 4750 | * cases where variable or value happens to match a keyword): |
| 4751 | */ |
| 4752 | #if ENABLE_HUSH_LOOPS |
| 4753 | if (ctx->ctx_res_w == RES_FOR |
| 4754 | || ctx->ctx_res_w == RES_IN) |
| 4755 | ctx->ctx_res_w = RES_NONE; |
| 4756 | #endif |
| 4757 | #if ENABLE_HUSH_CASE |
| 4758 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4759 | ctx->ctx_res_w = RES_CASE_BODY; |
| 4760 | if (ctx->ctx_res_w == RES_CASE) |
| 4761 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4762 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4763 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4764 | /* Create the memory for command, roughly: |
| 4765 | * ctx->pipe->cmds = new struct command; |
| 4766 | * ctx->command = &ctx->pipe->cmds[0]; |
| 4767 | */ |
| 4768 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4769 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4770 | } |
| 4771 | debug_printf_parse("done_pipe return\n"); |
| 4772 | } |
| 4773 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4774 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4775 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4776 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4777 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4778 | /* Create the memory for command, roughly: |
| 4779 | * ctx->pipe->cmds = new struct command; |
| 4780 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4781 | */ |
| 4782 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4785 | /* If a reserved word is found and processed, parse context is modified |
| 4786 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4787 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4788 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4789 | struct reserved_combo { |
| 4790 | char literal[6]; |
| 4791 | unsigned char res; |
| 4792 | unsigned char assignment_flag; |
| 4793 | int flag; |
| 4794 | }; |
| 4795 | enum { |
| 4796 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4797 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4798 | FLAG_IF = (1 << RES_IF ), |
| 4799 | FLAG_THEN = (1 << RES_THEN ), |
| 4800 | FLAG_ELIF = (1 << RES_ELIF ), |
| 4801 | FLAG_ELSE = (1 << RES_ELSE ), |
| 4802 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4803 | # endif |
| 4804 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4805 | FLAG_FOR = (1 << RES_FOR ), |
| 4806 | FLAG_WHILE = (1 << RES_WHILE), |
| 4807 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 4808 | FLAG_DO = (1 << RES_DO ), |
| 4809 | FLAG_DONE = (1 << RES_DONE ), |
| 4810 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4811 | # endif |
| 4812 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4813 | FLAG_MATCH = (1 << RES_MATCH), |
| 4814 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4815 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4816 | FLAG_START = (1 << RES_XXXX ), |
| 4817 | }; |
| 4818 | |
| 4819 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 4820 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4821 | /* Mostly a list of accepted follow-up reserved words. |
| 4822 | * FLAG_END means we are done with the sequence, and are ready |
| 4823 | * to turn the compound list into a command. |
| 4824 | * FLAG_START means the word must start a new compound list. |
| 4825 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4826 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4827 | # if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4828 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 4829 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 4830 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 4831 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 4832 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 4833 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4834 | # endif |
| 4835 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4836 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 4837 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 4838 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 4839 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 4840 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 4841 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4842 | # endif |
| 4843 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4844 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 4845 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4846 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4847 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4848 | const struct reserved_combo *r; |
| 4849 | |
| 4850 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 4851 | if (strcmp(word->data, r->literal) == 0) |
| 4852 | return r; |
| 4853 | } |
| 4854 | return NULL; |
| 4855 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4856 | /* Return 0: not a keyword, 1: keyword |
| 4857 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4858 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 4859 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4860 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4861 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4862 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4863 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4864 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4865 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4866 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4867 | if (word->o_quoted) |
| 4868 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4869 | r = match_reserved_word(word); |
| 4870 | if (!r) |
| 4871 | return 0; |
| 4872 | |
| 4873 | 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] | 4874 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4875 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 4876 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4877 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4878 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4879 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4880 | if (r->flag == 0) { /* '!' */ |
| 4881 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4882 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4883 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4884 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4885 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4886 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4887 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4888 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4889 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4890 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4891 | old = xmalloc(sizeof(*old)); |
| 4892 | debug_printf_parse("push stack %p\n", old); |
| 4893 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4894 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4895 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4896 | } 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] | 4897 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4898 | ctx->ctx_res_w = RES_SNTX; |
| 4899 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4900 | } else { |
| 4901 | /* "{...} fi" is ok. "{...} if" is not |
| 4902 | * Example: |
| 4903 | * if { echo foo; } then { echo bar; } fi */ |
| 4904 | if (ctx->command->group) |
| 4905 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4906 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4907 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4908 | ctx->ctx_res_w = r->res; |
| 4909 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4910 | word->o_assignment = r->assignment_flag; |
| 4911 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4912 | if (ctx->old_flag & FLAG_END) { |
| 4913 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4914 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4915 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4916 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4917 | old = ctx->stack; |
| 4918 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4919 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4920 | # if !BB_MMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4921 | o_addstr(&old->as_string, ctx->as_string.data); |
| 4922 | o_free_unsafe(&ctx->as_string); |
| 4923 | old->command->group_as_string = xstrdup(old->as_string.data); |
| 4924 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 4925 | old->command->group_as_string); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4926 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4927 | *ctx = *old; /* physical copy */ |
| 4928 | free(old); |
| 4929 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4930 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4931 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4932 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4933 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4934 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4935 | * Normal return is 0. Syntax errors return 1. |
| 4936 | * Note: on return, word is reset, but not o_free'd! |
| 4937 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4938 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4939 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4940 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4941 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4942 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4943 | if (word->length == 0 && word->o_quoted == 0) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4944 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 4945 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4946 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4947 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4948 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4949 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 4950 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4951 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4952 | * "2.7 Redirection |
| 4953 | * ...the word that follows the redirection operator |
| 4954 | * shall be subjected to tilde expansion, parameter expansion, |
| 4955 | * command substitution, arithmetic expansion, and quote |
| 4956 | * removal. Pathname expansion shall not be performed |
| 4957 | * on the word by a non-interactive shell; an interactive |
| 4958 | * shell may perform it, but shall do so only when |
| 4959 | * the expansion would result in one word." |
| 4960 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4961 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4962 | /* Cater for >\file case: |
| 4963 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 4964 | * Same with heredocs: |
| 4965 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 4966 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4967 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 4968 | unbackslash(ctx->pending_redirect->rd_filename); |
| 4969 | /* Is it <<"HEREDOC"? */ |
| 4970 | if (word->o_quoted) { |
| 4971 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 4972 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4973 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4974 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4975 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4976 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4977 | /* If this word wasn't an assignment, next ones definitely |
| 4978 | * can't be assignments. Even if they look like ones. */ |
| 4979 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 4980 | && word->o_assignment != WORD_IS_KEYWORD |
| 4981 | ) { |
| 4982 | word->o_assignment = NOT_ASSIGNMENT; |
| 4983 | } else { |
| 4984 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
| 4985 | command->assignment_cnt++; |
| 4986 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 4987 | } |
| 4988 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4989 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4990 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 4991 | if (ctx->ctx_dsemicolon |
| 4992 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 4993 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 4994 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4995 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 4996 | ctx->ctx_dsemicolon = 0; |
| 4997 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4998 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4999 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5000 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5001 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 5002 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5003 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5004 | # if ENABLE_HUSH_CASE |
| 5005 | && ctx->ctx_res_w != RES_CASE |
| 5006 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5007 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5008 | debug_printf_parse("checking '%s' for reserved-ness\n", word->data); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 5009 | if (reserved_word(word, ctx)) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5010 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5011 | debug_printf_parse("done_word return %d\n", |
| 5012 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5013 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5014 | } |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5015 | # ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 5016 | if (strcmp(word->data, "export") == 0 |
| 5017 | # if ENABLE_HUSH_LOCAL |
| 5018 | || strcmp(word->data, "local") == 0 |
| 5019 | # endif |
| 5020 | ) { |
| 5021 | command->cmd_type = CMD_SINGLEWORD_NOGLOB_COND; |
| 5022 | } else |
| 5023 | # endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 5024 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5025 | if (strcmp(word->data, "[[") == 0) { |
| 5026 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 5027 | } |
| 5028 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 5029 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5030 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5031 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5032 | if (command->group) { |
| 5033 | /* "{ echo foo; } echo bar" - bad */ |
| 5034 | syntax_error_at(word->data); |
| 5035 | debug_printf_parse("done_word return 1: syntax error, " |
| 5036 | "groups and arglists don't mix\n"); |
| 5037 | return 1; |
| 5038 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5039 | 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] | 5040 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 5041 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5042 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 5043 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5044 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 5045 | char *p = word->data; |
| 5046 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 5047 | && (p[1] & 0x7f) == '@' |
| 5048 | && p[2] == SPECIAL_VAR_SYMBOL |
| 5049 | ) { |
| 5050 | p += 3; |
| 5051 | } |
| 5052 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5053 | /* saw no "$@", or not only "$@" but some |
| 5054 | * real text is there too */ |
| 5055 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 5056 | * e.g. "", $empty"" etc to not disappear */ |
| 5057 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 5058 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 5059 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 5060 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 5061 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5062 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5063 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5064 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 5065 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5066 | if (ctx->ctx_res_w == RES_FOR) { |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5067 | if (word->o_quoted |
| 5068 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 5069 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5070 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5071 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5072 | return 1; |
| 5073 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5074 | /* Force FOR to have just one word (variable name) */ |
| 5075 | /* NB: basically, this makes hush see "for v in ..." |
| 5076 | * syntax as if it is "for v; in ...". FOR and IN become |
| 5077 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 5078 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5079 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 5080 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5081 | #if ENABLE_HUSH_CASE |
| 5082 | /* Force CASE to have just one word */ |
| 5083 | if (ctx->ctx_res_w == RES_CASE) { |
| 5084 | done_pipe(ctx, PIPE_SEQ); |
| 5085 | } |
| 5086 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5087 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5088 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5089 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5090 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5091 | return 0; |
| 5092 | } |
| 5093 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5094 | |
| 5095 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 5096 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5097 | * Return: |
| 5098 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 5099 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 5100 | * REDIRFD_TO_FILE if no & was seen, |
| 5101 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5102 | */ |
| 5103 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5104 | #define parse_redir_right_fd(as_string, input) \ |
| 5105 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5106 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5107 | 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] | 5108 | { |
| 5109 | int ch, d, ok; |
| 5110 | |
| 5111 | ch = i_peek(input); |
| 5112 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5113 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5114 | |
| 5115 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5116 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5117 | ch = i_peek(input); |
| 5118 | if (ch == '-') { |
| 5119 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5120 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5121 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5122 | } |
| 5123 | d = 0; |
| 5124 | ok = 0; |
| 5125 | while (ch != EOF && isdigit(ch)) { |
| 5126 | d = d*10 + (ch-'0'); |
| 5127 | ok = 1; |
| 5128 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5129 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5130 | ch = i_peek(input); |
| 5131 | } |
| 5132 | if (ok) return d; |
| 5133 | |
| 5134 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 5135 | |
| 5136 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5137 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5138 | } |
| 5139 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5140 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5141 | */ |
| 5142 | static int parse_redirect(struct parse_context *ctx, |
| 5143 | int fd, |
| 5144 | redir_type style, |
| 5145 | struct in_str *input) |
| 5146 | { |
| 5147 | struct command *command = ctx->command; |
| 5148 | struct redir_struct *redir; |
| 5149 | struct redir_struct **redirp; |
| 5150 | int dup_num; |
| 5151 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5152 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5153 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5154 | /* Check for a '>&1' type redirect */ |
| 5155 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 5156 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 5157 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5158 | } else { |
| 5159 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5160 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5161 | if (dup_num) { /* <<-... */ |
| 5162 | ch = i_getch(input); |
| 5163 | nommu_addchr(&ctx->as_string, ch); |
| 5164 | ch = i_peek(input); |
| 5165 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5166 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5167 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5168 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5169 | int ch = i_peek(input); |
| 5170 | if (ch == '|') { |
| 5171 | /* >|FILE redirect ("clobbering" >). |
| 5172 | * Since we do not support "set -o noclobber" yet, |
| 5173 | * >| and > are the same for now. Just eat |. |
| 5174 | */ |
| 5175 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5176 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5177 | } |
| 5178 | } |
| 5179 | |
| 5180 | /* Create a new redir_struct and append it to the linked list */ |
| 5181 | redirp = &command->redirects; |
| 5182 | while ((redir = *redirp) != NULL) { |
| 5183 | redirp = &(redir->next); |
| 5184 | } |
| 5185 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 5186 | /* redir->next = NULL; */ |
| 5187 | /* redir->rd_filename = NULL; */ |
| 5188 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5189 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5190 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5191 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 5192 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5193 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5194 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5195 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5196 | /* Erik had a check here that the file descriptor in question |
| 5197 | * is legit; I postpone that to "run time" |
| 5198 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5199 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 5200 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5201 | } else { |
| 5202 | /* Set ctx->pending_redirect, so we know what to do at the |
| 5203 | * end of the next parsed word. */ |
| 5204 | ctx->pending_redirect = redir; |
| 5205 | } |
| 5206 | return 0; |
| 5207 | } |
| 5208 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5209 | /* If a redirect is immediately preceded by a number, that number is |
| 5210 | * supposed to tell which file descriptor to redirect. This routine |
| 5211 | * looks for such preceding numbers. In an ideal world this routine |
| 5212 | * needs to handle all the following classes of redirects... |
| 5213 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 5214 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 5215 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 5216 | * 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] | 5217 | * |
| 5218 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 5219 | * "2.7 Redirection |
| 5220 | * ... If n is quoted, the number shall not be recognized as part of |
| 5221 | * the redirection expression. For example: |
| 5222 | * echo \2>a |
| 5223 | * writes the character 2 into file a" |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5224 | * We are getting it right by setting ->o_quoted on any \<char> |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5225 | * |
| 5226 | * A -1 return means no valid number was found, |
| 5227 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5228 | */ |
| 5229 | static int redirect_opt_num(o_string *o) |
| 5230 | { |
| 5231 | int num; |
| 5232 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5233 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5234 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5235 | num = bb_strtou(o->data, NULL, 10); |
| 5236 | if (errno || num < 0) |
| 5237 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5238 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5239 | return num; |
| 5240 | } |
| 5241 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5242 | #if BB_MMU |
| 5243 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 5244 | fetch_till_str(input, word, skip_tabs) |
| 5245 | #endif |
| 5246 | static char *fetch_till_str(o_string *as_string, |
| 5247 | struct in_str *input, |
| 5248 | const char *word, |
| 5249 | int skip_tabs) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5250 | { |
| 5251 | o_string heredoc = NULL_O_STRING; |
| 5252 | int past_EOL = 0; |
| 5253 | int ch; |
| 5254 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5255 | goto jump_in; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5256 | while (1) { |
| 5257 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5258 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5259 | if (ch == '\n') { |
| 5260 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 5261 | heredoc.data[past_EOL] = '\0'; |
| 5262 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 5263 | return heredoc.data; |
| 5264 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5265 | do { |
| 5266 | o_addchr(&heredoc, ch); |
| 5267 | past_EOL = heredoc.length; |
| 5268 | jump_in: |
| 5269 | do { |
| 5270 | ch = i_getch(input); |
| 5271 | nommu_addchr(as_string, ch); |
| 5272 | } while (skip_tabs && ch == '\t'); |
| 5273 | } while (ch == '\n'); |
| 5274 | } |
| 5275 | if (ch == EOF) { |
| 5276 | o_free_unsafe(&heredoc); |
| 5277 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5278 | } |
| 5279 | o_addchr(&heredoc, ch); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5280 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5281 | } |
| 5282 | } |
| 5283 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5284 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 5285 | * and load them all. There should be exactly heredoc_cnt of them. |
| 5286 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5287 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 5288 | { |
| 5289 | struct pipe *pi = ctx->list_head; |
| 5290 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5291 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5292 | int i; |
| 5293 | struct command *cmd = pi->cmds; |
| 5294 | |
| 5295 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 5296 | pi->num_cmds, |
| 5297 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 5298 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5299 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5300 | |
| 5301 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 5302 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5303 | while (redir) { |
| 5304 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5305 | char *p; |
| 5306 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5307 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5308 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5309 | p = fetch_till_str(&ctx->as_string, input, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5310 | redir->rd_filename, redir->rd_dup & HEREDOC_SKIPTABS); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5311 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5312 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5313 | return 1; |
| 5314 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5315 | free(redir->rd_filename); |
| 5316 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5317 | heredoc_cnt--; |
| 5318 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5319 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5320 | } |
| 5321 | cmd++; |
| 5322 | } |
| 5323 | pi = pi->next; |
| 5324 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5325 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5326 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5327 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5328 | bb_error_msg_and_die("heredoc BUG 2"); |
| 5329 | #endif |
| 5330 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5331 | } |
| 5332 | |
| 5333 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5334 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5335 | 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] | 5336 | { |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5337 | pid_t pid; |
| 5338 | int channel[2]; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5339 | # if !BB_MMU |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5340 | char **to_free; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5341 | # endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 5342 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 5343 | xpipe(channel); |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 5344 | pid = BB_MMU ? fork() : vfork(); |
| 5345 | if (pid < 0) |
| 5346 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5347 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5348 | if (pid == 0) { /* child */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5349 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 5350 | /* Process substitution is not considered to be usual |
| 5351 | * 'command execution'. |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 5352 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 5353 | */ |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 5354 | bb_signals(0 |
| 5355 | + (1 << SIGTSTP) |
| 5356 | + (1 << SIGTTIN) |
| 5357 | + (1 << SIGTTOU) |
| 5358 | , SIG_IGN); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 5359 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5360 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 5361 | xmove_fd(channel[1], 1); |
| 5362 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 5363 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denys Vlasenko | 91836ba | 2009-09-23 01:46:19 +0200 | [diff] [blame] | 5364 | /* Awful hack for `trap` or $(trap). |
| 5365 | * |
| 5366 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 5367 | * contains an example where "trap" is executed in a subshell: |
| 5368 | * |
| 5369 | * save_traps=$(trap) |
| 5370 | * ... |
| 5371 | * eval "$save_traps" |
| 5372 | * |
| 5373 | * Standard does not say that "trap" in subshell shall print |
| 5374 | * parent shell's traps. It only says that its output |
| 5375 | * must have suitable form, but then, in the above example |
| 5376 | * (which is not supposed to be normative), it implies that. |
| 5377 | * |
| 5378 | * bash (and probably other shell) does implement it |
| 5379 | * (traps are reset to defaults, but "trap" still shows them), |
| 5380 | * but as a result, "trap" logic is hopelessly messed up: |
| 5381 | * |
| 5382 | * # trap |
| 5383 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 5384 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 5385 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 5386 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 5387 | * trap -- 'echo Ho' SIGWINCH |
| 5388 | * # echo `(trap)` <--- in subshell in subshell - output |
| 5389 | * trap -- 'echo Ho' SIGWINCH |
| 5390 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 5391 | * trap -- 'echo Ho' SIGWINCH |
| 5392 | * |
| 5393 | * The rules when to forget and when to not forget traps |
| 5394 | * get really complex and nonsensical. |
| 5395 | * |
| 5396 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 5397 | */ |
| 5398 | s = skip_whitespace(s); |
| 5399 | if (strncmp(s, "trap", 4) == 0 && (*skip_whitespace(s + 4) == '\0')) |
| 5400 | { |
| 5401 | static const char *const argv[] = { NULL, NULL }; |
| 5402 | builtin_trap((char**)argv); |
| 5403 | exit(0); /* not _exit() - we need to fflush */ |
| 5404 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5405 | # if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5406 | reset_traps_to_defaults(); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5407 | parse_and_run_string(s); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5408 | _exit(G.last_exitcode); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5409 | # else |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5410 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5411 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 5412 | * huge=`cat BIG` # was blocking here forever |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5413 | * echo OK |
| 5414 | */ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5415 | re_execute_shell(&to_free, |
| 5416 | s, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5417 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 5418 | G.global_argv + 1, |
| 5419 | NULL); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5420 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5421 | } |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5422 | |
| 5423 | /* parent */ |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5424 | *pid_p = pid; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5425 | # if ENABLE_HUSH_FAST |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 5426 | G.count_SIGCHLD++; |
| 5427 | //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] | 5428 | # endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5429 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5430 | # if !BB_MMU |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5431 | free(to_free); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5432 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5433 | close(channel[1]); |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5434 | close_on_exec_on(channel[0]); |
| 5435 | return xfdopen_for_read(channel[0]); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5436 | } |
| 5437 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 5438 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5439 | static int process_command_subs(o_string *dest, const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5440 | { |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5441 | FILE *fp; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5442 | struct in_str pipe_str; |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5443 | pid_t pid; |
| 5444 | int status, ch, eol_cnt; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5445 | |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5446 | fp = generate_stream_from_string(s, &pid); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5447 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5448 | /* Now send results of command back into original context */ |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5449 | setup_file_in_str(&pipe_str, fp); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5450 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5451 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5452 | if (ch == '\n') { |
| 5453 | eol_cnt++; |
| 5454 | continue; |
| 5455 | } |
| 5456 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5457 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5458 | eol_cnt--; |
| 5459 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 5460 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5461 | } |
| 5462 | |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5463 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5464 | fclose(fp); |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5465 | /* We need to extract exitcode. Test case |
| 5466 | * "true; echo `sleep 1; false` $?" |
| 5467 | * should print 1 */ |
| 5468 | safe_waitpid(pid, &status, 0); |
| 5469 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 5470 | return WEXITSTATUS(status); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5471 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5472 | #endif /* ENABLE_HUSH_TICK */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5473 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5474 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5475 | struct in_str *input, int ch) |
| 5476 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5477 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5478 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5479 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5480 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5481 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5482 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5483 | |
| 5484 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5485 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5486 | if (ch == '(' && !dest->o_quoted) { |
| 5487 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5488 | if (done_word(dest, ctx)) |
| 5489 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5490 | if (!command->argv) |
| 5491 | goto skip; /* (... */ |
| 5492 | if (command->argv[1]) { /* word word ... (... */ |
| 5493 | syntax_error_unexpected_ch('('); |
| 5494 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5495 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5496 | /* it is "word(..." or "word (..." */ |
| 5497 | do |
| 5498 | ch = i_getch(input); |
| 5499 | while (ch == ' ' || ch == '\t'); |
| 5500 | if (ch != ')') { |
| 5501 | syntax_error_unexpected_ch(ch); |
| 5502 | return 1; |
| 5503 | } |
| 5504 | nommu_addchr(&ctx->as_string, ch); |
| 5505 | do |
| 5506 | ch = i_getch(input); |
| 5507 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 5508 | if (ch != '{') { |
| 5509 | syntax_error_unexpected_ch(ch); |
| 5510 | return 1; |
| 5511 | } |
| 5512 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5513 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5514 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5515 | } |
| 5516 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5517 | |
| 5518 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5519 | if (command->argv /* word [word]{... */ |
| 5520 | || dest->length /* word{... */ |
| 5521 | || dest->o_quoted /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5522 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5523 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5524 | debug_printf_parse("parse_group return 1: " |
| 5525 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5526 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5527 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5528 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5529 | |
| 5530 | #if ENABLE_HUSH_FUNCTIONS |
| 5531 | skip: |
| 5532 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5533 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 5534 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5535 | endch = ')'; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5536 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5537 | } else { |
| 5538 | /* bash does not allow "{echo...", requires whitespace */ |
| 5539 | ch = i_getch(input); |
| 5540 | if (ch != ' ' && ch != '\t' && ch != '\n') { |
| 5541 | syntax_error_unexpected_ch(ch); |
| 5542 | return 1; |
| 5543 | } |
| 5544 | nommu_addchr(&ctx->as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5545 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5546 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5547 | { |
| 5548 | #if !BB_MMU |
| 5549 | char *as_string = NULL; |
| 5550 | #endif |
| 5551 | pipe_list = parse_stream(&as_string, input, endch); |
| 5552 | #if !BB_MMU |
| 5553 | if (as_string) |
| 5554 | o_addstr(&ctx->as_string, as_string); |
| 5555 | #endif |
| 5556 | /* empty ()/{} or parse error? */ |
| 5557 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5558 | /* parse_stream already emitted error msg */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5559 | #if !BB_MMU |
| 5560 | free(as_string); |
| 5561 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5562 | debug_printf_parse("parse_group return 1: " |
| 5563 | "parse_stream returned %p\n", pipe_list); |
| 5564 | return 1; |
| 5565 | } |
| 5566 | command->group = pipe_list; |
| 5567 | #if !BB_MMU |
| 5568 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 5569 | command->group_as_string = as_string; |
| 5570 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 5571 | command->group_as_string); |
| 5572 | #endif |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5573 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5574 | debug_printf_parse("parse_group return 0\n"); |
| 5575 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5576 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5577 | } |
| 5578 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5579 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5580 | /* Subroutines for copying $(...) and `...` things */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5581 | static void add_till_backquote(o_string *dest, struct in_str *input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5582 | /* '...' */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5583 | 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] | 5584 | { |
| 5585 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5586 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5587 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5588 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5589 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5590 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5591 | if (ch == '\'') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5592 | return; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5593 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5594 | } |
| 5595 | } |
| 5596 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5597 | 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] | 5598 | { |
| 5599 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5600 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5601 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5602 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5603 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5604 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5605 | if (ch == '"') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5606 | return; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5607 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5608 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5609 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5610 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5611 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5612 | if (ch == '`') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5613 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5614 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5615 | continue; |
| 5616 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 5617 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5618 | } |
| 5619 | } |
| 5620 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 5621 | * \` quoting. |
| 5622 | * "Within the backquoted style of command substitution, backslash |
| 5623 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 5624 | * The search for the matching backquote shall be satisfied by the first |
| 5625 | * backquote found without a preceding backslash; during this search, |
| 5626 | * if a non-escaped backquote is encountered within a shell comment, |
| 5627 | * a here-document, an embedded command substitution of the $(command) |
| 5628 | * form, or a quoted string, undefined results occur. A single-quoted |
| 5629 | * or double-quoted string that begins, but does not end, within the |
| 5630 | * "`...`" sequence produces undefined results." |
| 5631 | * Example Output |
| 5632 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 5633 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5634 | static void add_till_backquote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5635 | { |
| 5636 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5637 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5638 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5639 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5640 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5641 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5642 | if (ch == '`') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5643 | return; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5644 | if (ch == '\\') { |
| 5645 | /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5646 | int ch2 = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5647 | if (ch2 == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5648 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5649 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5650 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5651 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5652 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5653 | ch = ch2; |
| 5654 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5655 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5656 | } |
| 5657 | } |
| 5658 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 5659 | * quoting and nested ()s. |
| 5660 | * "With the $(command) style of command substitution, all characters |
| 5661 | * following the open parenthesis to the matching closing parenthesis |
| 5662 | * constitute the command. Any valid shell script can be used for command, |
| 5663 | * except a script consisting solely of redirections which produces |
| 5664 | * unspecified results." |
| 5665 | * Example Output |
| 5666 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 5667 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 5668 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 5669 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5670 | 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] | 5671 | { |
| 5672 | int count = 0; |
| 5673 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5674 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5675 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5676 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5677 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5678 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5679 | if (ch == '(') |
| 5680 | count++; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5681 | if (ch == ')') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5682 | if (--count < 0) { |
| 5683 | if (!dbl) |
| 5684 | break; |
| 5685 | if (i_peek(input) == ')') { |
| 5686 | i_getch(input); |
| 5687 | break; |
| 5688 | } |
| 5689 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5690 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5691 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5692 | if (ch == '\'') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5693 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5694 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5695 | continue; |
| 5696 | } |
| 5697 | if (ch == '"') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5698 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5699 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5700 | continue; |
| 5701 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5702 | if (ch == '\\') { |
| 5703 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5704 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5705 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5706 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5707 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5708 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5709 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5710 | continue; |
| 5711 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5712 | } |
| 5713 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5714 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5715 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5716 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5717 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5718 | #define handle_dollar(as_string, dest, input) \ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5719 | handle_dollar(dest, input) |
| 5720 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5721 | static int handle_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5722 | o_string *dest, |
| 5723 | struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5724 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5725 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5726 | unsigned char quote_mask = dest->o_escape ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5727 | |
| 5728 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5729 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5730 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5731 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 5732 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5733 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5734 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5735 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5736 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5737 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5738 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5739 | if (!isalnum(ch) && ch != '_') |
| 5740 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5741 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5742 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5743 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5744 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5745 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5746 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5747 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5748 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5749 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5750 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5751 | o_addchr(dest, ch | quote_mask); |
| 5752 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5753 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5754 | case '$': /* pid */ |
| 5755 | case '!': /* last bg pid */ |
| 5756 | case '?': /* last exit code */ |
| 5757 | case '#': /* number of args */ |
| 5758 | case '*': /* args */ |
| 5759 | case '@': /* args */ |
| 5760 | goto make_one_char_var; |
| 5761 | case '{': { |
| 5762 | bool first_char, all_digits; |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 5763 | int expansion; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 5764 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5765 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5766 | nommu_addchr(as_string, ch); |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 5767 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5768 | |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 5769 | /* TODO: maybe someone will try to escape the '}' */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5770 | expansion = 0; |
| 5771 | first_char = true; |
| 5772 | all_digits = false; |
| 5773 | while (1) { |
| 5774 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5775 | nommu_addchr(as_string, ch); |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5776 | if (ch == '}') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5777 | break; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5778 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5779 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5780 | if (first_char) { |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5781 | if (ch == '#') { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5782 | /* ${#var}: length of var contents */ |
| 5783 | goto char_ok; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5784 | } |
| 5785 | if (isdigit(ch)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5786 | all_digits = true; |
| 5787 | goto char_ok; |
| 5788 | } |
Mike Frysinger | dc3bc40 | 2009-06-01 14:09:09 -0400 | [diff] [blame] | 5789 | /* They're being verbose and doing ${?} */ |
| 5790 | if (i_peek(input) == '}' && strchr("$!?#*@_", ch)) |
| 5791 | goto char_ok; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5792 | } |
| 5793 | |
| 5794 | if (expansion < 2 |
| 5795 | && ( (all_digits && !isdigit(ch)) |
| 5796 | || (!all_digits && !isalnum(ch) && ch != '_') |
| 5797 | ) |
| 5798 | ) { |
| 5799 | /* handle parameter expansions |
| 5800 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 5801 | */ |
| 5802 | if (first_char) |
| 5803 | goto case_default; |
| 5804 | switch (ch) { |
| 5805 | case ':': /* null modifier */ |
| 5806 | if (expansion == 0) { |
| 5807 | debug_printf_parse(": null modifier\n"); |
| 5808 | ++expansion; |
| 5809 | break; |
| 5810 | } |
| 5811 | goto case_default; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5812 | case '#': /* remove prefix */ |
| 5813 | case '%': /* remove suffix */ |
| 5814 | if (expansion == 0) { |
| 5815 | debug_printf_parse(": remove suffix/prefix\n"); |
| 5816 | expansion = 2; |
| 5817 | break; |
| 5818 | } |
| 5819 | goto case_default; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5820 | case '-': /* default value */ |
| 5821 | case '=': /* assign default */ |
| 5822 | case '+': /* alternative */ |
| 5823 | case '?': /* error indicate */ |
| 5824 | debug_printf_parse(": parameter expansion\n"); |
| 5825 | expansion = 2; |
| 5826 | break; |
| 5827 | default: |
| 5828 | case_default: |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5829 | syntax_error_unterm_str("${name}"); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5830 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 5831 | return 1; |
| 5832 | } |
| 5833 | } |
| 5834 | char_ok: |
| 5835 | debug_printf_parse(": '%c'\n", ch); |
| 5836 | o_addchr(dest, ch | quote_mask); |
| 5837 | quote_mask = 0; |
| 5838 | first_char = false; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5839 | } /* while (1) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5840 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5841 | break; |
| 5842 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5843 | #if ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5844 | case '(': { |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5845 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5846 | int pos; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5847 | # endif |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5848 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5849 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5850 | # if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5851 | if (i_peek(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5852 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5853 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5854 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5855 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5856 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5857 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5858 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5859 | add_till_closing_paren(dest, input, true); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5860 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5861 | if (as_string) { |
| 5862 | o_addstr(as_string, dest->data + pos); |
| 5863 | o_addchr(as_string, ')'); |
| 5864 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5865 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5866 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5867 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5868 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5869 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5870 | # endif |
| 5871 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5872 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5873 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5874 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5875 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5876 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5877 | add_till_closing_paren(dest, input, false); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5878 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5879 | if (as_string) { |
| 5880 | o_addstr(as_string, dest->data + pos); |
| 5881 | o_addchr(as_string, '`'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5882 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5883 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5884 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5885 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5886 | break; |
| 5887 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5888 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5889 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5890 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5891 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5892 | ch = i_peek(input); |
| 5893 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 5894 | ch = '_'; |
| 5895 | goto make_var; |
| 5896 | } |
| 5897 | /* else: it's $_ */ |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 5898 | /* TODO: $_ and $-: */ |
| 5899 | /* $_ Shell or shell script name; or last argument of last command |
| 5900 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 5901 | * 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] | 5902 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 5903 | default: |
| 5904 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5905 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5906 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5907 | return 0; |
| 5908 | } |
| 5909 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5910 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5911 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5912 | parse_stream_dquoted(dest, input, dquote_end) |
| 5913 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5914 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5915 | o_string *dest, |
| 5916 | struct in_str *input, |
| 5917 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5918 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5919 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5920 | int next; |
| 5921 | |
| 5922 | again: |
| 5923 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5924 | if (ch != EOF) |
| 5925 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5926 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5927 | if (dest->o_assignment == NOT_ASSIGNMENT) |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5928 | dest->o_escape ^= 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5929 | debug_printf_parse("parse_stream_dquoted return 0\n"); |
| 5930 | return 0; |
| 5931 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5932 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5933 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5934 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5935 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5936 | } |
| 5937 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5938 | if (ch != '\n') { |
| 5939 | next = i_peek(input); |
| 5940 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 5941 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5942 | ch, ch, dest->o_escape); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5943 | if (ch == '\\') { |
| 5944 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5945 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5946 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5947 | } |
| 5948 | /* bash: |
| 5949 | * "The backslash retains its special meaning [in "..."] |
| 5950 | * only when followed by one of the following characters: |
| 5951 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 5952 | * within double quotes by preceding it with a backslash." |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5953 | */ |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 5954 | if (strchr("$`\"\\\n", next) != NULL) { |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 5955 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 5956 | if (ch != '\n') { |
| 5957 | o_addqchr(dest, ch); |
| 5958 | nommu_addchr(as_string, ch); |
| 5959 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5960 | } else { |
| 5961 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 5962 | nommu_addchr(as_string, '\\'); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5963 | } |
| 5964 | goto again; |
| 5965 | } |
| 5966 | if (ch == '$') { |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5967 | if (handle_dollar(as_string, dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5968 | debug_printf_parse("parse_stream_dquoted return 1: " |
| 5969 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5970 | return 1; |
| 5971 | } |
| 5972 | goto again; |
| 5973 | } |
| 5974 | #if ENABLE_HUSH_TICK |
| 5975 | if (ch == '`') { |
| 5976 | //int pos = dest->length; |
| 5977 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5978 | o_addchr(dest, 0x80 | '`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5979 | add_till_backquote(dest, input); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5980 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5981 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5982 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5983 | } |
| 5984 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5985 | o_addQchr(dest, ch); |
| 5986 | if (ch == '=' |
| 5987 | && (dest->o_assignment == MAYBE_ASSIGNMENT |
| 5988 | || dest->o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5989 | && is_well_formed_var_name(dest->data, '=') |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5990 | ) { |
| 5991 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 5992 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5993 | goto again; |
| 5994 | } |
| 5995 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5996 | /* |
| 5997 | * Scan input until EOF or end_trigger char. |
| 5998 | * Return a list of pipes to execute, or NULL on EOF |
| 5999 | * or if end_trigger character is met. |
| 6000 | * On syntax error, exit is shell is not interactive, |
| 6001 | * reset parsing machinery and start parsing anew, |
| 6002 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6003 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6004 | static struct pipe *parse_stream(char **pstring, |
| 6005 | struct in_str *input, |
| 6006 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6007 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6008 | struct parse_context ctx; |
| 6009 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6010 | int is_in_dquote; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6011 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6012 | |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 6013 | /* Double-quote state is handled in the state variable is_in_dquote. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6014 | * 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] | 6015 | * found. When recursing, quote state is passed in via dest->o_escape. |
| 6016 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6017 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 6018 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6019 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6020 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 6021 | /* If very first arg is "" or '', dest.data may end up NULL. |
| 6022 | * Preventing this: */ |
| 6023 | o_addchr(&dest, '\0'); |
| 6024 | dest.length = 0; |
| 6025 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6026 | G.ifs = get_local_var_value("IFS"); |
| 6027 | if (G.ifs == NULL) |
| 6028 | G.ifs = " \t\n"; |
| 6029 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6030 | reset: |
| 6031 | #if ENABLE_HUSH_INTERACTIVE |
| 6032 | input->promptmode = 0; /* PS1 */ |
| 6033 | #endif |
| 6034 | /* dest.o_assignment = MAYBE_ASSIGNMENT; - already is */ |
| 6035 | initialize_context(&ctx); |
| 6036 | is_in_dquote = 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6037 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 6038 | while (1) { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6039 | const char *is_ifs; |
| 6040 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6041 | int ch; |
| 6042 | int next; |
| 6043 | int redir_fd; |
| 6044 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6045 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6046 | if (is_in_dquote) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 6047 | /* dest.o_quoted = 1; - already is (see below) */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6048 | if (parse_stream_dquoted(&ctx.as_string, &dest, input, '"')) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6049 | goto parse_error; |
| 6050 | } |
| 6051 | /* We reached closing '"' */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6052 | is_in_dquote = 0; |
| 6053 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6054 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6055 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 6056 | ch, ch, dest.o_escape); |
| 6057 | if (ch == EOF) { |
| 6058 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6059 | |
| 6060 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6061 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 6062 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6063 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 6064 | /* end_trigger == '}' case errors out earlier, |
| 6065 | * checking only ')' */ |
| 6066 | if (end_trigger == ')') { |
| 6067 | syntax_error_unterm_ch('('); /* exits */ |
| 6068 | /* goto parse_error; */ |
| 6069 | } |
| 6070 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6071 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 6072 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6073 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6074 | o_free(&dest); |
| 6075 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6076 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6077 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6078 | /* (this makes bare "&" cmd a no-op. |
| 6079 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6080 | if (pi->num_cmds == 0 |
| 6081 | IF_HAS_KEYWORDS( && pi->res_word == RES_NONE) |
| 6082 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6083 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6084 | pi = NULL; |
| 6085 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6086 | #if !BB_MMU |
| 6087 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 6088 | if (pstring) |
| 6089 | *pstring = ctx.as_string.data; |
| 6090 | else |
| 6091 | o_free_unsafe(&ctx.as_string); |
| 6092 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6093 | debug_leave(); |
| 6094 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6095 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 6096 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6097 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 6098 | |
| 6099 | next = '\0'; |
| 6100 | if (ch != '\n') |
| 6101 | next = i_peek(input); |
| 6102 | |
| 6103 | is_special = "{}<>;&|()#'" /* special outside of "str" */ |
| 6104 | "\\$\"" IF_HUSH_TICK("`"); /* always special */ |
| 6105 | /* Are { and } special here? */ |
| 6106 | if (ctx.command->argv /* word [word]{... */ |
| 6107 | || dest.length /* word{... */ |
| 6108 | || dest.o_quoted /* ""{... */ |
| 6109 | || (next != ';' && next != ')' && !strchr(G.ifs, next)) /* {word */ |
| 6110 | ) { |
| 6111 | /* They are not special, skip "{}" */ |
| 6112 | is_special += 2; |
| 6113 | } |
| 6114 | is_special = strchr(is_special, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6115 | is_ifs = strchr(G.ifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6116 | |
| 6117 | if (!is_special && !is_ifs) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 6118 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6119 | o_addQchr(&dest, ch); |
| 6120 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 6121 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6122 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6123 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6124 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6125 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6126 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6127 | continue; |
| 6128 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6129 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6130 | if (is_ifs) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6131 | if (done_word(&dest, &ctx)) { |
| 6132 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 6133 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 6134 | if (ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6135 | #if ENABLE_HUSH_CASE |
| 6136 | /* "case ... in <newline> word) ..." - |
| 6137 | * newlines are ignored (but ';' wouldn't be) */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6138 | if (ctx.command->argv == NULL |
| 6139 | && ctx.ctx_res_w == RES_MATCH |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6140 | ) { |
| 6141 | continue; |
| 6142 | } |
| 6143 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6144 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6145 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6146 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 6147 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 6148 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6149 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 6150 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6151 | heredoc_cnt = 0; |
| 6152 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6153 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6154 | ch = ';'; |
Denis Vlasenko | 7c98612 | 2009-04-04 12:15:42 +0000 | [diff] [blame] | 6155 | /* note: if (is_ifs) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6156 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6157 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6158 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 6159 | |
| 6160 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 6161 | * } is an ordinary char in this case, even inside { cmd; } |
| 6162 | * Pathological example: { ""}; } should exec "}" cmd |
| 6163 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 6164 | if (ch == '}') { |
| 6165 | if (!IS_NULL_CMD(ctx.command) /* cmd } */ |
| 6166 | || dest.length != 0 /* word} */ |
| 6167 | || dest.o_quoted /* ""} */ |
| 6168 | ) { |
| 6169 | goto ordinary_char; |
| 6170 | } |
| 6171 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
| 6172 | goto skip_end_trigger; |
| 6173 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 6174 | } |
| 6175 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6176 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 6177 | && (ch != ';' || heredoc_cnt == 0) |
| 6178 | #if ENABLE_HUSH_CASE |
| 6179 | && (ch != ')' |
| 6180 | || ctx.ctx_res_w != RES_MATCH |
| 6181 | || (!dest.o_quoted && strcmp(dest.data, "esac") == 0) |
| 6182 | ) |
| 6183 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6184 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6185 | if (heredoc_cnt) { |
| 6186 | /* This is technically valid: |
| 6187 | * { cat <<HERE; }; echo Ok |
| 6188 | * heredoc |
| 6189 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6190 | * HERE |
| 6191 | * but we don't support this. |
| 6192 | * We require heredoc to be in enclosing {}/(), |
| 6193 | * if any. |
| 6194 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6195 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6196 | goto parse_error; |
| 6197 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6198 | if (done_word(&dest, &ctx)) { |
| 6199 | goto parse_error; |
| 6200 | } |
| 6201 | done_pipe(&ctx, PIPE_SEQ); |
| 6202 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6203 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 6204 | if (!HAS_KEYWORDS |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6205 | 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] | 6206 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6207 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6208 | #if !BB_MMU |
| 6209 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 6210 | if (pstring) |
| 6211 | *pstring = ctx.as_string.data; |
| 6212 | else |
| 6213 | o_free_unsafe(&ctx.as_string); |
| 6214 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6215 | debug_leave(); |
| 6216 | debug_printf_parse("parse_stream return %p: " |
| 6217 | "end_trigger char found\n", |
| 6218 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6219 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6220 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6221 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 6222 | skip_end_trigger: |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6223 | if (is_ifs) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6224 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6225 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 6226 | /* Catch <, > before deciding whether this word is |
| 6227 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 6228 | switch (ch) { |
| 6229 | case '>': |
| 6230 | redir_fd = redirect_opt_num(&dest); |
| 6231 | if (done_word(&dest, &ctx)) { |
| 6232 | goto parse_error; |
| 6233 | } |
| 6234 | redir_style = REDIRECT_OVERWRITE; |
| 6235 | if (next == '>') { |
| 6236 | redir_style = REDIRECT_APPEND; |
| 6237 | ch = i_getch(input); |
| 6238 | nommu_addchr(&ctx.as_string, ch); |
| 6239 | } |
| 6240 | #if 0 |
| 6241 | else if (next == '(') { |
| 6242 | syntax_error(">(process) not supported"); |
| 6243 | goto parse_error; |
| 6244 | } |
| 6245 | #endif |
| 6246 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 6247 | goto parse_error; |
| 6248 | continue; /* back to top of while (1) */ |
| 6249 | case '<': |
| 6250 | redir_fd = redirect_opt_num(&dest); |
| 6251 | if (done_word(&dest, &ctx)) { |
| 6252 | goto parse_error; |
| 6253 | } |
| 6254 | redir_style = REDIRECT_INPUT; |
| 6255 | if (next == '<') { |
| 6256 | redir_style = REDIRECT_HEREDOC; |
| 6257 | heredoc_cnt++; |
| 6258 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 6259 | ch = i_getch(input); |
| 6260 | nommu_addchr(&ctx.as_string, ch); |
| 6261 | } else if (next == '>') { |
| 6262 | redir_style = REDIRECT_IO; |
| 6263 | ch = i_getch(input); |
| 6264 | nommu_addchr(&ctx.as_string, ch); |
| 6265 | } |
| 6266 | #if 0 |
| 6267 | else if (next == '(') { |
| 6268 | syntax_error("<(process) not supported"); |
| 6269 | goto parse_error; |
| 6270 | } |
| 6271 | #endif |
| 6272 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 6273 | goto parse_error; |
| 6274 | continue; /* back to top of while (1) */ |
| 6275 | } |
| 6276 | |
| 6277 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 6278 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 6279 | && !ctx.pending_redirect |
| 6280 | ) { |
| 6281 | /* ch is a special char and thus this word |
| 6282 | * cannot be an assignment */ |
| 6283 | dest.o_assignment = NOT_ASSIGNMENT; |
| 6284 | } |
| 6285 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6286 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 6287 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6288 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6289 | case '#': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6290 | if (dest.length == 0) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6291 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6292 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6293 | if (ch == EOF || ch == '\n') |
| 6294 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6295 | i_getch(input); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6296 | /* note: we do not add it to &ctx.as_string */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6297 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6298 | nommu_addchr(&ctx.as_string, '\n'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6299 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6300 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6301 | } |
| 6302 | break; |
| 6303 | case '\\': |
| 6304 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6305 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6306 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6307 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6308 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6309 | if (ch != '\n') { |
| 6310 | o_addchr(&dest, '\\'); |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6311 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6312 | o_addchr(&dest, ch); |
| 6313 | nommu_addchr(&ctx.as_string, ch); |
| 6314 | /* Example: echo Hello \2>file |
| 6315 | * we need to know that word 2 is quoted */ |
| 6316 | dest.o_quoted = 1; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 6317 | } |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6318 | #if !BB_MMU |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 6319 | else { |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6320 | /* It's "\<newline>". Remove trailing '\' from ctx.as_string */ |
| 6321 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6322 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 6323 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6324 | break; |
| 6325 | case '$': |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6326 | if (handle_dollar(&ctx.as_string, &dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6327 | debug_printf_parse("parse_stream parse error: " |
| 6328 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6329 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6330 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6331 | break; |
| 6332 | case '\'': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6333 | dest.o_quoted = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6334 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6335 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6336 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6337 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6338 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6339 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6340 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6341 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6342 | break; |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 6343 | o_addqchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6344 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6345 | break; |
| 6346 | case '"': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6347 | dest.o_quoted = 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6348 | is_in_dquote ^= 1; /* invert */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6349 | if (dest.o_assignment == NOT_ASSIGNMENT) |
| 6350 | dest.o_escape ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6351 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 6352 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6353 | case '`': { |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6354 | #if !BB_MMU |
| 6355 | int pos; |
| 6356 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6357 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6358 | o_addchr(&dest, '`'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6359 | #if !BB_MMU |
| 6360 | pos = dest.length; |
| 6361 | #endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6362 | add_till_backquote(&dest, input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6363 | #if !BB_MMU |
| 6364 | o_addstr(&ctx.as_string, dest.data + pos); |
| 6365 | o_addchr(&ctx.as_string, '`'); |
| 6366 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6367 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6368 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6369 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6370 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 6371 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6372 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6373 | #if ENABLE_HUSH_CASE |
| 6374 | case_semi: |
| 6375 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6376 | if (done_word(&dest, &ctx)) { |
| 6377 | goto parse_error; |
| 6378 | } |
| 6379 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6380 | #if ENABLE_HUSH_CASE |
| 6381 | /* Eat multiple semicolons, detect |
| 6382 | * whether it means something special */ |
| 6383 | while (1) { |
| 6384 | ch = i_peek(input); |
| 6385 | if (ch != ';') |
| 6386 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6387 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6388 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 6389 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6390 | ctx.ctx_dsemicolon = 1; |
| 6391 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6392 | break; |
| 6393 | } |
| 6394 | } |
| 6395 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6396 | new_cmd: |
| 6397 | /* We just finished a cmd. New one may start |
| 6398 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6399 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6400 | break; |
| 6401 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6402 | if (done_word(&dest, &ctx)) { |
| 6403 | goto parse_error; |
| 6404 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6405 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6406 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6407 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6408 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6409 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6410 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6411 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6412 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6413 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6414 | if (done_word(&dest, &ctx)) { |
| 6415 | goto parse_error; |
| 6416 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 6417 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6418 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6419 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 6420 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6421 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6422 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6423 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6424 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6425 | } else { |
| 6426 | /* we could pick up a file descriptor choice here |
| 6427 | * with redirect_opt_num(), but bash doesn't do it. |
| 6428 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6429 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6430 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6431 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6432 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6433 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6434 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6435 | if (ctx.ctx_res_w == RES_MATCH |
| 6436 | && ctx.command->argv == NULL /* not (word|(... */ |
| 6437 | && dest.length == 0 /* not word(... */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6438 | && dest.o_quoted == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6439 | ) { |
| 6440 | continue; |
| 6441 | } |
| 6442 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6443 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6444 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 6445 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6446 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6447 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6448 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6449 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6450 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6451 | goto case_semi; |
| 6452 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6453 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 6454 | /* proper use of this character is caught by end_trigger: |
| 6455 | * if we see {, we call parse_group(..., end_trigger='}') |
| 6456 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 6457 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6458 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6459 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 6460 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 6461 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6462 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6463 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6464 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6465 | parse_error: |
| 6466 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6467 | struct parse_context *pctx; |
| 6468 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6469 | |
| 6470 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 6471 | * Sample for finding leaks on syntax error recovery path. |
| 6472 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6473 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 6474 | * Samples to catch leaks at execution: |
| 6475 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 6476 | * 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] | 6477 | */ |
| 6478 | pctx = &ctx; |
| 6479 | do { |
| 6480 | /* Update pipe/command counts, |
| 6481 | * otherwise freeing may miss some */ |
| 6482 | done_pipe(pctx, PIPE_SEQ); |
| 6483 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 6484 | pctx->list_head, pctx); |
| 6485 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6486 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6487 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6488 | #if !BB_MMU |
| 6489 | o_free_unsafe(&pctx->as_string); |
| 6490 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6491 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6492 | if (pctx != &ctx) { |
| 6493 | free(pctx); |
| 6494 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6495 | IF_HAS_KEYWORDS(pctx = p2;) |
| 6496 | } while (HAS_KEYWORDS && pctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6497 | /* Free text, clear all dest fields */ |
| 6498 | o_free(&dest); |
| 6499 | /* If we are not in top-level parse, we return, |
| 6500 | * our caller will propagate error. |
| 6501 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6502 | if (end_trigger != ';') { |
| 6503 | #if !BB_MMU |
| 6504 | if (pstring) |
| 6505 | *pstring = NULL; |
| 6506 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6507 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6508 | return ERR_PTR; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6509 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6510 | /* Discard cached input, force prompt */ |
| 6511 | input->p = NULL; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6512 | IF_HUSH_INTERACTIVE(input->promptme = 1;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6513 | goto reset; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6514 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6515 | } |
| 6516 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6517 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6518 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 6519 | * end_trigger controls how often we stop parsing |
| 6520 | * NUL: parse all, execute, return |
| 6521 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 6522 | */ |
| 6523 | 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] | 6524 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6525 | /* Why we need empty flag? |
| 6526 | * An obscure corner case "false; ``; echo $?": |
| 6527 | * empty command in `` should still set $? to 0. |
| 6528 | * But we can't just set $? to 0 at the start, |
| 6529 | * this breaks "false; echo `echo $?`" case. |
| 6530 | */ |
| 6531 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6532 | while (1) { |
| 6533 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6534 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6535 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6536 | if (!pipe_list) { /* EOF */ |
| 6537 | if (empty) |
| 6538 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6539 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6540 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6541 | debug_print_tree(pipe_list, 0); |
| 6542 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 6543 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6544 | empty = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6545 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6546 | } |
| 6547 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6548 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6549 | { |
| 6550 | struct in_str input; |
| 6551 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6552 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6553 | } |
| 6554 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6555 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6556 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6557 | struct in_str input; |
| 6558 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6559 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6560 | } |
| 6561 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6562 | /* Called a few times only (or even once if "sh -c") */ |
| 6563 | static void block_signals(int second_time) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 6564 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6565 | unsigned sig; |
| 6566 | unsigned mask; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 6567 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6568 | mask = (1 << SIGQUIT); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6569 | if (G_interactive_fd) { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 6570 | mask = (1 << SIGQUIT) | SPECIAL_INTERACTIVE_SIGS; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6571 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6572 | mask |= SPECIAL_JOB_SIGS; |
| 6573 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6574 | G.non_DFL_mask = mask; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 6575 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6576 | if (!second_time) |
| 6577 | sigprocmask(SIG_SETMASK, NULL, &G.blocked_set); |
| 6578 | sig = 0; |
| 6579 | while (mask) { |
| 6580 | if (mask & 1) |
| 6581 | sigaddset(&G.blocked_set, sig); |
| 6582 | mask >>= 1; |
| 6583 | sig++; |
| 6584 | } |
| 6585 | sigdelset(&G.blocked_set, SIGCHLD); |
| 6586 | |
| 6587 | sigprocmask(SIG_SETMASK, &G.blocked_set, |
| 6588 | second_time ? NULL : &G.inherited_set); |
| 6589 | /* POSIX allows shell to re-enable SIGCHLD |
| 6590 | * even if it was SIG_IGN on entry */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 6591 | #if ENABLE_HUSH_FAST |
| 6592 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6593 | if (!second_time) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 6594 | signal(SIGCHLD, SIGCHLD_handler); |
| 6595 | #else |
| 6596 | if (!second_time) |
| 6597 | signal(SIGCHLD, SIG_DFL); |
| 6598 | #endif |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6599 | } |
| 6600 | |
| 6601 | #if ENABLE_HUSH_JOB |
| 6602 | /* helper */ |
| 6603 | static void maybe_set_to_sigexit(int sig) |
| 6604 | { |
| 6605 | void (*handler)(int); |
| 6606 | /* non_DFL_mask'ed signals are, well, masked, |
| 6607 | * no need to set handler for them. |
| 6608 | */ |
| 6609 | if (!((G.non_DFL_mask >> sig) & 1)) { |
| 6610 | handler = signal(sig, sigexit); |
| 6611 | if (handler == SIG_IGN) /* oops... restore back to IGN! */ |
| 6612 | signal(sig, handler); |
| 6613 | } |
| 6614 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6615 | /* Set handlers to restore tty pgrp and exit */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6616 | static void set_fatal_handlers(void) |
| 6617 | { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 6618 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6619 | if (HUSH_DEBUG) { |
| 6620 | maybe_set_to_sigexit(SIGILL ); |
| 6621 | maybe_set_to_sigexit(SIGFPE ); |
| 6622 | maybe_set_to_sigexit(SIGBUS ); |
| 6623 | maybe_set_to_sigexit(SIGSEGV); |
| 6624 | maybe_set_to_sigexit(SIGTRAP); |
| 6625 | } /* else: hush is perfect. what SEGV? */ |
| 6626 | maybe_set_to_sigexit(SIGABRT); |
| 6627 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 6628 | maybe_set_to_sigexit(SIGPIPE); |
| 6629 | maybe_set_to_sigexit(SIGALRM); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 6630 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are masked. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6631 | * if we aren't interactive... but in this case |
| 6632 | * 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] | 6633 | /*maybe_set_to_sigexit(SIGHUP );*/ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6634 | /*maybe_set_to_sigexit(SIGTERM);*/ |
| 6635 | /*maybe_set_to_sigexit(SIGINT );*/ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 6636 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 6637 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 6638 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6639 | static int set_mode(const char cstate, const char mode) |
| 6640 | { |
| 6641 | int state = (cstate == '-' ? 1 : 0); |
| 6642 | switch (mode) { |
| 6643 | case 'n': G.fake_mode = state; break; |
| 6644 | case 'x': /*G.debug_mode = state;*/ break; |
| 6645 | default: return EXIT_FAILURE; |
| 6646 | } |
| 6647 | return EXIT_SUCCESS; |
| 6648 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6649 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 6650 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 6651 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6652 | { |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6653 | static const struct variable const_shell_ver = { |
| 6654 | .next = NULL, |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 6655 | .varstr = (char*)hush_version_str, |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6656 | .max_len = 1, /* 0 can provoke free(name) */ |
| 6657 | .flg_export = 1, |
| 6658 | .flg_read_only = 1, |
| 6659 | }; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6660 | int signal_mask_is_inited = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6661 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6662 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6663 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6664 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 6665 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 6666 | INIT_G(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6667 | if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6668 | G.last_exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6669 | #if !BB_MMU |
| 6670 | G.argv0_for_re_execing = argv[0]; |
| 6671 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6672 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6673 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 6674 | G.top_var = &G.shell_ver; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6675 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6676 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 6677 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6678 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6679 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6680 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6681 | if (e) while (*e) { |
| 6682 | char *value = strchr(*e, '='); |
| 6683 | if (value) { /* paranoia */ |
| 6684 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 6685 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 6686 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6687 | cur_var->max_len = strlen(*e); |
| 6688 | cur_var->flg_export = 1; |
| 6689 | } |
| 6690 | e++; |
| 6691 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 6692 | /* reinstate HUSH_VERSION */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6693 | debug_printf_env("putenv '%s'\n", hush_version_str); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 6694 | putenv((char *)hush_version_str); |
| 6695 | |
| 6696 | /* Export PWD */ |
| 6697 | set_pwd_var(/*exp:*/ 1); |
| 6698 | /* bash also exports SHLVL and _, |
| 6699 | * and sets (but doesn't export) the following variables: |
| 6700 | * BASH=/bin/bash |
| 6701 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 6702 | * BASH_VERSION='3.2.0(1)-release' |
| 6703 | * HOSTTYPE=i386 |
| 6704 | * MACHTYPE=i386-pc-linux-gnu |
| 6705 | * OSTYPE=linux-gnu |
| 6706 | * HOSTNAME=<xxxxxxxxxx> |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 6707 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 6708 | * EUID=<NNNNN> |
| 6709 | * UID=<NNNNN> |
| 6710 | * GROUPS=() |
| 6711 | * LINES=<NNN> |
| 6712 | * COLUMNS=<NNN> |
| 6713 | * BASH_ARGC=() |
| 6714 | * BASH_ARGV=() |
| 6715 | * BASH_LINENO=() |
| 6716 | * BASH_SOURCE=() |
| 6717 | * DIRSTACK=() |
| 6718 | * PIPESTATUS=([0]="0") |
| 6719 | * HISTFILE=/<xxx>/.bash_history |
| 6720 | * HISTFILESIZE=500 |
| 6721 | * HISTSIZE=500 |
| 6722 | * MAILCHECK=60 |
| 6723 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 6724 | * SHELL=/bin/bash |
| 6725 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 6726 | * TERM=dumb |
| 6727 | * OPTERR=1 |
| 6728 | * OPTIND=1 |
| 6729 | * IFS=$' \t\n' |
| 6730 | * PS1='\s-\v\$ ' |
| 6731 | * PS2='> ' |
| 6732 | * PS4='+ ' |
| 6733 | */ |
| 6734 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 6735 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6736 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 6737 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6738 | G.global_argc = argc; |
| 6739 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 6740 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 6741 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 6742 | |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 6743 | if (setjmp(die_jmp)) { |
| 6744 | /* xfunc has failed! die die die */ |
| 6745 | /* no EXIT traps, this is an escape hatch! */ |
| 6746 | G.exiting = 1; |
| 6747 | hush_exit(xfunc_error_retval); |
| 6748 | } |
| 6749 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6750 | /* Shell is non-interactive at first. We need to call |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6751 | * block_signals(0) if we are going to execute "sh <script>", |
| 6752 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6753 | * If we later decide that we are interactive, we run block_signals(0) |
| 6754 | * (or re-run block_signals(1) if we ran block_signals(0) before) |
| 6755 | * in order to intercept (more) signals. |
| 6756 | */ |
| 6757 | |
| 6758 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 6759 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6760 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6761 | while (1) { |
Denys Vlasenko | a67a962 | 2009-08-20 03:38:58 +0200 | [diff] [blame] | 6762 | opt = getopt(argc, argv, "+c:xins" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6763 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 6764 | "<:$:R:V:" |
| 6765 | # if ENABLE_HUSH_FUNCTIONS |
| 6766 | "F:" |
| 6767 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6768 | #endif |
| 6769 | ); |
| 6770 | if (opt <= 0) |
| 6771 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6772 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6773 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6774 | /* Possibilities: |
| 6775 | * sh ... -c 'script' |
| 6776 | * sh ... -c 'script' ARG0 [ARG1...] |
| 6777 | * On NOMMU, if builtin_argc != 0, |
| 6778 | * sh ... -c 'builtin' [BARGV...] "" ARG0 [ARG1...] |
| 6779 | * "" needs to be replaced with NULL |
| 6780 | * and BARGV vector fed to builtin function. |
| 6781 | * Note: this form never happens: |
| 6782 | * sh ... -c 'builtin' [BARGV...] "" |
| 6783 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 6784 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6785 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 6786 | G.root_ppid = getppid(); |
| 6787 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6788 | G.global_argv = argv + optind; |
| 6789 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6790 | if (builtin_argc) { |
| 6791 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 6792 | const struct built_in_command *x; |
| 6793 | |
| 6794 | block_signals(0); /* 0: called 1st time */ |
| 6795 | x = find_builtin(optarg); |
| 6796 | if (x) { /* paranoia */ |
| 6797 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 6798 | G.global_argv += builtin_argc; |
| 6799 | G.global_argv[-1] = NULL; /* replace "" */ |
| 6800 | G.last_exitcode = x->function(argv + optind - 1); |
| 6801 | } |
| 6802 | goto final_return; |
| 6803 | } |
| 6804 | if (!G.global_argv[0]) { |
| 6805 | /* -c 'script' (no params): prevent empty $0 */ |
| 6806 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 6807 | G.global_argv[0] = argv[0]; |
| 6808 | G.global_argc--; |
| 6809 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6810 | block_signals(0); /* 0: called 1st time */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6811 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6812 | goto final_return; |
| 6813 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 6814 | /* Well, we cannot just declare interactiveness, |
| 6815 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6816 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6817 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 6818 | case 's': |
| 6819 | /* "-s" means "read from stdin", but this is how we always |
| 6820 | * operate, so simply do nothing here. */ |
| 6821 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6822 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 6823 | case '<': /* "big heredoc" support */ |
| 6824 | full_write(STDOUT_FILENO, optarg, strlen(optarg)); |
| 6825 | _exit(0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6826 | case '$': |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6827 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 6828 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 6829 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 6830 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6831 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 6832 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6833 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6834 | optarg++; |
| 6835 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6836 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6837 | optarg++; |
| 6838 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6839 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6840 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6841 | case 'R': |
| 6842 | case 'V': |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6843 | set_local_var(xstrdup(optarg), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ opt == 'R'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6844 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 6845 | # if ENABLE_HUSH_FUNCTIONS |
| 6846 | case 'F': { |
| 6847 | struct function *funcp = new_function(optarg); |
| 6848 | /* funcp->name is already set to optarg */ |
| 6849 | /* funcp->body is set to NULL. It's a special case. */ |
| 6850 | funcp->body_as_string = argv[optind]; |
| 6851 | optind++; |
| 6852 | break; |
| 6853 | } |
| 6854 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6855 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6856 | case 'n': |
| 6857 | case 'x': |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6858 | if (!set_mode('-', opt)) |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6859 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6860 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6861 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6862 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 6863 | " or: sh -c command [args]...\n\n"); |
| 6864 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6865 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6866 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6867 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6868 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6869 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6870 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 6871 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6872 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 6873 | G.root_ppid = getppid(); |
| 6874 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6875 | |
| 6876 | /* If we are login shell... */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6877 | if (argv[0] && argv[0][0] == '-') { |
| 6878 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6879 | debug_printf("sourcing /etc/profile\n"); |
| 6880 | input = fopen_for_read("/etc/profile"); |
| 6881 | if (input != NULL) { |
| 6882 | close_on_exec_on(fileno(input)); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6883 | block_signals(0); /* 0: called 1st time */ |
| 6884 | signal_mask_is_inited = 1; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6885 | parse_and_run_file(input); |
| 6886 | fclose(input); |
| 6887 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6888 | /* bash: after sourcing /etc/profile, |
| 6889 | * tries to source (in the given order): |
| 6890 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 6891 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6892 | * bash also sources ~/.bash_logout on exit. |
| 6893 | * If called as sh, skips .bash_XXX files. |
| 6894 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6895 | } |
| 6896 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6897 | if (argv[optind]) { |
| 6898 | FILE *input; |
| 6899 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6900 | * "bash <script>" (which is never interactive (unless -i?)) |
| 6901 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6902 | * If called as sh, does the same but with $ENV. |
| 6903 | */ |
| 6904 | debug_printf("running script '%s'\n", argv[optind]); |
| 6905 | G.global_argv = argv + optind; |
| 6906 | G.global_argc = argc - optind; |
| 6907 | input = xfopen_for_read(argv[optind]); |
| 6908 | close_on_exec_on(fileno(input)); |
| 6909 | if (!signal_mask_is_inited) |
| 6910 | block_signals(0); /* 0: called 1st time */ |
| 6911 | parse_and_run_file(input); |
| 6912 | #if ENABLE_FEATURE_CLEAN_UP |
| 6913 | fclose(input); |
| 6914 | #endif |
| 6915 | goto final_return; |
| 6916 | } |
| 6917 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6918 | /* Up to here, shell was non-interactive. Now it may become one. |
| 6919 | * NB: don't forget to (re)run block_signals(0/1) as needed. |
| 6920 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6921 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 6922 | /* A shell is interactive if the '-i' flag was given, |
| 6923 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 6924 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6925 | * no arguments remaining or the -s flag given |
| 6926 | * standard input is a terminal |
| 6927 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6928 | * Refer to Posix.2, the description of the 'sh' utility. |
| 6929 | */ |
| 6930 | #if ENABLE_HUSH_JOB |
| 6931 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6932 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 6933 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 6934 | if (G_saved_tty_pgrp < 0) |
| 6935 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6936 | |
| 6937 | /* try to dup stdin to high fd#, >= 255 */ |
| 6938 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 6939 | if (G_interactive_fd < 0) { |
| 6940 | /* try to dup to any fd */ |
| 6941 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6942 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6943 | /* give up */ |
| 6944 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6945 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 6946 | } |
| 6947 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6948 | // TODO: track & disallow any attempts of user |
| 6949 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6950 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6951 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6952 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6953 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6954 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6955 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6956 | /* If we were run as 'hush &', sleep until we are |
| 6957 | * in the foreground (tty pgrp == our pgrp). |
| 6958 | * If we get started under a job aware app (like bash), |
| 6959 | * make sure we are now in charge so we don't fight over |
| 6960 | * who gets the foreground */ |
| 6961 | while (1) { |
| 6962 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6963 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 6964 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6965 | break; |
| 6966 | /* send TTIN to ourself (should stop us) */ |
| 6967 | kill(- shell_pgrp, SIGTTIN); |
| 6968 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6969 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6970 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6971 | /* Block some signals */ |
| 6972 | block_signals(signal_mask_is_inited); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6973 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6974 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6975 | /* Set other signals to restore saved_tty_pgrp */ |
| 6976 | set_fatal_handlers(); |
| 6977 | /* Put ourselves in our own process group |
| 6978 | * (bash, too, does this only if ctty is available) */ |
| 6979 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 6980 | /* Grab control of the terminal */ |
| 6981 | tcsetpgrp(G_interactive_fd, getpid()); |
| 6982 | } |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 6983 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 6984 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6985 | enable_restore_tty_pgrp_on_exit(); /* sets die_sleep = -1 */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6986 | } else if (!signal_mask_is_inited) { |
| 6987 | block_signals(0); /* 0: called 1st time */ |
| 6988 | } /* else: block_signals(0) was done before */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6989 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6990 | /* No job control compiled in, only prompt/line editing */ |
| 6991 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6992 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 6993 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6994 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6995 | G_interactive_fd = dup(STDIN_FILENO); |
| 6996 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6997 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6998 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6999 | } |
| 7000 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7001 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7002 | close_on_exec_on(G_interactive_fd); |
| 7003 | block_signals(signal_mask_is_inited); |
| 7004 | } else if (!signal_mask_is_inited) { |
| 7005 | block_signals(0); |
| 7006 | } |
| 7007 | #else |
| 7008 | /* We have interactiveness code disabled */ |
| 7009 | if (!signal_mask_is_inited) { |
| 7010 | block_signals(0); |
| 7011 | } |
| 7012 | #endif |
| 7013 | /* bash: |
| 7014 | * if interactive but not a login shell, sources ~/.bashrc |
| 7015 | * (--norc turns this off, --rcfile <file> overrides) |
| 7016 | */ |
| 7017 | |
| 7018 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 7019 | /* note: ash and hush share this string */ |
| 7020 | printf("\n\n%s %s\n" |
| 7021 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 7022 | "\n", |
| 7023 | bb_banner, |
| 7024 | "hush - the humble shell" |
| 7025 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 7026 | } |
| 7027 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7028 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7029 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7030 | final_return: |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 7031 | #if ENABLE_FEATURE_CLEAN_UP |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7032 | if (G.cwd != bb_msg_unknown) |
| 7033 | free((char*)G.cwd); |
| 7034 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7035 | while (cur_var) { |
| 7036 | struct variable *tmp = cur_var; |
| 7037 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 7038 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7039 | cur_var = cur_var->next; |
| 7040 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 7041 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7042 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7043 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7044 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 7045 | |
| 7046 | |
| 7047 | #if ENABLE_LASH |
| 7048 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 7049 | int lash_main(int argc, char **argv) |
| 7050 | { |
Denys Vlasenko | 0793491 | 2009-08-20 03:40:04 +0200 | [diff] [blame] | 7051 | bb_error_msg("lash is deprecated, please use hush instead"); |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 7052 | return hush_main(argc, argv); |
| 7053 | } |
| 7054 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7055 | |
Denys Vlasenko | 1cc4b13 | 2009-08-21 00:05:51 +0200 | [diff] [blame] | 7056 | #if ENABLE_MSH |
| 7057 | int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 7058 | int msh_main(int argc, char **argv) |
| 7059 | { |
| 7060 | //bb_error_msg("msh is deprecated, please use hush instead"); |
| 7061 | return hush_main(argc, argv); |
| 7062 | } |
| 7063 | #endif |
| 7064 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7065 | |
| 7066 | /* |
| 7067 | * Built-ins |
| 7068 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7069 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7070 | { |
| 7071 | return 0; |
| 7072 | } |
| 7073 | |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 7074 | 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] | 7075 | { |
| 7076 | int argc = 0; |
| 7077 | while (*argv) { |
| 7078 | argc++; |
| 7079 | argv++; |
| 7080 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 7081 | return applet_main_func(argc, argv - argc); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 7082 | } |
| 7083 | |
| 7084 | static int FAST_FUNC builtin_test(char **argv) |
| 7085 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7086 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7087 | } |
| 7088 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7089 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7090 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7091 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7092 | } |
| 7093 | |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 7094 | #if ENABLE_PRINTF |
| 7095 | static int FAST_FUNC builtin_printf(char **argv) |
| 7096 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7097 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 7098 | } |
| 7099 | #endif |
| 7100 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7101 | static int FAST_FUNC builtin_eval(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7102 | { |
| 7103 | int rcode = EXIT_SUCCESS; |
| 7104 | |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7105 | if (*++argv) { |
| 7106 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7107 | /* bash: |
| 7108 | * eval "echo Hi; done" ("done" is syntax error): |
| 7109 | * "echo Hi" will not execute too. |
| 7110 | */ |
| 7111 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7112 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7113 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7114 | } |
| 7115 | return rcode; |
| 7116 | } |
| 7117 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7118 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7119 | { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7120 | const char *newdir = argv[1]; |
| 7121 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7122 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7123 | * bash says "bash: cd: HOME not set" and does nothing |
| 7124 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7125 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 7126 | const char *home = get_local_var_value("HOME"); |
| 7127 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7128 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7129 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7130 | /* Mimic bash message exactly */ |
| 7131 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7132 | return EXIT_FAILURE; |
| 7133 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7134 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 7135 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 7136 | * set it again, but do not export. bash does the same. |
| 7137 | */ |
| 7138 | set_pwd_var(/*exp:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7139 | return EXIT_SUCCESS; |
| 7140 | } |
| 7141 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7142 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7143 | { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7144 | if (*++argv == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7145 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 7146 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 7147 | /* Careful: we can end up here after [v]fork. Do not restore |
| 7148 | * tty pgrp then, only top-level shell process does that */ |
| 7149 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 7150 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 7151 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 7152 | /* TODO: if exec fails, bash does NOT exit! We do. |
| 7153 | * We'll need to undo sigprocmask (it's inside execvp_or_die) |
| 7154 | * and tcsetpgrp, and this is inherently racy. |
| 7155 | */ |
| 7156 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7157 | } |
| 7158 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7159 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7160 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 7161 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 7162 | |
| 7163 | /* interactive bash: |
| 7164 | * # trap "echo EEE" EXIT |
| 7165 | * # exit |
| 7166 | * exit |
| 7167 | * There are stopped jobs. |
| 7168 | * (if there are _stopped_ jobs, running ones don't count) |
| 7169 | * # exit |
| 7170 | * exit |
| 7171 | # EEE (then bash exits) |
| 7172 | * |
| 7173 | * we can use G.exiting = -1 as indicator "last cmd was exit" |
| 7174 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 7175 | |
| 7176 | /* note: EXIT trap is run by hush_exit */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7177 | if (*++argv == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7178 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7179 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 7180 | xfunc_error_retval = 255; |
| 7181 | /* bash: exit -2 == exit 254, no error msg */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7182 | hush_exit(xatoi(*argv) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7183 | } |
| 7184 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7185 | static void print_escaped(const char *s) |
| 7186 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7187 | if (*s == '\'') |
| 7188 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7189 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7190 | const char *p = strchrnul(s, '\''); |
| 7191 | /* print 'xxxx', possibly just '' */ |
| 7192 | printf("'%.*s'", (int)(p - s), s); |
| 7193 | if (*p == '\0') |
| 7194 | break; |
| 7195 | s = p; |
| 7196 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7197 | /* s points to '; print "'''...'''" */ |
| 7198 | putchar('"'); |
| 7199 | do putchar('\''); while (*++s == '\''); |
| 7200 | putchar('"'); |
| 7201 | } while (*s); |
| 7202 | } |
| 7203 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7204 | #if !ENABLE_HUSH_LOCAL |
| 7205 | #define helper_export_local(argv, exp, lvl) \ |
| 7206 | helper_export_local(argv, exp) |
| 7207 | #endif |
| 7208 | static void helper_export_local(char **argv, int exp, int lvl) |
| 7209 | { |
| 7210 | do { |
| 7211 | char *name = *argv; |
| 7212 | |
| 7213 | /* So far we do not check that name is valid (TODO?) */ |
| 7214 | |
| 7215 | if (strchr(name, '=') == NULL) { |
| 7216 | struct variable *var; |
| 7217 | |
| 7218 | var = get_local_var(name); |
| 7219 | if (exp == -1) { /* unexporting? */ |
| 7220 | /* export -n NAME (without =VALUE) */ |
| 7221 | if (var) { |
| 7222 | var->flg_export = 0; |
| 7223 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 7224 | unsetenv(name); |
| 7225 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 7226 | continue; |
| 7227 | } |
| 7228 | if (exp == 1) { /* exporting? */ |
| 7229 | /* export NAME (without =VALUE) */ |
| 7230 | if (var) { |
| 7231 | var->flg_export = 1; |
| 7232 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 7233 | putenv(var->varstr); |
| 7234 | continue; |
| 7235 | } |
| 7236 | } |
| 7237 | /* Exporting non-existing variable. |
| 7238 | * bash does not put it in environment, |
| 7239 | * but remembers that it is exported, |
| 7240 | * and does put it in env when it is set later. |
| 7241 | * We just set it to "" and export. */ |
| 7242 | /* Or, it's "local NAME" (without =VALUE). |
| 7243 | * bash sets the value to "". */ |
| 7244 | name = xasprintf("%s=", name); |
| 7245 | } else { |
| 7246 | /* (Un)exporting/making local NAME=VALUE */ |
| 7247 | name = xstrdup(name); |
| 7248 | } |
| 7249 | set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0); |
| 7250 | } while (*++argv); |
| 7251 | } |
| 7252 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7253 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7254 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 7255 | unsigned opt_unexport; |
| 7256 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 7257 | #if ENABLE_HUSH_EXPORT_N |
| 7258 | /* "!": do not abort on errors */ |
| 7259 | opt_unexport = getopt32(argv, "!n"); |
| 7260 | if (opt_unexport == (uint32_t)-1) |
| 7261 | return EXIT_FAILURE; |
| 7262 | argv += optind; |
| 7263 | #else |
| 7264 | opt_unexport = 0; |
| 7265 | argv++; |
| 7266 | #endif |
| 7267 | |
| 7268 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7269 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7270 | if (e) { |
| 7271 | while (*e) { |
| 7272 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7273 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7274 | #else |
| 7275 | /* ash emits: export VAR='VAL' |
| 7276 | * bash: declare -x VAR="VAL" |
| 7277 | * we follow ash example */ |
| 7278 | const char *s = *e++; |
| 7279 | const char *p = strchr(s, '='); |
| 7280 | |
| 7281 | if (!p) /* wtf? take next variable */ |
| 7282 | continue; |
| 7283 | /* export var= */ |
| 7284 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7285 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7286 | putchar('\n'); |
| 7287 | #endif |
| 7288 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 7289 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7290 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7291 | return EXIT_SUCCESS; |
| 7292 | } |
| 7293 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7294 | helper_export_local(argv, (opt_unexport ? -1 : 1), 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7295 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7296 | return EXIT_SUCCESS; |
| 7297 | } |
| 7298 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7299 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7300 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7301 | { |
| 7302 | if (G.func_nest_level == 0) { |
| 7303 | bb_error_msg("%s: not in a function", argv[0]); |
| 7304 | return EXIT_FAILURE; /* bash compat */ |
| 7305 | } |
| 7306 | helper_export_local(argv, 0, G.func_nest_level); |
| 7307 | return EXIT_SUCCESS; |
| 7308 | } |
| 7309 | #endif |
| 7310 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7311 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7312 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7313 | int sig; |
| 7314 | char *new_cmd; |
| 7315 | |
| 7316 | if (!G.traps) |
| 7317 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 7318 | |
| 7319 | argv++; |
| 7320 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7321 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7322 | /* No args: print all trapped */ |
| 7323 | for (i = 0; i < NSIG; ++i) { |
| 7324 | if (G.traps[i]) { |
| 7325 | printf("trap -- "); |
| 7326 | print_escaped(G.traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 7327 | /* note: bash adds "SIG", but only if invoked |
| 7328 | * as "bash". If called as "sh", or if set -o posix, |
| 7329 | * then it prints short signal names. |
| 7330 | * We are printing short names: */ |
| 7331 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7332 | } |
| 7333 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 7334 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7335 | return EXIT_SUCCESS; |
| 7336 | } |
| 7337 | |
| 7338 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7339 | /* If first arg is a number: reset all specified signals */ |
| 7340 | sig = bb_strtou(*argv, NULL, 10); |
| 7341 | if (errno == 0) { |
| 7342 | int ret; |
| 7343 | process_sig_list: |
| 7344 | ret = EXIT_SUCCESS; |
| 7345 | while (*argv) { |
| 7346 | sig = get_signum(*argv++); |
| 7347 | if (sig < 0 || sig >= NSIG) { |
| 7348 | ret = EXIT_FAILURE; |
| 7349 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7350 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7351 | continue; |
| 7352 | } |
| 7353 | |
| 7354 | free(G.traps[sig]); |
| 7355 | G.traps[sig] = xstrdup(new_cmd); |
| 7356 | |
| 7357 | debug_printf("trap: setting SIG%s (%i) to '%s'", |
| 7358 | get_signame(sig), sig, G.traps[sig]); |
| 7359 | |
| 7360 | /* There is no signal for 0 (EXIT) */ |
| 7361 | if (sig == 0) |
| 7362 | continue; |
| 7363 | |
| 7364 | if (new_cmd) { |
| 7365 | sigaddset(&G.blocked_set, sig); |
| 7366 | } else { |
| 7367 | /* There was a trap handler, we are removing it |
| 7368 | * (if sig has non-DFL handling, |
| 7369 | * we don't need to do anything) */ |
| 7370 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 7371 | continue; |
| 7372 | sigdelset(&G.blocked_set, sig); |
| 7373 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7374 | } |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7375 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7376 | return ret; |
| 7377 | } |
| 7378 | |
| 7379 | if (!argv[1]) { /* no second arg */ |
| 7380 | bb_error_msg("trap: invalid arguments"); |
| 7381 | return EXIT_FAILURE; |
| 7382 | } |
| 7383 | |
| 7384 | /* First arg is "-": reset all specified to default */ |
| 7385 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 7386 | /* Everything else: set arg as signal handler |
| 7387 | * (includes "" case, which ignores signal) */ |
| 7388 | if (argv[0][0] == '-') { |
| 7389 | if (argv[0][1] == '\0') { /* "-" */ |
| 7390 | /* new_cmd remains NULL: "reset these sigs" */ |
| 7391 | goto reset_traps; |
| 7392 | } |
| 7393 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 7394 | argv++; |
| 7395 | } |
| 7396 | /* else: "-something", no special meaning */ |
| 7397 | } |
| 7398 | new_cmd = *argv; |
| 7399 | reset_traps: |
| 7400 | argv++; |
| 7401 | goto process_sig_list; |
| 7402 | } |
| 7403 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7404 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7405 | static int FAST_FUNC builtin_type(char **argv) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7406 | { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7407 | int ret = EXIT_SUCCESS; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7408 | |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7409 | while (*++argv) { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7410 | const char *type; |
Denys Vlasenko | 171932d | 2009-05-28 17:07:22 +0200 | [diff] [blame] | 7411 | char *path = NULL; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7412 | |
| 7413 | if (0) {} /* make conditional compile easier below */ |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7414 | /*else if (find_alias(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7415 | type = "an alias";*/ |
| 7416 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7417 | else if (find_function(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7418 | type = "a function"; |
| 7419 | #endif |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7420 | else if (find_builtin(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7421 | type = "a shell builtin"; |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7422 | else if ((path = find_in_path(*argv)) != NULL) |
| 7423 | type = path; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7424 | else { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7425 | bb_error_msg("type: %s: not found", *argv); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7426 | ret = EXIT_FAILURE; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7427 | continue; |
| 7428 | } |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7429 | |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7430 | printf("%s is %s\n", *argv, type); |
| 7431 | free(path); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7432 | } |
| 7433 | |
| 7434 | return ret; |
| 7435 | } |
| 7436 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7437 | #if ENABLE_HUSH_JOB |
| 7438 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7439 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7440 | { |
| 7441 | int i, jobnum; |
| 7442 | struct pipe *pi; |
| 7443 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7444 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7445 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7446 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7447 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 7448 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7449 | for (pi = G.job_list; pi; pi = pi->next) { |
| 7450 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7451 | goto found; |
| 7452 | } |
| 7453 | } |
| 7454 | bb_error_msg("%s: no current job", argv[0]); |
| 7455 | return EXIT_FAILURE; |
| 7456 | } |
| 7457 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 7458 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 7459 | return EXIT_FAILURE; |
| 7460 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7461 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7462 | if (pi->jobid == jobnum) { |
| 7463 | goto found; |
| 7464 | } |
| 7465 | } |
| 7466 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 7467 | return EXIT_FAILURE; |
| 7468 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 7469 | /* TODO: bash prints a string representation |
| 7470 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7471 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7472 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7473 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7474 | } |
| 7475 | |
| 7476 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7477 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 7478 | for (i = 0; i < pi->num_cmds; i++) { |
| 7479 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 7480 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7481 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7482 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7483 | |
| 7484 | i = kill(- pi->pgrp, SIGCONT); |
| 7485 | if (i < 0) { |
| 7486 | if (errno == ESRCH) { |
| 7487 | delete_finished_bg_job(pi); |
| 7488 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7489 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7490 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7491 | } |
| 7492 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7493 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7494 | remove_bg_job(pi); |
| 7495 | return checkjobs_and_fg_shell(pi); |
| 7496 | } |
| 7497 | return EXIT_SUCCESS; |
| 7498 | } |
| 7499 | #endif |
| 7500 | |
| 7501 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7502 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7503 | { |
| 7504 | const struct built_in_command *x; |
| 7505 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7506 | printf( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7507 | "Built-in commands:\n" |
| 7508 | "------------------\n"); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7509 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
| 7510 | if (x->descr) |
| 7511 | printf("%s\t%s\n", x->cmd, x->descr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7512 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7513 | bb_putchar('\n'); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7514 | return EXIT_SUCCESS; |
| 7515 | } |
| 7516 | #endif |
| 7517 | |
| 7518 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7519 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7520 | { |
| 7521 | struct pipe *job; |
| 7522 | const char *status_string; |
| 7523 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7524 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7525 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7526 | status_string = "Stopped"; |
| 7527 | else |
| 7528 | status_string = "Running"; |
| 7529 | |
| 7530 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 7531 | } |
| 7532 | return EXIT_SUCCESS; |
| 7533 | } |
| 7534 | #endif |
| 7535 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7536 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7537 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7538 | { |
| 7539 | void *p; |
| 7540 | unsigned long l; |
| 7541 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7542 | # ifdef M_TRIM_THRESHOLD |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 7543 | /* Optional. Reduces probability of false positives */ |
| 7544 | malloc_trim(0); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7545 | # endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7546 | /* Crude attempt to find where "free memory" starts, |
| 7547 | * sans fragmentation. */ |
| 7548 | p = malloc(240); |
| 7549 | l = (unsigned long)p; |
| 7550 | free(p); |
| 7551 | p = malloc(3400); |
| 7552 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 7553 | free(p); |
| 7554 | |
| 7555 | if (!G.memleak_value) |
| 7556 | G.memleak_value = l; |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 7557 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7558 | l -= G.memleak_value; |
| 7559 | if ((long)l < 0) |
| 7560 | l = 0; |
| 7561 | l /= 1024; |
| 7562 | if (l > 127) |
| 7563 | l = 127; |
| 7564 | |
| 7565 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 7566 | return l; |
| 7567 | } |
| 7568 | #endif |
| 7569 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7570 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7571 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7572 | puts(get_cwd(0)); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7573 | return EXIT_SUCCESS; |
| 7574 | } |
| 7575 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7576 | static int FAST_FUNC builtin_read(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7577 | { |
| 7578 | char *string; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 7579 | const char *name = "REPLY"; |
| 7580 | |
| 7581 | if (argv[1]) { |
| 7582 | name = argv[1]; |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 7583 | /* bash (3.2.33(1)) bug: "read 0abcd" will execute, |
| 7584 | * and _after_ that_ it will complain */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 7585 | if (!is_well_formed_var_name(name, '\0')) { |
| 7586 | /* Mimic bash message */ |
| 7587 | bb_error_msg("read: '%s': not a valid identifier", name); |
| 7588 | return 1; |
| 7589 | } |
| 7590 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7591 | |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 7592 | //TODO: bash unbackslashes input, splits words and puts them in argv[i] |
| 7593 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7594 | string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7595 | return set_local_var(string, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7596 | } |
| 7597 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7598 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 7599 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7600 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7601 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 7602 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7603 | * set -- [argument...] |
| 7604 | * set -o |
| 7605 | * set +o |
| 7606 | * Implementations shall support the options in both their hyphen and |
| 7607 | * plus-sign forms. These options can also be specified as options to sh. |
| 7608 | * Examples: |
| 7609 | * Write out all variables and their values: set |
| 7610 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 7611 | * Turn on the -x and -v options: set -xv |
| 7612 | * Unset all positional parameters: set -- |
| 7613 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 7614 | * Set the positional parameters to the expansion of x, even if x expands |
| 7615 | * with a leading '-' or '+': set -- $x |
| 7616 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7617 | * 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] | 7618 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7619 | static int FAST_FUNC builtin_set(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7620 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7621 | int n; |
| 7622 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7623 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7624 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7625 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7626 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7627 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7628 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7629 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7630 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7631 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7632 | do { |
| 7633 | if (!strcmp(arg, "--")) { |
| 7634 | ++argv; |
| 7635 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7636 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 7637 | if (arg[0] != '+' && arg[0] != '-') |
| 7638 | break; |
| 7639 | for (n = 1; arg[n]; ++n) |
| 7640 | if (set_mode(arg[0], arg[n])) |
| 7641 | goto error; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7642 | } while ((arg = *++argv) != NULL); |
| 7643 | /* Now argv[0] is 1st argument */ |
| 7644 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7645 | if (arg == NULL) |
| 7646 | return EXIT_SUCCESS; |
| 7647 | set_argv: |
| 7648 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7649 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 7650 | g_argv = G.global_argv; |
| 7651 | if (G.global_args_malloced) { |
| 7652 | pp = g_argv; |
| 7653 | while (*++pp) |
| 7654 | free(*pp); |
| 7655 | g_argv[1] = NULL; |
| 7656 | } else { |
| 7657 | G.global_args_malloced = 1; |
| 7658 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 7659 | pp[0] = g_argv[0]; /* retain $0 */ |
| 7660 | g_argv = pp; |
| 7661 | } |
| 7662 | /* This realloc's G.global_argv */ |
| 7663 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 7664 | |
| 7665 | n = 1; |
| 7666 | while (*++pp) |
| 7667 | n++; |
| 7668 | G.global_argc = n; |
| 7669 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7670 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7671 | |
| 7672 | /* Nothing known, so abort */ |
| 7673 | error: |
| 7674 | bb_error_msg("set: %s: invalid option", arg); |
| 7675 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7676 | } |
| 7677 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7678 | static int FAST_FUNC builtin_shift(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7679 | { |
| 7680 | int n = 1; |
| 7681 | if (argv[1]) { |
| 7682 | n = atoi(argv[1]); |
| 7683 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7684 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 7685 | if (G.global_args_malloced) { |
| 7686 | int m = 1; |
| 7687 | while (m <= n) |
| 7688 | free(G.global_argv[m++]); |
| 7689 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7690 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 7691 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 7692 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7693 | return EXIT_SUCCESS; |
| 7694 | } |
| 7695 | return EXIT_FAILURE; |
| 7696 | } |
| 7697 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7698 | static int FAST_FUNC builtin_source(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7699 | { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7700 | char *arg_path; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7701 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7702 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7703 | #if ENABLE_HUSH_FUNCTIONS |
| 7704 | smallint sv_flg; |
| 7705 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7706 | |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7707 | if (*++argv == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7708 | return EXIT_FAILURE; |
| 7709 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7710 | if (strchr(*argv, '/') == NULL && (arg_path = find_in_path(*argv)) != NULL) { |
| 7711 | input = fopen_for_read(arg_path); |
| 7712 | free(arg_path); |
| 7713 | } else |
| 7714 | input = fopen_or_warn(*argv, "r"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7715 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7716 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7717 | return EXIT_FAILURE; |
| 7718 | } |
| 7719 | close_on_exec_on(fileno(input)); |
| 7720 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7721 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7722 | sv_flg = G.flag_return_in_progress; |
| 7723 | /* "we are inside sourced file, ok to use return" */ |
| 7724 | G.flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7725 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7726 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7727 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7728 | parse_and_run_file(input); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7729 | fclose(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7730 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7731 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7732 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7733 | G.flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7734 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7735 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7736 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7737 | } |
| 7738 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7739 | static int FAST_FUNC builtin_umask(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7740 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7741 | int rc; |
| 7742 | mode_t mask; |
| 7743 | |
| 7744 | mask = umask(0); |
| 7745 | if (argv[1]) { |
| 7746 | mode_t old_mask = mask; |
| 7747 | |
| 7748 | mask ^= 0777; |
| 7749 | rc = bb_parse_mode(argv[1], &mask); |
| 7750 | mask ^= 0777; |
| 7751 | if (rc == 0) { |
| 7752 | mask = old_mask; |
| 7753 | /* bash messages: |
| 7754 | * bash: umask: 'q': invalid symbolic mode operator |
| 7755 | * bash: umask: 999: octal number out of range |
| 7756 | */ |
| 7757 | bb_error_msg("%s: '%s' invalid mode", argv[0], argv[1]); |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7758 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7759 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7760 | rc = 1; |
| 7761 | /* Mimic bash */ |
| 7762 | printf("%04o\n", (unsigned) mask); |
| 7763 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7764 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7765 | umask(mask); |
| 7766 | |
| 7767 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7768 | } |
| 7769 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7770 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7771 | static int FAST_FUNC builtin_unset(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7772 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7773 | int ret; |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7774 | unsigned opts; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7775 | |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7776 | /* "!": do not abort on errors */ |
| 7777 | /* "+": stop at 1st non-option */ |
| 7778 | opts = getopt32(argv, "!+vf"); |
| 7779 | if (opts == (unsigned)-1) |
| 7780 | return EXIT_FAILURE; |
| 7781 | if (opts == 3) { |
| 7782 | bb_error_msg("unset: -v and -f are exclusive"); |
| 7783 | return EXIT_FAILURE; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7784 | } |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7785 | argv += optind; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7786 | |
| 7787 | ret = EXIT_SUCCESS; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7788 | while (*argv) { |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7789 | if (!(opts & 2)) { /* not -f */ |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7790 | if (unset_local_var(*argv)) { |
| 7791 | /* unset <nonexistent_var> doesn't fail. |
| 7792 | * Error is when one tries to unset RO var. |
| 7793 | * Message was printed by unset_local_var. */ |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7794 | ret = EXIT_FAILURE; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7795 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7796 | } |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 7797 | #if ENABLE_HUSH_FUNCTIONS |
| 7798 | else { |
| 7799 | unset_func(*argv); |
| 7800 | } |
| 7801 | #endif |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7802 | argv++; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7803 | } |
| 7804 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7805 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7806 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7807 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7808 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7809 | { |
| 7810 | int ret = EXIT_SUCCESS; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7811 | int status, sig; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7812 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7813 | if (*++argv == NULL) { |
| 7814 | /* Don't care about wait results */ |
| 7815 | /* Note 1: must wait until there are no more children */ |
| 7816 | /* Note 2: must be interruptible */ |
| 7817 | /* Examples: |
| 7818 | * $ sleep 3 & sleep 6 & wait |
| 7819 | * [1] 30934 sleep 3 |
| 7820 | * [2] 30935 sleep 6 |
| 7821 | * [1] Done sleep 3 |
| 7822 | * [2] Done sleep 6 |
| 7823 | * $ sleep 3 & sleep 6 & wait |
| 7824 | * [1] 30936 sleep 3 |
| 7825 | * [2] 30937 sleep 6 |
| 7826 | * [1] Done sleep 3 |
| 7827 | * ^C <-- after ~4 sec from keyboard |
| 7828 | * $ |
| 7829 | */ |
| 7830 | sigaddset(&G.blocked_set, SIGCHLD); |
| 7831 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7832 | while (1) { |
| 7833 | checkjobs(NULL); |
| 7834 | if (errno == ECHILD) |
| 7835 | break; |
| 7836 | /* Wait for SIGCHLD or any other signal of interest */ |
| 7837 | /* sigtimedwait with infinite timeout: */ |
| 7838 | sig = sigwaitinfo(&G.blocked_set, NULL); |
| 7839 | if (sig > 0) { |
| 7840 | sig = check_and_run_traps(sig); |
| 7841 | if (sig && sig != SIGCHLD) { /* see note 2 */ |
| 7842 | ret = 128 + sig; |
| 7843 | break; |
| 7844 | } |
| 7845 | } |
| 7846 | } |
| 7847 | sigdelset(&G.blocked_set, SIGCHLD); |
| 7848 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7849 | return ret; |
| 7850 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7851 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7852 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7853 | while (*argv) { |
| 7854 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 7855 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7856 | /* mimic bash message */ |
| 7857 | 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] | 7858 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7859 | } |
| 7860 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7861 | if (WIFSIGNALED(status)) |
| 7862 | ret = 128 + WTERMSIG(status); |
| 7863 | else if (WIFEXITED(status)) |
| 7864 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7865 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7866 | ret = EXIT_FAILURE; |
| 7867 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7868 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7869 | ret = 127; |
| 7870 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7871 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7872 | } |
| 7873 | |
| 7874 | return ret; |
| 7875 | } |
| 7876 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7877 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 7878 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 7879 | { |
| 7880 | if (argv[1]) { |
| 7881 | def = bb_strtou(argv[1], NULL, 10); |
| 7882 | if (errno || def < def_min || argv[2]) { |
| 7883 | bb_error_msg("%s: bad arguments", argv[0]); |
| 7884 | def = UINT_MAX; |
| 7885 | } |
| 7886 | } |
| 7887 | return def; |
| 7888 | } |
| 7889 | #endif |
| 7890 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 7891 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7892 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7893 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7894 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7895 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 7896 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 7897 | return EXIT_SUCCESS; /* bash compat */ |
| 7898 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7899 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7900 | |
| 7901 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 7902 | if (depth == UINT_MAX) |
| 7903 | G.flag_break_continue = BC_BREAK; |
| 7904 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7905 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7906 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7907 | return EXIT_SUCCESS; |
| 7908 | } |
| 7909 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7910 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7911 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 7912 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 7913 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7914 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 7915 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7916 | |
| 7917 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7918 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7919 | { |
| 7920 | int rc; |
| 7921 | |
| 7922 | if (G.flag_return_in_progress != -1) { |
| 7923 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 7924 | return EXIT_FAILURE; /* bash compat */ |
| 7925 | } |
| 7926 | |
| 7927 | G.flag_return_in_progress = 1; |
| 7928 | |
| 7929 | /* bash: |
| 7930 | * out of range: wraps around at 256, does not error out |
| 7931 | * non-numeric param: |
| 7932 | * f() { false; return qwe; }; f; echo $? |
| 7933 | * bash: return: qwe: numeric argument required <== we do this |
| 7934 | * 255 <== we also do this |
| 7935 | */ |
| 7936 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 7937 | return rc; |
| 7938 | } |
| 7939 | #endif |