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" |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 89 | #ifndef PIPE_BUF |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 90 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 91 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 92 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 93 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 94 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 95 | #define LEAK_HUNTING 0 |
| 96 | #define BUILD_AS_NOMMU 0 |
| 97 | /* Enable/disable sanity checks. Ok to enable in production, |
| 98 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 99 | * Keeping 1 for now even in released versions. |
| 100 | */ |
| 101 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 102 | /* Slightly bigger (+200 bytes), but faster hush. |
| 103 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 104 | * which allows us to do fewer waitpid's. |
| 105 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 106 | * and therefore waitpid will return the same result as last time) |
| 107 | */ |
| 108 | #define ENABLE_HUSH_FAST 0 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 109 | |
| 110 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 111 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 112 | # undef BB_MMU |
| 113 | # undef USE_FOR_NOMMU |
| 114 | # undef USE_FOR_MMU |
| 115 | # define BB_MMU 0 |
| 116 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 117 | # define USE_FOR_MMU(...) |
| 118 | #endif |
| 119 | |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 120 | #if defined SINGLE_APPLET_MAIN |
| 121 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 122 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 123 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 124 | # undef IF_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 125 | # define IF_FEATURE_SH_STANDALONE(...) |
| 126 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denys Vlasenko | 673e945 | 2009-05-27 14:39:35 +0200 | [diff] [blame] | 127 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 128 | #endif |
| 129 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 130 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 131 | # undef ENABLE_FEATURE_EDITING |
| 132 | # define ENABLE_FEATURE_EDITING 0 |
| 133 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 134 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 135 | #endif |
| 136 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 137 | /* Do we support ANY keywords? */ |
| 138 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 139 | # define HAS_KEYWORDS 1 |
| 140 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 141 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 142 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 143 | # define HAS_KEYWORDS 0 |
| 144 | # define IF_HAS_KEYWORDS(...) |
| 145 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 146 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 147 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 148 | /* If you comment out one of these below, it will be #defined later |
| 149 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 150 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 151 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 152 | #define debug_printf_parse(...) do {} while (0) |
| 153 | #define debug_print_tree(a, b) do {} while (0) |
| 154 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 155 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 156 | #define debug_printf_jobs(...) do {} while (0) |
| 157 | #define debug_printf_expand(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 158 | #define debug_printf_glob(...) do {} while (0) |
| 159 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 160 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 161 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 162 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 163 | #define ERR_PTR ((void*)(long)1) |
| 164 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 165 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 166 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 167 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 168 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 169 | struct variable; |
| 170 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 171 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 172 | |
| 173 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 174 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 175 | */ |
| 176 | #if !BB_MMU |
| 177 | typedef struct nommu_save_t { |
| 178 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 179 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 180 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 181 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 182 | } nommu_save_t; |
| 183 | #endif |
| 184 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 185 | typedef enum reserved_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 186 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 187 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 188 | RES_IF , |
| 189 | RES_THEN , |
| 190 | RES_ELIF , |
| 191 | RES_ELSE , |
| 192 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 193 | #endif |
| 194 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 195 | RES_FOR , |
| 196 | RES_WHILE , |
| 197 | RES_UNTIL , |
| 198 | RES_DO , |
| 199 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 200 | #endif |
| 201 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 202 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 203 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 204 | #if ENABLE_HUSH_CASE |
| 205 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 206 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 207 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 208 | RES_MATCH , /* "word)" */ |
| 209 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 210 | RES_ESAC , |
| 211 | #endif |
| 212 | RES_XXXX , |
| 213 | RES_SNTX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 214 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 215 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 216 | typedef struct o_string { |
| 217 | char *data; |
| 218 | int length; /* position where data is appended */ |
| 219 | int maxlen; |
| 220 | /* Protect newly added chars against globbing |
| 221 | * (by prepending \ to *, ?, [, \) */ |
| 222 | smallint o_escape; |
| 223 | smallint o_glob; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 224 | /* At least some part of the string was inside '' or "", |
| 225 | * possibly empty one: word"", wo''rd etc. */ |
| 226 | smallint o_quoted; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 227 | smallint has_empty_slot; |
| 228 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 229 | } o_string; |
| 230 | enum { |
| 231 | MAYBE_ASSIGNMENT = 0, |
| 232 | DEFINITELY_ASSIGNMENT = 1, |
| 233 | NOT_ASSIGNMENT = 2, |
| 234 | WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */ |
| 235 | }; |
| 236 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 237 | #define NULL_O_STRING { NULL } |
| 238 | |
| 239 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
| 240 | * available? Where is it documented? */ |
| 241 | typedef struct in_str { |
| 242 | const char *p; |
| 243 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 244 | char eof_flag; /* meaningless if ->p == NULL */ |
| 245 | char peek_buf[2]; |
| 246 | #if ENABLE_HUSH_INTERACTIVE |
| 247 | smallint promptme; |
| 248 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 249 | #endif |
| 250 | FILE *file; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 251 | int (*get) (struct in_str *) FAST_FUNC; |
| 252 | int (*peek) (struct in_str *) FAST_FUNC; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 253 | } in_str; |
| 254 | #define i_getch(input) ((input)->get(input)) |
| 255 | #define i_peek(input) ((input)->peek(input)) |
| 256 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 257 | /* The descrip member of this structure is only used to make |
| 258 | * debugging output pretty */ |
| 259 | static const struct { |
| 260 | int mode; |
| 261 | signed char default_fd; |
| 262 | char descrip[3]; |
| 263 | } redir_table[] = { |
| 264 | { O_RDONLY, 0, "<" }, |
| 265 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 266 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 267 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 268 | { O_RDONLY, 0, "<<" }, |
| 269 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 270 | /* { O_RDONLY, 77, "<<" }, */ |
| 271 | }; |
| 272 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 273 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 274 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 275 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 276 | int rd_fd; /* fd to redirect */ |
| 277 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 278 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 279 | smallint rd_type; /* (enum redir_type) */ |
| 280 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 281 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 282 | * bit 0: do we need to trim leading tabs? |
| 283 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 284 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 285 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 286 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 287 | REDIRECT_INPUT = 0, |
| 288 | REDIRECT_OVERWRITE = 1, |
| 289 | REDIRECT_APPEND = 2, |
| 290 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 291 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 292 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 293 | |
| 294 | REDIRFD_CLOSE = -3, |
| 295 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 296 | REDIRFD_TO_FILE = -1, |
| 297 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 298 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 299 | HEREDOC_SKIPTABS = 1, |
| 300 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 301 | } redir_type; |
| 302 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 303 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 304 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 305 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 306 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 307 | smallint is_stopped; /* is the command currently running? */ |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 308 | smallint cmd_type; /* CMD_xxx */ |
| 309 | #define CMD_NORMAL 0 |
| 310 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 311 | |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 312 | /* used for "[[ EXPR ]]" */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 313 | #if ENABLE_HUSH_BASH_COMPAT |
| 314 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 315 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 316 | |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 317 | /* used for "export noglob=* glob* a=`echo a b`" */ |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 318 | //#define CMD_SINGLEWORD_NOGLOB_COND 3 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 319 | // It is hard to implement correctly, it adds significant amounts of tricky code, |
| 320 | // and all this is only useful for really obscure export statements |
| 321 | // almost nobody would use anyway. #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 322 | // guards the code which implements it, but I have doubts it works |
| 323 | // in all cases (especially with mixed globbed/non-globbed arguments) |
| 324 | |
| 325 | #if ENABLE_HUSH_FUNCTIONS |
| 326 | # define CMD_FUNCDEF 3 |
| 327 | #endif |
| 328 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 329 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 330 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 331 | #if !BB_MMU |
| 332 | char *group_as_string; |
| 333 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 334 | #if ENABLE_HUSH_FUNCTIONS |
| 335 | struct function *child_func; |
| 336 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 337 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 338 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 339 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 340 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 341 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 342 | * we put those fields back into cmd->xxx |
| 343 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 344 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 345 | * Without this trick, loop would execute a;b;b;b;... |
| 346 | * instead of correct sequence a;b;a;b;... |
| 347 | * When command is freed, it severs the link |
| 348 | * (sets ->child_func->parent_cmd to NULL). |
| 349 | */ |
| 350 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 351 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 352 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 353 | * and on execution these are substituted with their values. |
| 354 | * Substitution can make _several_ words out of one argv[n]! |
| 355 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 356 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 357 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 358 | struct redir_struct *redirects; /* I/O redirections */ |
| 359 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 360 | /* Is there anything in this command at all? */ |
| 361 | #define IS_NULL_CMD(cmd) \ |
| 362 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 363 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 364 | |
| 365 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 366 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 367 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 368 | int alive_cmds; /* number of commands running (not exited) */ |
| 369 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 370 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 371 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 372 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 373 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 374 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 375 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 376 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 377 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 378 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 379 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 380 | typedef enum pipe_style { |
| 381 | PIPE_SEQ = 1, |
| 382 | PIPE_AND = 2, |
| 383 | PIPE_OR = 3, |
| 384 | PIPE_BG = 4, |
| 385 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 386 | /* Is there anything in this pipe at all? */ |
| 387 | #define IS_NULL_PIPE(pi) \ |
| 388 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 389 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 390 | /* This holds pointers to the various results of parsing */ |
| 391 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 392 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 393 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 394 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 395 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 396 | /* last command in pipe (being constructed right now) */ |
| 397 | struct command *command; |
| 398 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 399 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 400 | #if !BB_MMU |
| 401 | o_string as_string; |
| 402 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 403 | #if HAS_KEYWORDS |
| 404 | smallint ctx_res_w; |
| 405 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 406 | #if ENABLE_HUSH_CASE |
| 407 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 408 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 409 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 410 | int old_flag; |
| 411 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 412 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 413 | * when we see "if" or "then", we malloc and copy current context, |
| 414 | * and make ->stack point to it. then we parse pipeN. |
| 415 | * when closing "then" / fi" / whatever is found, |
| 416 | * we move list_head into ->stack->command->group, |
| 417 | * copy ->stack into current context, and delete ->stack. |
| 418 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 419 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 420 | struct parse_context *stack; |
| 421 | #endif |
| 422 | }; |
| 423 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 424 | /* On program start, environ points to initial environment. |
| 425 | * putenv adds new pointers into it, unsetenv removes them. |
| 426 | * Neither of these (de)allocates the strings. |
| 427 | * setenv allocates new strings in malloc space and does putenv, |
| 428 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 429 | #define setenv(...) setenv_is_leaky_dont_use() |
| 430 | struct variable { |
| 431 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 432 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 433 | #if ENABLE_HUSH_LOCAL |
| 434 | unsigned func_nest_level; |
| 435 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 436 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 437 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 438 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 439 | }; |
| 440 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 441 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 442 | BC_BREAK = 1, |
| 443 | BC_CONTINUE = 2, |
| 444 | }; |
| 445 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 446 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 447 | struct function { |
| 448 | struct function *next; |
| 449 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 450 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 451 | struct pipe *body; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 452 | #if !BB_MMU |
| 453 | char *body_as_string; |
| 454 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 455 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 456 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 457 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 458 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 459 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 460 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 461 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 462 | /* interactive_fd != 0 means we are an interactive shell. |
| 463 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 464 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 465 | * job control still works, but terminal signals |
| 466 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 467 | * process groups can only be created with "cmd &". |
| 468 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 469 | * to give tty to the foreground process group, |
| 470 | * and will take it back when the group is stopped (^Z) |
| 471 | * or killed (^C). |
| 472 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 473 | #if ENABLE_HUSH_INTERACTIVE |
| 474 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 475 | * _AND_ if we decided to act interactively */ |
| 476 | int interactive_fd; |
| 477 | const char *PS1; |
| 478 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 479 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 480 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 481 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 482 | #endif |
| 483 | #if ENABLE_FEATURE_EDITING |
| 484 | line_input_t *line_input_state; |
| 485 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 486 | pid_t root_pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 487 | pid_t last_bg_pid; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 488 | #if ENABLE_HUSH_JOB |
| 489 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 490 | int last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 491 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 492 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 493 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 494 | #else |
| 495 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 496 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 497 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 498 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 499 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 500 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 501 | #if ENABLE_HUSH_FUNCTIONS |
| 502 | /* 0: outside of a function (or sourced file) |
| 503 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 504 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 505 | */ |
| 506 | smallint flag_return_in_progress; |
| 507 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 508 | smallint fake_mode; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 509 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 510 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 511 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 512 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 513 | smalluint global_args_malloced; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 514 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 515 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 516 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 517 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 518 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 519 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 520 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 521 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 522 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 523 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 524 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 525 | const char *cwd; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 526 | struct variable *top_var; /* = &G.shell_ver (set in main()) */ |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 527 | struct variable shell_ver; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 528 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 529 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 530 | # if ENABLE_HUSH_LOCAL |
| 531 | struct variable **shadowed_vars_pp; |
| 532 | unsigned func_nest_level; |
| 533 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 534 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 535 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 536 | #if ENABLE_HUSH_FAST |
| 537 | unsigned count_SIGCHLD; |
| 538 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 539 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 540 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 541 | /* which signals have non-DFL handler (even with no traps set)? */ |
| 542 | unsigned non_DFL_mask; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 543 | char **traps; /* char *traps[NSIG] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 544 | sigset_t blocked_set; |
| 545 | sigset_t inherited_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 546 | #if HUSH_DEBUG |
| 547 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 548 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 549 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 550 | char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2]; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 551 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 552 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 553 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 554 | * (too many defines). Also, I actually prefer to see when a variable |
| 555 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 556 | #define INIT_G() do { \ |
| 557 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 558 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 559 | |
| 560 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 561 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 562 | static int builtin_cd(char **argv) FAST_FUNC; |
| 563 | static int builtin_echo(char **argv) FAST_FUNC; |
| 564 | static int builtin_eval(char **argv) FAST_FUNC; |
| 565 | static int builtin_exec(char **argv) FAST_FUNC; |
| 566 | static int builtin_exit(char **argv) FAST_FUNC; |
| 567 | static int builtin_export(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 568 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 569 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 570 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 571 | #endif |
| 572 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 573 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 574 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 575 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 576 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 577 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 578 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 579 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 580 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 581 | static int builtin_pwd(char **argv) FAST_FUNC; |
| 582 | static int builtin_read(char **argv) FAST_FUNC; |
| 583 | static int builtin_set(char **argv) FAST_FUNC; |
| 584 | static int builtin_shift(char **argv) FAST_FUNC; |
| 585 | static int builtin_source(char **argv) FAST_FUNC; |
| 586 | static int builtin_test(char **argv) FAST_FUNC; |
| 587 | static int builtin_trap(char **argv) FAST_FUNC; |
| 588 | static int builtin_type(char **argv) FAST_FUNC; |
| 589 | static int builtin_true(char **argv) FAST_FUNC; |
| 590 | static int builtin_umask(char **argv) FAST_FUNC; |
| 591 | static int builtin_unset(char **argv) FAST_FUNC; |
| 592 | static int builtin_wait(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 593 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 594 | static int builtin_break(char **argv) FAST_FUNC; |
| 595 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 596 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 597 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 598 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 599 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 600 | |
| 601 | /* Table of built-in functions. They can be forked or not, depending on |
| 602 | * context: within pipes, they fork. As simple commands, they do not. |
| 603 | * When used in non-forking context, they can change global variables |
| 604 | * in the parent shell process. If forked, of course they cannot. |
| 605 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 606 | * still be set at the end. */ |
| 607 | struct built_in_command { |
| 608 | const char *cmd; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 609 | int (*function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 610 | #if ENABLE_HUSH_HELP |
| 611 | const char *descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 612 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 613 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 614 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 615 | #endif |
| 616 | }; |
| 617 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 618 | static const struct built_in_command bltins1[] = { |
| 619 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 620 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 621 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 622 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 623 | #endif |
| 624 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 625 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 626 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 627 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 628 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 629 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 630 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 631 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 632 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 633 | BLTIN("exit" , builtin_exit , "Exit"), |
| 634 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 635 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 636 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 637 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 638 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 639 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 640 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 641 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 642 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 643 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 644 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 645 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 646 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 647 | #if HUSH_DEBUG |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 648 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 649 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 650 | BLTIN("read" , builtin_read , "Input into variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 651 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 652 | BLTIN("return" , builtin_return , "Return from a function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 653 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 654 | BLTIN("set" , builtin_set , "Set/unset positional parameters"), |
| 655 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
| 656 | BLTIN("trap" , builtin_trap , "Trap signals"), |
| 657 | BLTIN("type" , builtin_type , "Write a description of command type"), |
| 658 | // BLTIN("ulimit" , builtin_ulimit , "Control resource limits"), |
| 659 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 660 | BLTIN("unset" , builtin_unset , "Unset variables"), |
| 661 | BLTIN("wait" , builtin_wait , "Wait for process"), |
| 662 | }; |
| 663 | /* For now, echo and test are unconditionally enabled. |
| 664 | * Maybe make it configurable? */ |
| 665 | static const struct built_in_command bltins2[] = { |
| 666 | BLTIN("[" , builtin_test , NULL), |
| 667 | BLTIN("echo" , builtin_echo , NULL), |
| 668 | BLTIN("pwd" , builtin_pwd , NULL), |
| 669 | BLTIN("test" , builtin_test , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 670 | }; |
| 671 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 672 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 673 | /* Debug printouts. |
| 674 | */ |
| 675 | #if HUSH_DEBUG |
| 676 | /* prevent disasters with G.debug_indent < 0 */ |
| 677 | # define indent() fprintf(stderr, "%*s", (G.debug_indent * 2) & 0xff, "") |
| 678 | # define debug_enter() (G.debug_indent++) |
| 679 | # define debug_leave() (G.debug_indent--) |
| 680 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 681 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 682 | # define debug_enter() ((void)0) |
| 683 | # define debug_leave() ((void)0) |
| 684 | #endif |
| 685 | |
| 686 | #ifndef debug_printf |
| 687 | # define debug_printf(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 688 | #endif |
| 689 | |
| 690 | #ifndef debug_printf_parse |
| 691 | # define debug_printf_parse(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 692 | #endif |
| 693 | |
| 694 | #ifndef debug_printf_exec |
| 695 | #define debug_printf_exec(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 696 | #endif |
| 697 | |
| 698 | #ifndef debug_printf_env |
| 699 | # define debug_printf_env(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 700 | #endif |
| 701 | |
| 702 | #ifndef debug_printf_jobs |
| 703 | # define debug_printf_jobs(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 704 | # define DEBUG_JOBS 1 |
| 705 | #else |
| 706 | # define DEBUG_JOBS 0 |
| 707 | #endif |
| 708 | |
| 709 | #ifndef debug_printf_expand |
| 710 | # define debug_printf_expand(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 711 | # define DEBUG_EXPAND 1 |
| 712 | #else |
| 713 | # define DEBUG_EXPAND 0 |
| 714 | #endif |
| 715 | |
| 716 | #ifndef debug_printf_glob |
| 717 | # define debug_printf_glob(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 718 | # define DEBUG_GLOB 1 |
| 719 | #else |
| 720 | # define DEBUG_GLOB 0 |
| 721 | #endif |
| 722 | |
| 723 | #ifndef debug_printf_list |
| 724 | # define debug_printf_list(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 725 | #endif |
| 726 | |
| 727 | #ifndef debug_printf_subst |
| 728 | # define debug_printf_subst(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 729 | #endif |
| 730 | |
| 731 | #ifndef debug_printf_clean |
| 732 | # define debug_printf_clean(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 733 | # define DEBUG_CLEAN 1 |
| 734 | #else |
| 735 | # define DEBUG_CLEAN 0 |
| 736 | #endif |
| 737 | |
| 738 | #if DEBUG_EXPAND |
| 739 | static void debug_print_strings(const char *prefix, char **vv) |
| 740 | { |
| 741 | indent(); |
| 742 | fprintf(stderr, "%s:\n", prefix); |
| 743 | while (*vv) |
| 744 | fprintf(stderr, " '%s'\n", *vv++); |
| 745 | } |
| 746 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 747 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 748 | #endif |
| 749 | |
| 750 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 751 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 752 | */ |
| 753 | #if LEAK_HUNTING |
| 754 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 755 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 756 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 757 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 758 | return ptr; |
| 759 | } |
| 760 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 761 | { |
| 762 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 763 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 764 | return ptr; |
| 765 | } |
| 766 | static char *xxstrdup(int lineno, const char *str) |
| 767 | { |
| 768 | char *ptr = xstrdup(str); |
| 769 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 770 | return ptr; |
| 771 | } |
| 772 | static void xxfree(void *ptr) |
| 773 | { |
| 774 | fdprintf(2, "free %p\n", ptr); |
| 775 | free(ptr); |
| 776 | } |
| 777 | #define xmalloc(s) xxmalloc(__LINE__, s) |
| 778 | #define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 779 | #define xstrdup(s) xxstrdup(__LINE__, s) |
| 780 | #define free(p) xxfree(p) |
| 781 | #endif |
| 782 | |
| 783 | |
| 784 | /* Syntax and runtime errors. They always abort scripts. |
| 785 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 786 | * and return to the prompt. |
| 787 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 788 | */ |
| 789 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 790 | # define die_if_script(lineno, ...) die_if_script(__VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 791 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 792 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 793 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 794 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 795 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 796 | #endif |
| 797 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 798 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 799 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 800 | va_list p; |
| 801 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 802 | #if HUSH_DEBUG >= 2 |
| 803 | bb_error_msg("hush.c:%u", lineno); |
| 804 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 805 | va_start(p, fmt); |
| 806 | bb_verror_msg(fmt, p, NULL); |
| 807 | va_end(p); |
| 808 | if (!G_interactive_fd) |
| 809 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 810 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 811 | |
| 812 | static void syntax_error(unsigned lineno, const char *msg) |
| 813 | { |
| 814 | if (msg) |
| 815 | die_if_script(lineno, "syntax error: %s", msg); |
| 816 | else |
| 817 | die_if_script(lineno, "syntax error", NULL); |
| 818 | } |
| 819 | |
| 820 | static void syntax_error_at(unsigned lineno, const char *msg) |
| 821 | { |
| 822 | die_if_script(lineno, "syntax error at '%s'", msg); |
| 823 | } |
| 824 | |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 825 | static void syntax_error_unterm_str(unsigned lineno, const char *s) |
| 826 | { |
| 827 | die_if_script(lineno, "syntax error: unterminated %s", s); |
| 828 | } |
| 829 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 830 | /* It so happens that all such cases are totally fatal |
| 831 | * even if shell is interactive: EOF while looking for closing |
| 832 | * delimiter. There is nowhere to read stuff from after that, |
| 833 | * it's EOF! The only choice is to terminate. |
| 834 | */ |
| 835 | static void syntax_error_unterm_ch(unsigned lineno, char ch) NORETURN; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 836 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 837 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 838 | char msg[2] = { ch, '\0' }; |
| 839 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 840 | xfunc_die(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 843 | static void syntax_error_unexpected_ch(unsigned lineno, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 844 | { |
| 845 | char msg[2]; |
| 846 | msg[0] = ch; |
| 847 | msg[1] = '\0'; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 848 | die_if_script(lineno, "syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 851 | #if HUSH_DEBUG < 2 |
| 852 | # undef die_if_script |
| 853 | # undef syntax_error |
| 854 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 855 | # undef syntax_error_unterm_ch |
| 856 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 857 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 858 | #else |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 859 | # define die_if_script(...) die_if_script(__LINE__, __VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 860 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 861 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 862 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 863 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 864 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 865 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 866 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 867 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 868 | #if ENABLE_HUSH_INTERACTIVE |
| 869 | static void cmdedit_update_prompt(void); |
| 870 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 871 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 872 | #endif |
| 873 | |
| 874 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 875 | /* Utility functions |
| 876 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 877 | static int glob_needed(const char *s) |
| 878 | { |
| 879 | while (*s) { |
| 880 | if (*s == '\\') |
| 881 | s++; |
| 882 | if (*s == '*' || *s == '[' || *s == '?') |
| 883 | return 1; |
| 884 | s++; |
| 885 | } |
| 886 | return 0; |
| 887 | } |
| 888 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 889 | static int is_well_formed_var_name(const char *s, char terminator) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 890 | { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 891 | if (!s || !(isalpha(*s) || *s == '_')) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 892 | return 0; |
| 893 | s++; |
| 894 | while (isalnum(*s) || *s == '_') |
| 895 | s++; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 896 | return *s == terminator; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 899 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 900 | static char *unbackslash(char *src) |
| 901 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame^] | 902 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 903 | while (1) { |
| 904 | if (*src == '\\') |
| 905 | src++; |
| 906 | if ((*dst++ = *src++) == '\0') |
| 907 | break; |
| 908 | } |
| 909 | return dst; |
| 910 | } |
| 911 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 912 | 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] | 913 | { |
| 914 | int i; |
| 915 | unsigned count1; |
| 916 | unsigned count2; |
| 917 | char **v; |
| 918 | |
| 919 | v = strings; |
| 920 | count1 = 0; |
| 921 | if (v) { |
| 922 | while (*v) { |
| 923 | count1++; |
| 924 | v++; |
| 925 | } |
| 926 | } |
| 927 | count2 = 0; |
| 928 | v = add; |
| 929 | while (*v) { |
| 930 | count2++; |
| 931 | v++; |
| 932 | } |
| 933 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 934 | v[count1 + count2] = NULL; |
| 935 | i = count2; |
| 936 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 937 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 938 | return v; |
| 939 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 940 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 941 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 942 | { |
| 943 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 944 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 945 | return ptr; |
| 946 | } |
| 947 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 948 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 949 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 950 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 951 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 952 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 953 | { |
| 954 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 955 | v[0] = add; |
| 956 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 957 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 958 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 959 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 960 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 961 | { |
| 962 | char **ptr = add_string_to_strings(strings, add); |
| 963 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 964 | return ptr; |
| 965 | } |
| 966 | #define add_string_to_strings(strings, add) \ |
| 967 | xx_add_string_to_strings(__LINE__, strings, add) |
| 968 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 969 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 970 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 971 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 972 | char **v; |
| 973 | |
| 974 | if (!strings) |
| 975 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 976 | v = strings; |
| 977 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 978 | free(*v); |
| 979 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 980 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 981 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 984 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 985 | /* Helpers for setting new $n and restoring them back |
| 986 | */ |
| 987 | typedef struct save_arg_t { |
| 988 | char *sv_argv0; |
| 989 | char **sv_g_argv; |
| 990 | int sv_g_argc; |
| 991 | smallint sv_g_malloced; |
| 992 | } save_arg_t; |
| 993 | |
| 994 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 995 | { |
| 996 | int n; |
| 997 | |
| 998 | sv->sv_argv0 = argv[0]; |
| 999 | sv->sv_g_argv = G.global_argv; |
| 1000 | sv->sv_g_argc = G.global_argc; |
| 1001 | sv->sv_g_malloced = G.global_args_malloced; |
| 1002 | |
| 1003 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1004 | G.global_argv = argv; |
| 1005 | G.global_args_malloced = 0; |
| 1006 | |
| 1007 | n = 1; |
| 1008 | while (*++argv) |
| 1009 | n++; |
| 1010 | G.global_argc = n; |
| 1011 | } |
| 1012 | |
| 1013 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1014 | { |
| 1015 | char **pp; |
| 1016 | |
| 1017 | if (G.global_args_malloced) { |
| 1018 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 1019 | pp = G.global_argv; |
| 1020 | while (*++pp) /* note: does not free $0 */ |
| 1021 | free(*pp); |
| 1022 | free(G.global_argv); |
| 1023 | } |
| 1024 | argv[0] = sv->sv_argv0; |
| 1025 | G.global_argv = sv->sv_g_argv; |
| 1026 | G.global_argc = sv->sv_g_argc; |
| 1027 | G.global_args_malloced = sv->sv_g_malloced; |
| 1028 | } |
| 1029 | |
| 1030 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1031 | /* Basic theory of signal handling in shell |
| 1032 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1033 | * This does not describe what hush does, rather, it is current understanding |
| 1034 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1035 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1036 | * |
| 1037 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1038 | * is finished or backgrounded. It is the same in interactive and |
| 1039 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1040 | * 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] | 1041 | * ^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] | 1042 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1043 | * pipe. |
| 1044 | * |
| 1045 | * Wait builtin in interruptible by signals for which user trap is set |
| 1046 | * or by SIGINT in interactive shell. |
| 1047 | * |
| 1048 | * Trap handlers will execute even within trap handlers. (right?) |
| 1049 | * |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1050 | * User trap handlers are forgotten when subshell ("(cmd)") is entered. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1051 | * |
| 1052 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1053 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1054 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1055 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1056 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1057 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1058 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1059 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1060 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1061 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1062 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1063 | * |
| 1064 | * SIGQUIT: ignore |
| 1065 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1066 | * SIGHUP (interactive): |
| 1067 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1068 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1069 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1070 | * that all pipe members are stopped. Try this in bash: |
| 1071 | * while :; do :; done - ^Z does not background it |
| 1072 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1073 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1074 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1075 | * to interactive shell while shell is waiting for a pipe, |
| 1076 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1077 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1078 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1079 | * Example 2: this does not wait and does not execute ls: |
| 1080 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1081 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1082 | * "sleep 5; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1083 | * |
| 1084 | * (What happens to signals which are IGN on shell start?) |
| 1085 | * (What happens with signal mask on shell start?) |
| 1086 | * |
| 1087 | * Implementation in hush |
| 1088 | * ====================== |
| 1089 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1090 | * We block all signals which we don't want to take action immediately, |
| 1091 | * i.e. we block all signals which need to have special handling as described |
| 1092 | * above, and all signals which have traps set. |
| 1093 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1094 | * and act on them. |
| 1095 | * |
| 1096 | * unsigned non_DFL_mask: a mask of such "special" signals |
| 1097 | * sigset_t blocked_set: current blocked signal set |
| 1098 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1099 | * "trap - SIGxxx": |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1100 | * 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] | 1101 | * "trap 'cmd' SIGxxx": |
| 1102 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1103 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1104 | * unblock signals with special interactive handling |
| 1105 | * (child shell is not interactive), |
| 1106 | * unset all traps (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1107 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1108 | * 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] | 1109 | * 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] | 1110 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1111 | * 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] | 1112 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1113 | * and to restore tty pgrp on signal-induced exit. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1114 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1115 | enum { |
| 1116 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1117 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1118 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1119 | | (1 << SIGHUP) |
| 1120 | , |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1121 | SPECIAL_JOB_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1122 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1123 | | (1 << SIGTTIN) |
| 1124 | | (1 << SIGTTOU) |
| 1125 | | (1 << SIGTSTP) |
| 1126 | #endif |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1127 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1128 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1129 | #if ENABLE_HUSH_FAST |
| 1130 | static void SIGCHLD_handler(int sig UNUSED_PARAM) |
| 1131 | { |
| 1132 | G.count_SIGCHLD++; |
| 1133 | //bb_error_msg("[%d] SIGCHLD_handler: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1134 | } |
| 1135 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1136 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1137 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1138 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1139 | /* After [v]fork, in child: do not restore tty pgrp on xfunc death */ |
| 1140 | #define disable_restore_tty_pgrp_on_exit() (die_sleep = 0) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1141 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1142 | #define enable_restore_tty_pgrp_on_exit() (die_sleep = -1) |
| 1143 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1144 | /* Restores tty foreground process group, and exits. |
| 1145 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1146 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1147 | * or called directly with -EXITCODE. |
| 1148 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1149 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1150 | static void sigexit(int sig) |
| 1151 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1152 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 1153 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1154 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1155 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1156 | * tty pgrp then, only top-level shell process does that */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1157 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 1158 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1159 | |
| 1160 | /* Not a signal, just exit */ |
| 1161 | if (sig <= 0) |
| 1162 | _exit(- sig); |
| 1163 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1164 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1165 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1166 | #else |
| 1167 | |
| 1168 | #define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1169 | #define enable_restore_tty_pgrp_on_exit() ((void)0) |
| 1170 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1171 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1172 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1173 | /* Restores tty foreground process group, and exits. */ |
| 1174 | static void hush_exit(int exitcode) NORETURN; |
| 1175 | static void hush_exit(int exitcode) |
| 1176 | { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1177 | if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) { |
| 1178 | /* Prevent recursion: |
| 1179 | * trap "echo Hi; exit" EXIT; exit |
| 1180 | */ |
| 1181 | char *argv[] = { NULL, G.traps[0], NULL }; |
| 1182 | G.traps[0] = NULL; |
| 1183 | G.exiting = 1; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1184 | builtin_eval(argv); |
| 1185 | free(argv[1]); |
| 1186 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1187 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1188 | #if ENABLE_HUSH_JOB |
| 1189 | fflush(NULL); /* flush all streams */ |
| 1190 | sigexit(- (exitcode & 0xff)); |
| 1191 | #else |
| 1192 | exit(exitcode); |
| 1193 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1196 | static int check_and_run_traps(int sig) |
| 1197 | { |
| 1198 | static const struct timespec zero_timespec = { 0, 0 }; |
| 1199 | smalluint save_rcode; |
| 1200 | int last_sig = 0; |
| 1201 | |
| 1202 | if (sig) |
| 1203 | goto jump_in; |
| 1204 | while (1) { |
| 1205 | sig = sigtimedwait(&G.blocked_set, NULL, &zero_timespec); |
| 1206 | if (sig <= 0) |
| 1207 | break; |
| 1208 | jump_in: |
| 1209 | last_sig = sig; |
| 1210 | if (G.traps && G.traps[sig]) { |
| 1211 | if (G.traps[sig][0]) { |
| 1212 | /* We have user-defined handler */ |
| 1213 | char *argv[] = { NULL, xstrdup(G.traps[sig]), NULL }; |
| 1214 | save_rcode = G.last_exitcode; |
| 1215 | builtin_eval(argv); |
| 1216 | free(argv[1]); |
| 1217 | G.last_exitcode = save_rcode; |
| 1218 | } /* else: "" trap, ignoring signal */ |
| 1219 | continue; |
| 1220 | } |
| 1221 | /* not a trap: special action */ |
| 1222 | switch (sig) { |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1223 | #if ENABLE_HUSH_FAST |
| 1224 | case SIGCHLD: |
| 1225 | G.count_SIGCHLD++; |
| 1226 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1227 | break; |
| 1228 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1229 | case SIGINT: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1230 | /* Builtin was ^C'ed, make it look prettier: */ |
| 1231 | bb_putchar('\n'); |
| 1232 | G.flag_SIGINT = 1; |
| 1233 | break; |
| 1234 | #if ENABLE_HUSH_JOB |
| 1235 | case SIGHUP: { |
| 1236 | struct pipe *job; |
| 1237 | /* bash is observed to signal whole process groups, |
| 1238 | * not individual processes */ |
| 1239 | for (job = G.job_list; job; job = job->next) { |
| 1240 | if (job->pgrp <= 0) |
| 1241 | continue; |
| 1242 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 1243 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 1244 | kill(- job->pgrp, SIGCONT); |
| 1245 | } |
| 1246 | sigexit(SIGHUP); |
| 1247 | } |
| 1248 | #endif |
| 1249 | default: /* ignored: */ |
| 1250 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
| 1251 | break; |
| 1252 | } |
| 1253 | } |
| 1254 | return last_sig; |
| 1255 | } |
| 1256 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1257 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1258 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1259 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1260 | if (force || G.cwd == NULL) { |
| 1261 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1262 | * we must not try to free(bb_msg_unknown) */ |
| 1263 | if (G.cwd == bb_msg_unknown) |
| 1264 | G.cwd = NULL; |
| 1265 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1266 | if (!G.cwd) |
| 1267 | G.cwd = bb_msg_unknown; |
| 1268 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1269 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1272 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1273 | /* |
| 1274 | * Shell and environment variable support |
| 1275 | */ |
| 1276 | static struct variable **get_ptr_to_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1277 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1278 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1279 | struct variable *cur; |
| 1280 | int len; |
| 1281 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1282 | len = strlen(name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1283 | pp = &G.top_var; |
| 1284 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1285 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1286 | return pp; |
| 1287 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1288 | } |
| 1289 | return NULL; |
| 1290 | } |
| 1291 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1292 | static struct variable *get_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1293 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1294 | struct variable **pp = get_ptr_to_local_var(name); |
| 1295 | if (pp) |
| 1296 | return *pp; |
| 1297 | return NULL; |
| 1298 | } |
| 1299 | |
| 1300 | static const char *get_local_var_value(const char *name) |
| 1301 | { |
| 1302 | struct variable **pp = get_ptr_to_local_var(name); |
| 1303 | if (pp) |
| 1304 | return strchr((*pp)->varstr, '=') + 1; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1305 | return NULL; |
| 1306 | } |
| 1307 | |
| 1308 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1309 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1310 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1311 | * 0: do not change export flag |
| 1312 | * (if creating new variable, flag will be 0) |
| 1313 | * 1: set export flag and putenv the variable |
| 1314 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1315 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1316 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1317 | #if !BB_MMU && ENABLE_HUSH_LOCAL |
| 1318 | /* all params are used */ |
| 1319 | #elif BB_MMU && ENABLE_HUSH_LOCAL |
| 1320 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1321 | set_local_var(str, flg_export, local_lvl) |
| 1322 | #elif BB_MMU && !ENABLE_HUSH_LOCAL |
| 1323 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1324 | set_local_var(str, flg_export) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1325 | #elif !BB_MMU && !ENABLE_HUSH_LOCAL |
| 1326 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1327 | set_local_var(str, flg_export, flg_read_only) |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1328 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1329 | 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] | 1330 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1331 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1332 | struct variable *cur; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1333 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1334 | int name_len; |
| 1335 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1336 | eq_sign = strchr(str, '='); |
| 1337 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1338 | free(str); |
| 1339 | return -1; |
| 1340 | } |
| 1341 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1342 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1343 | var_pp = &G.top_var; |
| 1344 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1345 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1346 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1347 | continue; |
| 1348 | } |
| 1349 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1350 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1351 | #if !BB_MMU |
| 1352 | if (!flg_read_only) |
| 1353 | #endif |
| 1354 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1355 | free(str); |
| 1356 | return -1; |
| 1357 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1358 | if (flg_export == -1) { // "&& cur->flg_export" ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1359 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1360 | *eq_sign = '\0'; |
| 1361 | unsetenv(str); |
| 1362 | *eq_sign = '='; |
| 1363 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1364 | #if ENABLE_HUSH_LOCAL |
| 1365 | if (cur->func_nest_level < local_lvl) { |
| 1366 | /* New variable is declared as local, |
| 1367 | * and existing one is global, or local |
| 1368 | * from enclosing function. |
| 1369 | * Remove and save old one: */ |
| 1370 | *var_pp = cur->next; |
| 1371 | cur->next = *G.shadowed_vars_pp; |
| 1372 | *G.shadowed_vars_pp = cur; |
| 1373 | /* bash 3.2.33(1) and exported vars: |
| 1374 | * # export z=z |
| 1375 | * # f() { local z=a; env | grep ^z; } |
| 1376 | * # f |
| 1377 | * z=a |
| 1378 | * # env | grep ^z |
| 1379 | * z=z |
| 1380 | */ |
| 1381 | if (cur->flg_export) |
| 1382 | flg_export = 1; |
| 1383 | break; |
| 1384 | } |
| 1385 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1386 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1387 | free_and_exp: |
| 1388 | free(str); |
| 1389 | goto exp; |
| 1390 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1391 | if (cur->max_len != 0) { |
| 1392 | if (cur->max_len >= strlen(str)) { |
| 1393 | /* This one is from startup env, reuse space */ |
| 1394 | strcpy(cur->varstr, str); |
| 1395 | goto free_and_exp; |
| 1396 | } |
| 1397 | } else { |
| 1398 | /* max_len == 0 signifies "malloced" var, which we can |
| 1399 | * (and has to) free */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1400 | free(cur->varstr); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1401 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1402 | cur->max_len = 0; |
| 1403 | goto set_str_and_exp; |
| 1404 | } |
| 1405 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1406 | /* Not found - create new variable struct */ |
| 1407 | cur = xzalloc(sizeof(*cur)); |
| 1408 | #if ENABLE_HUSH_LOCAL |
| 1409 | cur->func_nest_level = local_lvl; |
| 1410 | #endif |
| 1411 | cur->next = *var_pp; |
| 1412 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1413 | |
| 1414 | set_str_and_exp: |
| 1415 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1416 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1417 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1418 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1419 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1420 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1421 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1422 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1423 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1424 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1425 | if (flg_export == -1) { |
| 1426 | cur->flg_export = 0; |
| 1427 | /* unsetenv was already done */ |
| 1428 | } else { |
| 1429 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1430 | return putenv(cur->varstr); |
| 1431 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1432 | } |
| 1433 | return 0; |
| 1434 | } |
| 1435 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1436 | /* Used at startup and after each cd */ |
| 1437 | static void set_pwd_var(int exp) |
| 1438 | { |
| 1439 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), |
| 1440 | /*exp:*/ exp, /*lvl:*/ 0, /*ro:*/ 0); |
| 1441 | } |
| 1442 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1443 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1444 | { |
| 1445 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1446 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1447 | |
| 1448 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1449 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1450 | var_pp = &G.top_var; |
| 1451 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1452 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1453 | if (cur->flg_read_only) { |
| 1454 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1455 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1456 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1457 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1458 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1459 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1460 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1461 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1462 | if (!cur->max_len) |
| 1463 | free(cur->varstr); |
| 1464 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1465 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1466 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1467 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1468 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1469 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1472 | static int unset_local_var(const char *name) |
| 1473 | { |
| 1474 | return unset_local_var_len(name, strlen(name)); |
| 1475 | } |
| 1476 | |
| 1477 | static void unset_vars(char **strings) |
| 1478 | { |
| 1479 | char **v; |
| 1480 | |
| 1481 | if (!strings) |
| 1482 | return; |
| 1483 | v = strings; |
| 1484 | while (*v) { |
| 1485 | const char *eq = strchrnul(*v, '='); |
| 1486 | unset_local_var_len(*v, (int)(eq - *v)); |
| 1487 | v++; |
| 1488 | } |
| 1489 | free(strings); |
| 1490 | } |
| 1491 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1492 | #if ENABLE_SH_MATH_SUPPORT |
| 1493 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1494 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 1495 | static char *endofname(const char *name) |
| 1496 | { |
| 1497 | char *p; |
| 1498 | |
| 1499 | p = (char *) name; |
| 1500 | if (!is_name(*p)) |
| 1501 | return p; |
| 1502 | while (*++p) { |
| 1503 | if (!is_in_name(*p)) |
| 1504 | break; |
| 1505 | } |
| 1506 | return p; |
| 1507 | } |
| 1508 | |
| 1509 | static void arith_set_local_var(const char *name, const char *val, int flags) |
| 1510 | { |
| 1511 | /* arith code doesnt malloc space, so do it for it */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1512 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1513 | set_local_var(var, flags, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1514 | } |
| 1515 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1516 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1517 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1518 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1519 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 1520 | */ |
| 1521 | static void add_vars(struct variable *var) |
| 1522 | { |
| 1523 | struct variable *next; |
| 1524 | |
| 1525 | while (var) { |
| 1526 | next = var->next; |
| 1527 | var->next = G.top_var; |
| 1528 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1529 | if (var->flg_export) { |
| 1530 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1531 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1532 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1533 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1534 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1535 | var = next; |
| 1536 | } |
| 1537 | } |
| 1538 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1539 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1540 | { |
| 1541 | char **s; |
| 1542 | struct variable *old = NULL; |
| 1543 | |
| 1544 | if (!strings) |
| 1545 | return old; |
| 1546 | s = strings; |
| 1547 | while (*s) { |
| 1548 | struct variable *var_p; |
| 1549 | struct variable **var_pp; |
| 1550 | char *eq; |
| 1551 | |
| 1552 | eq = strchr(*s, '='); |
| 1553 | if (eq) { |
| 1554 | *eq = '\0'; |
| 1555 | var_pp = get_ptr_to_local_var(*s); |
| 1556 | *eq = '='; |
| 1557 | if (var_pp) { |
| 1558 | /* Remove variable from global linked list */ |
| 1559 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1560 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1561 | *var_pp = var_p->next; |
| 1562 | /* Add it to returned list */ |
| 1563 | var_p->next = old; |
| 1564 | old = var_p; |
| 1565 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1566 | set_local_var(*s, /*exp:*/ 1, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1567 | } |
| 1568 | s++; |
| 1569 | } |
| 1570 | return old; |
| 1571 | } |
| 1572 | |
| 1573 | |
| 1574 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1575 | * in_str support |
| 1576 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1577 | static int FAST_FUNC static_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1578 | { |
| 1579 | int ch = *i->p++; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1580 | if (ch != '\0') |
| 1581 | return ch; |
| 1582 | i->p--; |
| 1583 | return EOF; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1586 | static int FAST_FUNC static_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1587 | { |
| 1588 | return *i->p; |
| 1589 | } |
| 1590 | |
| 1591 | #if ENABLE_HUSH_INTERACTIVE |
| 1592 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1593 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1594 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1595 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1596 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1597 | if (G.PS1 == NULL) |
| 1598 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1599 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1600 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1601 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1602 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1603 | if (G.PS2 == NULL) |
| 1604 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1605 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1606 | |
| 1607 | static const char* setup_prompt_string(int promptmode) |
| 1608 | { |
| 1609 | const char *prompt_str; |
| 1610 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1611 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1612 | /* Set up the prompt */ |
| 1613 | if (promptmode == 0) { /* PS1 */ |
| 1614 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1615 | /* bash uses $PWD value, even if it is set by user. |
| 1616 | * It uses current dir only if PWD is unset. |
| 1617 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1618 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1619 | prompt_str = G.PS1; |
| 1620 | } else |
| 1621 | prompt_str = G.PS2; |
| 1622 | } else |
| 1623 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1624 | debug_printf("result '%s'\n", prompt_str); |
| 1625 | return prompt_str; |
| 1626 | } |
| 1627 | |
| 1628 | static void get_user_input(struct in_str *i) |
| 1629 | { |
| 1630 | int r; |
| 1631 | const char *prompt_str; |
| 1632 | |
| 1633 | prompt_str = setup_prompt_string(i->promptmode); |
| 1634 | #if ENABLE_FEATURE_EDITING |
| 1635 | /* Enable command line editing only while a command line |
| 1636 | * is actually being read */ |
| 1637 | do { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1638 | G.flag_SIGINT = 0; |
| 1639 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 1640 | * only after <Enter>. (^C will work) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1641 | r = read_line_input(prompt_str, G.user_input_buf, BUFSIZ-1, G.line_input_state); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1642 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1643 | check_and_run_traps(0); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1644 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1645 | i->eof_flag = (r < 0); |
| 1646 | if (i->eof_flag) { /* EOF/error detected */ |
| 1647 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1648 | G.user_input_buf[1] = '\0'; |
| 1649 | } |
| 1650 | #else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1651 | do { |
| 1652 | G.flag_SIGINT = 0; |
| 1653 | fputs(prompt_str, stdout); |
| 1654 | fflush(stdout); |
| 1655 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1656 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1657 | //do we need check_and_run_traps(0)? (maybe only if stdin) |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1658 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1659 | i->eof_flag = (r == EOF); |
| 1660 | #endif |
| 1661 | i->p = G.user_input_buf; |
| 1662 | } |
| 1663 | |
| 1664 | #endif /* INTERACTIVE */ |
| 1665 | |
| 1666 | /* This is the magic location that prints prompts |
| 1667 | * and gets data back from the user */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1668 | static int FAST_FUNC file_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1669 | { |
| 1670 | int ch; |
| 1671 | |
| 1672 | /* If there is data waiting, eat it up */ |
| 1673 | if (i->p && *i->p) { |
| 1674 | #if ENABLE_HUSH_INTERACTIVE |
| 1675 | take_cached: |
| 1676 | #endif |
| 1677 | ch = *i->p++; |
| 1678 | if (i->eof_flag && !*i->p) |
| 1679 | ch = EOF; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1680 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1681 | } else { |
| 1682 | /* need to double check i->file because we might be doing something |
| 1683 | * more complicated by now, like sourcing or substituting. */ |
| 1684 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 1685 | if (G_interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1686 | do { |
| 1687 | get_user_input(i); |
| 1688 | } while (!*i->p); /* need non-empty line */ |
| 1689 | i->promptmode = 1; /* PS2 */ |
| 1690 | i->promptme = 0; |
| 1691 | goto take_cached; |
| 1692 | } |
| 1693 | #endif |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1694 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1695 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1696 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1697 | #if ENABLE_HUSH_INTERACTIVE |
| 1698 | if (ch == '\n') |
| 1699 | i->promptme = 1; |
| 1700 | #endif |
| 1701 | return ch; |
| 1702 | } |
| 1703 | |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1704 | /* All callers guarantee this routine will never |
| 1705 | * be used right after a newline, so prompting is not needed. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1706 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1707 | static int FAST_FUNC file_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1708 | { |
| 1709 | int ch; |
| 1710 | if (i->p && *i->p) { |
| 1711 | if (i->eof_flag && !i->p[1]) |
| 1712 | return EOF; |
| 1713 | return *i->p; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1714 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1715 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1716 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1717 | i->eof_flag = (ch == EOF); |
| 1718 | i->peek_buf[0] = ch; |
| 1719 | i->peek_buf[1] = '\0'; |
| 1720 | i->p = i->peek_buf; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1721 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1722 | return ch; |
| 1723 | } |
| 1724 | |
| 1725 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1726 | { |
| 1727 | i->peek = file_peek; |
| 1728 | i->get = file_get; |
| 1729 | #if ENABLE_HUSH_INTERACTIVE |
| 1730 | i->promptme = 1; |
| 1731 | i->promptmode = 0; /* PS1 */ |
| 1732 | #endif |
| 1733 | i->file = f; |
| 1734 | i->p = NULL; |
| 1735 | } |
| 1736 | |
| 1737 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1738 | { |
| 1739 | i->peek = static_peek; |
| 1740 | i->get = static_get; |
| 1741 | #if ENABLE_HUSH_INTERACTIVE |
| 1742 | i->promptme = 1; |
| 1743 | i->promptmode = 0; /* PS1 */ |
| 1744 | #endif |
| 1745 | i->p = s; |
| 1746 | i->eof_flag = 0; |
| 1747 | } |
| 1748 | |
| 1749 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1750 | /* |
| 1751 | * o_string support |
| 1752 | */ |
| 1753 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1754 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1755 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1756 | { |
| 1757 | o->length = 0; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 1758 | o->o_quoted = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1759 | if (o->data) |
| 1760 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1763 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1764 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1765 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1766 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1769 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 1770 | { |
| 1771 | free(o->data); |
| 1772 | } |
| 1773 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1774 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1775 | { |
| 1776 | if (o->length + len > o->maxlen) { |
| 1777 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 1778 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 1779 | } |
| 1780 | } |
| 1781 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1782 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1783 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1784 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 1785 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1786 | o->data[o->length] = ch; |
| 1787 | o->length++; |
| 1788 | o->data[o->length] = '\0'; |
| 1789 | } |
| 1790 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1791 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1792 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1793 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1794 | memcpy(&o->data[o->length], str, len); |
| 1795 | o->length += len; |
| 1796 | o->data[o->length] = '\0'; |
| 1797 | } |
| 1798 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1799 | #if !BB_MMU |
| 1800 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1801 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1802 | o_addblock(o, str, strlen(str)); |
| 1803 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1804 | static void nommu_addchr(o_string *o, int ch) |
| 1805 | { |
| 1806 | if (o) |
| 1807 | o_addchr(o, ch); |
| 1808 | } |
| 1809 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1810 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1811 | #endif |
| 1812 | |
| 1813 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 1814 | { |
| 1815 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1818 | 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] | 1819 | { |
| 1820 | while (len) { |
| 1821 | o_addchr(o, *str); |
| 1822 | if (*str++ == '\\' |
| 1823 | && (*str != '*' && *str != '?' && *str != '[') |
| 1824 | ) { |
| 1825 | o_addchr(o, '\\'); |
| 1826 | } |
| 1827 | len--; |
| 1828 | } |
| 1829 | } |
| 1830 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1831 | /* My analysis of quoting semantics tells me that state information |
| 1832 | * is associated with a destination, not a source. |
| 1833 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1834 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1835 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1836 | int sz = 1; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1837 | char *found = strchr("*?[\\", ch); |
| 1838 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1839 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1840 | o_grow_by(o, sz); |
| 1841 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1842 | o->data[o->length] = '\\'; |
| 1843 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1844 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1845 | o->data[o->length] = ch; |
| 1846 | o->length++; |
| 1847 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1850 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1851 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1852 | int sz = 1; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1853 | if (o->o_escape && strchr("*?[\\", ch)) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1854 | sz++; |
| 1855 | o->data[o->length] = '\\'; |
| 1856 | o->length++; |
| 1857 | } |
| 1858 | o_grow_by(o, sz); |
| 1859 | o->data[o->length] = ch; |
| 1860 | o->length++; |
| 1861 | o->data[o->length] = '\0'; |
| 1862 | } |
| 1863 | |
| 1864 | static void o_addQstr(o_string *o, const char *str, int len) |
| 1865 | { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1866 | if (!o->o_escape) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1867 | o_addblock(o, str, len); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1868 | return; |
| 1869 | } |
| 1870 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1871 | char ch; |
| 1872 | int sz; |
| 1873 | int ordinary_cnt = strcspn(str, "*?[\\"); |
| 1874 | if (ordinary_cnt > len) /* paranoia */ |
| 1875 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1876 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1877 | if (ordinary_cnt == len) |
| 1878 | return; |
| 1879 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1880 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1881 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1882 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1883 | sz = 1; |
| 1884 | if (ch) { /* it is necessarily one of "*?[\\" */ |
| 1885 | sz++; |
| 1886 | o->data[o->length] = '\\'; |
| 1887 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1888 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1889 | o_grow_by(o, sz); |
| 1890 | o->data[o->length] = ch; |
| 1891 | o->length++; |
| 1892 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1893 | } |
| 1894 | } |
| 1895 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1896 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1897 | * 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] | 1898 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1899 | * list[i] contains an INDEX (int!) into this string data. |
| 1900 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1901 | * but list[i]'s need not be modified. |
| 1902 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1903 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1904 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1905 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1906 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1907 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1908 | { |
| 1909 | char **list = (char**)o->data; |
| 1910 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1911 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1912 | |
| 1913 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1914 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1915 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1916 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1917 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1918 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1919 | o->data + (int)list[i] + string_start, |
| 1920 | o->data + (int)list[i] + string_start); |
| 1921 | i++; |
| 1922 | } |
| 1923 | if (n) { |
| 1924 | const char *p = o->data + (int)list[n - 1] + string_start; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1925 | indent(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1926 | 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] | 1927 | } |
| 1928 | } |
| 1929 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1930 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1931 | #endif |
| 1932 | |
| 1933 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1934 | * in list[n] so that it points past last stored byte so far. |
| 1935 | * It returns n+1. */ |
| 1936 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1937 | { |
| 1938 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1939 | int string_start; |
| 1940 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1941 | |
| 1942 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1943 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1944 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1945 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1946 | 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] | 1947 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1948 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1949 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 1950 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1951 | memmove(list + n + 0x10, list + n, string_len); |
| 1952 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1953 | } else { |
| 1954 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 1955 | n, string_len, string_start); |
| 1956 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1957 | } else { |
| 1958 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1959 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1960 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1961 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 1962 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1963 | o->has_empty_slot = 0; |
| 1964 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1965 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1966 | return n + 1; |
| 1967 | } |
| 1968 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1969 | /* "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] | 1970 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1971 | { |
| 1972 | char **list = (char**)o->data; |
| 1973 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1974 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1975 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1978 | /* o_glob performs globbing on last list[], saving each result |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1979 | * as a new list[]. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1980 | static int o_glob(o_string *o, int n) |
| 1981 | { |
| 1982 | glob_t globdata; |
| 1983 | int gr; |
| 1984 | char *pattern; |
| 1985 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1986 | 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] | 1987 | if (!o->data) |
| 1988 | return o_save_ptr_helper(o, n); |
| 1989 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1990 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1991 | if (!glob_needed(pattern)) { |
| 1992 | literal: |
| 1993 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1994 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1995 | return o_save_ptr_helper(o, n); |
| 1996 | } |
| 1997 | |
| 1998 | memset(&globdata, 0, sizeof(globdata)); |
| 1999 | gr = glob(pattern, 0, NULL, &globdata); |
| 2000 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 2001 | if (gr == GLOB_NOSPACE) |
| 2002 | bb_error_msg_and_die("out of memory during glob"); |
| 2003 | if (gr == GLOB_NOMATCH) { |
| 2004 | globfree(&globdata); |
| 2005 | goto literal; |
| 2006 | } |
| 2007 | if (gr != 0) { /* GLOB_ABORTED ? */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 2008 | /* TODO: testcase for bad glob pattern behavior */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2009 | bb_error_msg("glob(3) error %d on '%s'", gr, pattern); |
| 2010 | } |
| 2011 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2012 | char **argv = globdata.gl_pathv; |
| 2013 | o->length = pattern - o->data; /* "forget" pattern */ |
| 2014 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2015 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2016 | n = o_save_ptr_helper(o, n); |
| 2017 | argv++; |
| 2018 | if (!*argv) |
| 2019 | break; |
| 2020 | } |
| 2021 | } |
| 2022 | globfree(&globdata); |
| 2023 | if (DEBUG_GLOB) |
| 2024 | debug_print_list("o_glob returning", o, n); |
| 2025 | return n; |
| 2026 | } |
| 2027 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2028 | /* If o->o_glob == 1, glob the string so far remembered. |
| 2029 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2030 | static int o_save_ptr(o_string *o, int n) |
| 2031 | { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2032 | if (o->o_glob) { /* if globbing is requested */ |
| 2033 | /* If o->has_empty_slot, list[n] was already globbed |
| 2034 | * (if it was requested back then when it was filled) |
| 2035 | * so don't do that again! */ |
| 2036 | if (!o->has_empty_slot) |
| 2037 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 2038 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2039 | return o_save_ptr_helper(o, n); |
| 2040 | } |
| 2041 | |
| 2042 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2043 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2044 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2045 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2046 | int string_start; |
| 2047 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2048 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 2049 | if (DEBUG_EXPAND) |
| 2050 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2051 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2052 | list = (char**)o->data; |
| 2053 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2054 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2055 | while (n) { |
| 2056 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 2057 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2058 | } |
| 2059 | return list; |
| 2060 | } |
| 2061 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2062 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2063 | /* Expansion can recurse */ |
| 2064 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2065 | static int process_command_subs(o_string *dest, const char *s); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2066 | #endif |
| 2067 | static char *expand_string_to_string(const char *str); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2068 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2069 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2070 | parse_stream_dquoted(dest, input, dquote_end) |
| 2071 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2072 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2073 | o_string *dest, |
| 2074 | struct in_str *input, |
| 2075 | int dquote_end); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2076 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2077 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 2078 | * all variable references within and returns a pointer to |
| 2079 | * a list of expanded strings, possibly with larger number |
| 2080 | * of strings. (Think VAR="a b"; echo $VAR). |
| 2081 | * This new list is allocated as a single malloc block. |
| 2082 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 2083 | * followed by strings themself. |
| 2084 | * Caller can deallocate entire list by single free(list). */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2085 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2086 | /* Store given string, finalizing the word and starting new one whenever |
| 2087 | * we encounter IFS char(s). This is used for expanding variable values. |
| 2088 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 2089 | 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] | 2090 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2091 | while (1) { |
| 2092 | int word_len = strcspn(str, G.ifs); |
| 2093 | if (word_len) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2094 | if (output->o_escape || !output->o_glob) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2095 | o_addQstr(output, str, word_len); |
| 2096 | else /* protect backslashes against globbing up :) */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2097 | o_addblock_duplicate_backslash(output, str, word_len); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2098 | str += word_len; |
| 2099 | } |
| 2100 | if (!*str) /* EOL - do not finalize word */ |
| 2101 | break; |
| 2102 | o_addchr(output, '\0'); |
| 2103 | debug_print_list("expand_on_ifs", output, n); |
| 2104 | n = o_save_ptr(output, n); |
| 2105 | str += strspn(str, G.ifs); /* skip ifs chars */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2106 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2107 | debug_print_list("expand_on_ifs[1]", output, n); |
| 2108 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2111 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 2112 | * they are in double quotes, with the exception that they are not :). |
| 2113 | * Just the rules are similar: "expand only $var and `cmd`" |
| 2114 | * |
| 2115 | * Returns malloced string. |
| 2116 | * As an optimization, we return NULL if expansion is not needed. |
| 2117 | */ |
| 2118 | static char *expand_pseudo_dquoted(const char *str) |
| 2119 | { |
| 2120 | char *exp_str; |
| 2121 | struct in_str input; |
| 2122 | o_string dest = NULL_O_STRING; |
| 2123 | |
| 2124 | if (strchr(str, '$') == NULL |
| 2125 | #if ENABLE_HUSH_TICK |
| 2126 | && strchr(str, '`') == NULL |
| 2127 | #endif |
| 2128 | ) { |
| 2129 | return NULL; |
| 2130 | } |
| 2131 | |
| 2132 | /* We need to expand. Example: |
| 2133 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 2134 | */ |
| 2135 | setup_string_in_str(&input, str); |
| 2136 | parse_stream_dquoted(NULL, &dest, &input, EOF); |
| 2137 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
| 2138 | exp_str = expand_string_to_string(dest.data); |
| 2139 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 2140 | o_free_unsafe(&dest); |
| 2141 | return exp_str; |
| 2142 | } |
| 2143 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2144 | /* Expand all variable references in given string, adding words to list[] |
| 2145 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 2146 | * to be filled). This routine is extremely tricky: has to deal with |
| 2147 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 2148 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
| 2149 | static 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] | 2150 | { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 2151 | /* or_mask is either 0 (normal case) or 0x80 - |
| 2152 | * expansion of right-hand side of assignment == 1-element expand. |
| 2153 | * It will also do no globbing, and thus we must not backslash-quote! |
| 2154 | */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2155 | char ored_ch; |
| 2156 | char *p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2157 | |
| 2158 | ored_ch = 0; |
| 2159 | |
| 2160 | debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg); |
| 2161 | debug_print_list("expand_vars_to_list", output, n); |
| 2162 | n = o_save_ptr(output, n); |
| 2163 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 2164 | |
| 2165 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2166 | char first_ch; |
| 2167 | int i; |
| 2168 | char *dyn_val = NULL; |
| 2169 | const char *val = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2170 | #if ENABLE_HUSH_TICK |
| 2171 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2172 | #endif |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2173 | #if ENABLE_SH_MATH_SUPPORT |
| 2174 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 2175 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2176 | o_addblock(output, arg, p - arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2177 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 2178 | arg = ++p; |
| 2179 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 2180 | |
| 2181 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
| 2182 | /* "$@" is special. Even if quoted, it can still |
| 2183 | * expand to nothing (not even an empty string) */ |
| 2184 | if ((first_ch & 0x7f) != '@') |
| 2185 | ored_ch |= first_ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2186 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2187 | switch (first_ch & 0x7f) { |
| 2188 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 2189 | case '$': /* pid */ |
| 2190 | val = utoa(G.root_pid); |
| 2191 | break; |
| 2192 | case '!': /* bg pid */ |
| 2193 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
| 2194 | break; |
| 2195 | case '?': /* exitcode */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 2196 | val = utoa(G.last_exitcode); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2197 | break; |
| 2198 | case '#': /* argc */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2199 | if (arg[1] != SPECIAL_VAR_SYMBOL) |
| 2200 | /* actually, it's a ${#var} */ |
| 2201 | goto case_default; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2202 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 2203 | break; |
| 2204 | case '*': |
| 2205 | case '@': |
| 2206 | i = 1; |
| 2207 | if (!G.global_argv[i]) |
| 2208 | break; |
| 2209 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
| 2210 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2211 | smallint sv = output->o_escape; |
| 2212 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2213 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2214 | while (G.global_argv[i]) { |
| 2215 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 2216 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 2217 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 2218 | /* this argv[] is not empty and not last: |
| 2219 | * put terminating NUL, start new word */ |
| 2220 | o_addchr(output, '\0'); |
| 2221 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 2222 | n = o_save_ptr(output, n); |
| 2223 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 2224 | } |
| 2225 | } |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2226 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2227 | } else |
| 2228 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
| 2229 | * and in this case should treat it like '$*' - see 'else...' below */ |
| 2230 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
| 2231 | while (1) { |
| 2232 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2233 | if (++i >= G.global_argc) |
| 2234 | break; |
| 2235 | o_addchr(output, '\0'); |
| 2236 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 2237 | n = o_save_ptr(output, n); |
| 2238 | } |
| 2239 | } else { /* quoted $*: add as one word */ |
| 2240 | while (1) { |
| 2241 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2242 | if (!G.global_argv[++i]) |
| 2243 | break; |
| 2244 | if (G.ifs[0]) |
| 2245 | o_addchr(output, G.ifs[0]); |
| 2246 | } |
| 2247 | } |
| 2248 | break; |
| 2249 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 2250 | /* "Empty variable", used to make "" etc to not disappear */ |
| 2251 | arg++; |
| 2252 | ored_ch = 0x80; |
| 2253 | break; |
| 2254 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2255 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2256 | *p = '\0'; |
| 2257 | arg++; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2258 | /* Can't just stuff it into output o_string, |
| 2259 | * expanded result may need to be globbed |
| 2260 | * and $IFS-splitted */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2261 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2262 | process_command_subs(&subst_result, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2263 | debug_printf_subst("SUBST RES '%s'\n", subst_result.data); |
| 2264 | val = subst_result.data; |
| 2265 | goto store_val; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2266 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2267 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2268 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2269 | arith_eval_hooks_t hooks; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2270 | arith_t res; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2271 | int errcode; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2272 | char *exp_str; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2273 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2274 | arg++; /* skip '+' */ |
| 2275 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2276 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2277 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2278 | exp_str = expand_pseudo_dquoted(arg); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 2279 | hooks.lookupvar = get_local_var_value; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2280 | hooks.setvar = arith_set_local_var; |
| 2281 | hooks.endofname = endofname; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2282 | res = arith(exp_str ? exp_str : arg, &errcode, &hooks); |
| 2283 | free(exp_str); |
| 2284 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2285 | if (errcode < 0) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2286 | const char *msg = "error in arithmetic"; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2287 | switch (errcode) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2288 | case -3: |
| 2289 | msg = "exponent less than 0"; |
| 2290 | break; |
| 2291 | case -2: |
| 2292 | msg = "divide by 0"; |
| 2293 | break; |
| 2294 | case -5: |
| 2295 | msg = "expression recursion loop detected"; |
| 2296 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2297 | } |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2298 | die_if_script(msg); |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2299 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2300 | debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2301 | sprintf(arith_buf, arith_t_fmt, res); |
| 2302 | val = arith_buf; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2303 | break; |
| 2304 | } |
| 2305 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2306 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2307 | case_default: { |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2308 | bool exp_len = false; |
| 2309 | bool exp_null = false; |
| 2310 | char *var = arg; |
| 2311 | char exp_save = exp_save; /* for compiler */ |
| 2312 | char exp_op = exp_op; /* for compiler */ |
| 2313 | char *exp_word = exp_word; /* for compiler */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2314 | size_t exp_off = 0; |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2315 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2316 | *p = '\0'; |
| 2317 | arg[0] = first_ch & 0x7f; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2318 | |
| 2319 | /* prepare for expansions */ |
| 2320 | if (var[0] == '#') { |
| 2321 | /* handle length expansion ${#var} */ |
| 2322 | exp_len = true; |
| 2323 | ++var; |
| 2324 | } else { |
| 2325 | /* maybe handle parameter expansion */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2326 | exp_off = strcspn(var, ":-=+?%#"); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2327 | if (!var[exp_off]) |
| 2328 | exp_off = 0; |
| 2329 | if (exp_off) { |
| 2330 | exp_save = var[exp_off]; |
| 2331 | exp_null = exp_save == ':'; |
| 2332 | exp_word = var + exp_off; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2333 | if (exp_null) |
| 2334 | ++exp_word; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2335 | exp_op = *exp_word++; |
| 2336 | var[exp_off] = '\0'; |
| 2337 | } |
| 2338 | } |
| 2339 | |
| 2340 | /* lookup the variable in question */ |
| 2341 | if (isdigit(var[0])) { |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 2342 | /* handle_dollar() should have vetted var for us */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2343 | i = xatoi_u(var); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2344 | if (i < G.global_argc) |
| 2345 | val = G.global_argv[i]; |
| 2346 | /* else val remains NULL: $N with too big N */ |
| 2347 | } else |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 2348 | val = get_local_var_value(var); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2349 | |
| 2350 | /* handle any expansions */ |
| 2351 | if (exp_len) { |
| 2352 | debug_printf_expand("expand: length of '%s' = ", val); |
| 2353 | val = utoa(val ? strlen(val) : 0); |
| 2354 | debug_printf_expand("%s\n", val); |
| 2355 | } else if (exp_off) { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2356 | if (exp_op == '%' || exp_op == '#') { |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2357 | if (val) { |
| 2358 | /* we need to do a pattern match */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2359 | bool match_at_left; |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2360 | char *loc; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2361 | scan_t scan = pick_scan(exp_op, *exp_word, &match_at_left); |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2362 | if (exp_op == *exp_word) /* ## or %% */ |
| 2363 | ++exp_word; |
| 2364 | val = dyn_val = xstrdup(val); |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2365 | loc = scan(dyn_val, exp_word, match_at_left); |
| 2366 | if (match_at_left) /* # or ## */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2367 | val = loc; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2368 | else if (loc) /* % or %% and match was found */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2369 | *loc = '\0'; |
| 2370 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2371 | } else { |
| 2372 | /* we need to do an expansion */ |
| 2373 | int exp_test = (!val || (exp_null && !val[0])); |
| 2374 | if (exp_op == '+') |
| 2375 | exp_test = !exp_test; |
| 2376 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 2377 | exp_null ? "true" : "false", exp_test); |
| 2378 | if (exp_test) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2379 | if (exp_op == '?') { |
| 2380 | //TODO: how interactive bash aborts expansion mid-command? |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2381 | /* ${var?[error_msg_if_unset]} */ |
| 2382 | /* ${var:?[error_msg_if_unset_or_null]} */ |
| 2383 | /* mimic bash message */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2384 | die_if_script("%s: %s", |
| 2385 | var, |
| 2386 | exp_word[0] ? exp_word : "parameter null or not set" |
| 2387 | ); |
| 2388 | } else { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2389 | val = exp_word; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2390 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2391 | |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2392 | if (exp_op == '=') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2393 | /* ${var=[word]} or ${var:=[word]} */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2394 | if (isdigit(var[0]) || var[0] == '#') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2395 | /* mimic bash message */ |
| 2396 | die_if_script("$%s: cannot assign in this way", var); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2397 | val = NULL; |
| 2398 | } else { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2399 | char *new_var = xasprintf("%s=%s", var, val); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2400 | set_local_var(new_var, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2401 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2402 | } |
| 2403 | } |
| 2404 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2405 | |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2406 | var[exp_off] = exp_save; |
| 2407 | } |
| 2408 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2409 | arg[0] = first_ch; |
| 2410 | #if ENABLE_HUSH_TICK |
| 2411 | store_val: |
| 2412 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2413 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2414 | 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] | 2415 | if (val) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2416 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2417 | smallint sv = output->o_escape; |
| 2418 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2419 | n = expand_on_ifs(output, n, val); |
| 2420 | val = NULL; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2421 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2422 | } |
| 2423 | } else { /* quoted $VAR, val will be appended below */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2424 | 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] | 2425 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2426 | } /* default: */ |
| 2427 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2428 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2429 | if (val) { |
| 2430 | o_addQstr(output, val, strlen(val)); |
| 2431 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2432 | free(dyn_val); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2433 | /* Do the check to avoid writing to a const string */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2434 | if (*p != SPECIAL_VAR_SYMBOL) |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2435 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2436 | |
| 2437 | #if ENABLE_HUSH_TICK |
| 2438 | o_free(&subst_result); |
| 2439 | #endif |
| 2440 | arg = ++p; |
| 2441 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 2442 | |
| 2443 | if (arg[0]) { |
| 2444 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 2445 | /* this part is literal, and it was already pre-quoted |
| 2446 | * if needed (much earlier), do not use o_addQstr here! */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2447 | o_addstr_with_NUL(output, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2448 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 2449 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 2450 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 2451 | ) { |
| 2452 | n--; |
| 2453 | /* allow to reuse list[n] later without re-growth */ |
| 2454 | output->has_empty_slot = 1; |
| 2455 | } else { |
| 2456 | o_addchr(output, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2457 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2458 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2461 | static char **expand_variables(char **argv, int or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2462 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2463 | int n; |
| 2464 | char **list; |
| 2465 | char **v; |
| 2466 | o_string output = NULL_O_STRING; |
| 2467 | |
| 2468 | if (or_mask & 0x100) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2469 | output.o_escape = 1; /* protect against globbing for "$var" */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2470 | /* (unquoted $var will temporarily switch it off) */ |
| 2471 | output.o_glob = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2472 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2473 | |
| 2474 | n = 0; |
| 2475 | v = argv; |
| 2476 | while (*v) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2477 | n = expand_vars_to_list(&output, n, *v, (unsigned char)or_mask); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2478 | v++; |
| 2479 | } |
| 2480 | debug_print_list("expand_variables", &output, n); |
| 2481 | |
| 2482 | /* output.data (malloced in one block) gets returned in "list" */ |
| 2483 | list = o_finalize_list(&output, n); |
| 2484 | debug_print_strings("expand_variables[1]", list); |
| 2485 | return list; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2488 | static char **expand_strvec_to_strvec(char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2489 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2490 | return expand_variables(argv, 0x100); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 2493 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2494 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 2495 | { |
| 2496 | return expand_variables(argv, 0x80); |
| 2497 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 2498 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2499 | |
| 2500 | #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 2501 | static char **expand_strvec_to_strvec_singleword_noglob_cond(char **argv) |
| 2502 | { |
| 2503 | int n; |
| 2504 | char **list; |
| 2505 | char **v; |
| 2506 | o_string output = NULL_O_STRING; |
| 2507 | |
| 2508 | n = 0; |
| 2509 | v = argv; |
| 2510 | while (*v) { |
| 2511 | int is_var = is_well_formed_var_name(*v, '='); |
| 2512 | /* is_var * 0x80: singleword expansion for vars */ |
| 2513 | n = expand_vars_to_list(&output, n, *v, is_var * 0x80); |
| 2514 | |
| 2515 | /* Subtle! expand_vars_to_list did not glob last word yet. |
| 2516 | * It does this only when fed with further data. |
| 2517 | * Therefore we set globbing flags AFTER it, not before: |
| 2518 | */ |
| 2519 | |
| 2520 | /* if it is not recognizably abc=...; then: */ |
| 2521 | output.o_escape = !is_var; /* protect against globbing for "$var" */ |
| 2522 | /* (unquoted $var will temporarily switch it off) */ |
| 2523 | output.o_glob = !is_var; /* and indeed do globbing */ |
| 2524 | v++; |
| 2525 | } |
| 2526 | debug_print_list("expand_cond", &output, n); |
| 2527 | |
| 2528 | /* output.data (malloced in one block) gets returned in "list" */ |
| 2529 | list = o_finalize_list(&output, n); |
| 2530 | debug_print_strings("expand_cond[1]", list); |
| 2531 | return list; |
| 2532 | } |
| 2533 | #endif |
| 2534 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2535 | /* Used for expansion of right hand of assignments */ |
| 2536 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 2537 | * "v=/bin/c*" */ |
| 2538 | static char *expand_string_to_string(const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2539 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2540 | char *argv[2], **list; |
| 2541 | |
| 2542 | argv[0] = (char*)str; |
| 2543 | argv[1] = NULL; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2544 | list = expand_variables(argv, 0x80); /* 0x80: singleword expansion */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2545 | if (HUSH_DEBUG) |
| 2546 | if (!list[0] || list[1]) |
| 2547 | bb_error_msg_and_die("BUG in varexp2"); |
| 2548 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 2549 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 2550 | unbackslash((char*)list); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2551 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 2552 | return (char*)list; |
| 2553 | } |
| 2554 | |
| 2555 | /* Used for "eval" builtin */ |
| 2556 | static char* expand_strvec_to_string(char **argv) |
| 2557 | { |
| 2558 | char **list; |
| 2559 | |
| 2560 | list = expand_variables(argv, 0x80); |
| 2561 | /* Convert all NULs to spaces */ |
| 2562 | if (list[0]) { |
| 2563 | int n = 1; |
| 2564 | while (list[n]) { |
| 2565 | if (HUSH_DEBUG) |
| 2566 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 2567 | bb_error_msg_and_die("BUG in varexp3"); |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 2568 | /* bash uses ' ' regardless of $IFS contents */ |
| 2569 | list[n][-1] = ' '; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2570 | n++; |
| 2571 | } |
| 2572 | } |
| 2573 | overlapping_strcpy((char*)list, list[0]); |
| 2574 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 2575 | return (char*)list; |
| 2576 | } |
| 2577 | |
| 2578 | static char **expand_assignments(char **argv, int count) |
| 2579 | { |
| 2580 | int i; |
| 2581 | char **p = NULL; |
| 2582 | /* Expand assignments into one string each */ |
| 2583 | for (i = 0; i < count; i++) { |
| 2584 | p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
| 2585 | } |
| 2586 | return p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2589 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2590 | #if BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2591 | /* never called */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2592 | void re_execute_shell(char ***to_free, const char *s, |
| 2593 | char *g_argv0, char **g_argv, |
| 2594 | char **builtin_argv) NORETURN; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2595 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2596 | static void reset_traps_to_defaults(void) |
| 2597 | { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2598 | /* This function is always called in a child shell |
| 2599 | * after fork (not vfork, NOMMU doesn't use this function). |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2600 | * Child shells are not interactive. |
| 2601 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 2602 | * Testcase: (while :; do :; done) + ^Z should background. |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2603 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2604 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2605 | unsigned sig; |
| 2606 | unsigned mask; |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2607 | |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2608 | if (!G.traps && !(G.non_DFL_mask & SPECIAL_INTERACTIVE_SIGS)) |
| 2609 | return; |
| 2610 | |
| 2611 | /* Stupid. It can be done with *single* &= op, but we can't use |
| 2612 | * the fact that G.blocked_set is implemented as a bitmask... */ |
| 2613 | mask = (SPECIAL_INTERACTIVE_SIGS >> 1); |
| 2614 | sig = 1; |
| 2615 | while (1) { |
| 2616 | if (mask & 1) |
| 2617 | sigdelset(&G.blocked_set, sig); |
| 2618 | mask >>= 1; |
| 2619 | if (!mask) |
| 2620 | break; |
| 2621 | sig++; |
| 2622 | } |
| 2623 | |
| 2624 | G.non_DFL_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
| 2625 | mask = G.non_DFL_mask; |
| 2626 | if (G.traps) for (sig = 0; sig < NSIG; sig++, mask >>= 1) { |
| 2627 | if (!G.traps[sig]) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2628 | continue; |
| 2629 | free(G.traps[sig]); |
| 2630 | G.traps[sig] = NULL; |
| 2631 | /* There is no signal for 0 (EXIT) */ |
| 2632 | if (sig == 0) |
| 2633 | continue; |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2634 | /* There was a trap handler, we are removing it. |
| 2635 | * But if sig still has non-DFL handling, |
| 2636 | * we should not unblock it. */ |
| 2637 | if (mask & 1) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2638 | continue; |
| 2639 | sigdelset(&G.blocked_set, sig); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2640 | } |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2641 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | #else /* !BB_MMU */ |
| 2645 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2646 | static void re_execute_shell(char ***to_free, const char *s, |
| 2647 | char *g_argv0, char **g_argv, |
| 2648 | char **builtin_argv) NORETURN; |
| 2649 | static void re_execute_shell(char ***to_free, const char *s, |
| 2650 | char *g_argv0, char **g_argv, |
| 2651 | char **builtin_argv) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2652 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2653 | char param_buf[sizeof("-$%x:%x:%x:%x:%x") + sizeof(unsigned) * 2]; |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2654 | char *heredoc_argv[4]; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2655 | struct variable *cur; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2656 | #if ENABLE_HUSH_FUNCTIONS |
| 2657 | struct function *funcp; |
| 2658 | #endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2659 | char **argv, **pp; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2660 | unsigned cnt; |
| 2661 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2662 | if (!g_argv0) { /* heredoc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2663 | argv = heredoc_argv; |
| 2664 | argv[0] = (char *) G.argv0_for_re_execing; |
| 2665 | argv[1] = (char *) "-<"; |
| 2666 | argv[2] = (char *) s; |
| 2667 | argv[3] = NULL; |
Denis Vlasenko | f50caac | 2009-04-09 01:40:15 +0000 | [diff] [blame] | 2668 | pp = &argv[3]; /* used as pointer to empty environment */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2669 | goto do_exec; |
| 2670 | } |
| 2671 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2672 | cnt = 0; |
| 2673 | pp = builtin_argv; |
| 2674 | if (pp) while (*pp++) |
| 2675 | cnt++; |
| 2676 | |
| 2677 | sprintf(param_buf, "-$%x:%x:%x:%x" IF_HUSH_LOOPS(":%x") |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2678 | , (unsigned) G.root_pid |
| 2679 | , (unsigned) G.last_bg_pid |
| 2680 | , (unsigned) G.last_exitcode |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2681 | , cnt |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 2682 | IF_HUSH_LOOPS(, G.depth_of_loop) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2683 | ); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2684 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<depth> <vars...> <funcs...> |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2685 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2686 | */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2687 | cnt += 6; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2688 | for (cur = G.top_var; cur; cur = cur->next) { |
| 2689 | if (!cur->flg_export || cur->flg_read_only) |
| 2690 | cnt += 2; |
| 2691 | } |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2692 | #if ENABLE_HUSH_FUNCTIONS |
| 2693 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 2694 | cnt += 3; |
| 2695 | #endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2696 | pp = g_argv; |
| 2697 | while (*pp++) |
| 2698 | cnt++; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2699 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2700 | *pp++ = (char *) G.argv0_for_re_execing; |
| 2701 | *pp++ = param_buf; |
| 2702 | for (cur = G.top_var; cur; cur = cur->next) { |
| 2703 | if (cur->varstr == hush_version_str) |
| 2704 | continue; |
| 2705 | if (cur->flg_read_only) { |
| 2706 | *pp++ = (char *) "-R"; |
| 2707 | *pp++ = cur->varstr; |
| 2708 | } else if (!cur->flg_export) { |
| 2709 | *pp++ = (char *) "-V"; |
| 2710 | *pp++ = cur->varstr; |
| 2711 | } |
| 2712 | } |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2713 | #if ENABLE_HUSH_FUNCTIONS |
| 2714 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 2715 | *pp++ = (char *) "-F"; |
| 2716 | *pp++ = funcp->name; |
| 2717 | *pp++ = funcp->body_as_string; |
| 2718 | } |
| 2719 | #endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2720 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 2721 | * |
| 2722 | * However, POSIX says that subshells reset signals with traps |
| 2723 | * to SIG_DFL. |
| 2724 | * I tested bash-3.2 and it not only does that with true subshells |
| 2725 | * of the form ( list ), but with any forked children shells. |
| 2726 | * I set trap "echo W" WINCH; and then tried: |
| 2727 | * |
| 2728 | * { echo 1; sleep 20; echo 2; } & |
| 2729 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 2730 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 2731 | * |
| 2732 | * In all these cases sending SIGWINCH to the child shell |
| 2733 | * did not run the trap. If I add trap "echo V" WINCH; |
| 2734 | * _inside_ group (just before echo 1), it works. |
| 2735 | * |
| 2736 | * I conclude it means we don't need to pass active traps here. |
| 2737 | * exec syscall below resets them to SIG_DFL for us. |
| 2738 | */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2739 | *pp++ = (char *) "-c"; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2740 | *pp++ = (char *) s; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2741 | if (builtin_argv) { |
| 2742 | while (*++builtin_argv) |
| 2743 | *pp++ = *builtin_argv; |
| 2744 | *pp++ = (char *) ""; |
| 2745 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2746 | *pp++ = g_argv0; |
| 2747 | while (*g_argv) |
| 2748 | *pp++ = *g_argv++; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2749 | /* *pp = NULL; - is already there */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2750 | pp = environ; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2751 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2752 | do_exec: |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2753 | debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s); |
| 2754 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2755 | execve(bb_busybox_exec_path, argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2756 | /* Fallback. Useful for init=/bin/hush usage etc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2757 | if (argv[0][0] == '/') |
| 2758 | execve(argv[0], argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2759 | xfunc_error_retval = 127; |
| 2760 | bb_error_msg_and_die("can't re-execute the shell"); |
| 2761 | } |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2762 | #endif /* !BB_MMU */ |
| 2763 | |
| 2764 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2765 | static void setup_heredoc(struct redir_struct *redir) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2766 | { |
| 2767 | struct fd_pair pair; |
| 2768 | pid_t pid; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2769 | int len, written; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2770 | /* the _body_ of heredoc (misleading field name) */ |
| 2771 | const char *heredoc = redir->rd_filename; |
| 2772 | char *expanded; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2773 | #if !BB_MMU |
| 2774 | char **to_free; |
| 2775 | #endif |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2776 | |
| 2777 | expanded = NULL; |
| 2778 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
| 2779 | expanded = expand_pseudo_dquoted(heredoc); |
| 2780 | if (expanded) |
| 2781 | heredoc = expanded; |
| 2782 | } |
| 2783 | len = strlen(heredoc); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2784 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2785 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2786 | xpiped_pair(pair); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2787 | xmove_fd(pair.rd, redir->rd_fd); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2788 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2789 | /* Try writing without forking. Newer kernels have |
| 2790 | * dynamically growing pipes. Must use non-blocking write! */ |
| 2791 | ndelay_on(pair.wr); |
| 2792 | while (1) { |
| 2793 | written = write(pair.wr, heredoc, len); |
| 2794 | if (written <= 0) |
| 2795 | break; |
| 2796 | len -= written; |
| 2797 | if (len == 0) { |
| 2798 | close(pair.wr); |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 2799 | free(expanded); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2800 | return; |
| 2801 | } |
| 2802 | heredoc += written; |
| 2803 | } |
| 2804 | ndelay_off(pair.wr); |
| 2805 | |
| 2806 | /* Okay, pipe buffer was not big enough */ |
| 2807 | /* Note: we must not create a stray child (bastard? :) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2808 | * for the unsuspecting parent process. Child creates a grandchild |
| 2809 | * and exits before parent execs the process which consumes heredoc |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2810 | * (that exec happens after we return from this function) */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 2811 | #if !BB_MMU |
| 2812 | to_free = NULL; |
| 2813 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2814 | pid = vfork(); |
| 2815 | if (pid < 0) |
| 2816 | bb_perror_msg_and_die("vfork"); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2817 | if (pid == 0) { |
| 2818 | /* child */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 2819 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2820 | pid = BB_MMU ? fork() : vfork(); |
| 2821 | if (pid < 0) |
| 2822 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
| 2823 | if (pid != 0) |
| 2824 | _exit(0); |
| 2825 | /* grandchild */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2826 | close(redir->rd_fd); /* read side of the pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2827 | #if BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2828 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2829 | _exit(0); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2830 | #else |
| 2831 | /* Delegate blocking writes to another process */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2832 | xmove_fd(pair.wr, STDOUT_FILENO); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2833 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2834 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2835 | } |
| 2836 | /* parent */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 2837 | #if ENABLE_HUSH_FAST |
| 2838 | G.count_SIGCHLD++; |
| 2839 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 2840 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2841 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2842 | #if !BB_MMU |
| 2843 | free(to_free); |
| 2844 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2845 | close(pair.wr); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2846 | free(expanded); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2847 | wait(NULL); /* wait till child has died */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2850 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 2851 | * and stderr if they are redirected. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2852 | static int setup_redirects(struct command *prog, int squirrel[]) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2853 | { |
| 2854 | int openfd, mode; |
| 2855 | struct redir_struct *redir; |
| 2856 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2857 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2858 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2859 | /* rd_fd<<HERE case */ |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 2860 | if (squirrel && redir->rd_fd < 3 |
| 2861 | && squirrel[redir->rd_fd] < 0 |
| 2862 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2863 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2864 | } |
| 2865 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 2866 | * of the heredoc */ |
| 2867 | debug_printf_parse("set heredoc '%s'\n", |
| 2868 | redir->rd_filename); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2869 | setup_heredoc(redir); |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2870 | continue; |
| 2871 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2872 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2873 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
| 2874 | /* rd_fd<*>file case (<*> is <,>,>>,<>) */ |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2875 | char *p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2876 | if (redir->rd_filename == NULL) { |
| 2877 | /* Something went wrong in the parse. |
| 2878 | * Pretend it didn't happen */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2879 | bb_error_msg("bug in redirect parse"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2880 | continue; |
| 2881 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2882 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2883 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2884 | openfd = open_or_warn(p, mode); |
| 2885 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2886 | if (openfd < 0) { |
| 2887 | /* this could get lost if stderr has been redirected, but |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2888 | * bash and ash both lose it as well (though zsh doesn't!) */ |
| 2889 | //what the above comment tries to say? |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2890 | return 1; |
| 2891 | } |
| 2892 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2893 | /* rd_fd<*>rd_dup or rd_fd<*>- cases */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2894 | openfd = redir->rd_dup; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2897 | if (openfd != redir->rd_fd) { |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 2898 | if (squirrel && redir->rd_fd < 3 |
| 2899 | && squirrel[redir->rd_fd] < 0 |
| 2900 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2901 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2902 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2903 | if (openfd == REDIRFD_CLOSE) { |
| 2904 | /* "n>-" means "close me" */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2905 | close(redir->rd_fd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2906 | } else { |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2907 | xdup2(openfd, redir->rd_fd); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2908 | if (redir->rd_dup == REDIRFD_TO_FILE) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2909 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2910 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2911 | } |
| 2912 | } |
| 2913 | return 0; |
| 2914 | } |
| 2915 | |
| 2916 | static void restore_redirects(int squirrel[]) |
| 2917 | { |
| 2918 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2919 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2920 | fd = squirrel[i]; |
| 2921 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2922 | /* We simply die on error */ |
| 2923 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2924 | } |
| 2925 | } |
| 2926 | } |
| 2927 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2928 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2929 | static void free_pipe_list(struct pipe *head); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2930 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2931 | /* Return code is the exit status of the pipe */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2932 | static void free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2933 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2934 | char **p; |
| 2935 | struct command *command; |
| 2936 | struct redir_struct *r, *rnext; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2937 | int a, i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2938 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2939 | if (pi->stopped_cmds > 0) /* why? */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2940 | return; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2941 | debug_printf_clean("run pipe: (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2942 | for (i = 0; i < pi->num_cmds; i++) { |
| 2943 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2944 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2945 | if (command->argv) { |
| 2946 | for (a = 0, p = command->argv; *p; a++, p++) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2947 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2948 | } |
| 2949 | free_strings(command->argv); |
| 2950 | command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2951 | } |
| 2952 | /* not "else if": on syntax error, we may have both! */ |
| 2953 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2954 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 2955 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2956 | free_pipe_list(command->group); |
| 2957 | debug_printf_clean(" end group\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2958 | command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2959 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 2960 | /* else is crucial here. |
| 2961 | * If group != NULL, child_func is meaningless */ |
| 2962 | #if ENABLE_HUSH_FUNCTIONS |
| 2963 | else if (command->child_func) { |
| 2964 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 2965 | command->child_func->parent_cmd = NULL; |
| 2966 | } |
| 2967 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2968 | #if !BB_MMU |
| 2969 | free(command->group_as_string); |
| 2970 | command->group_as_string = NULL; |
| 2971 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2972 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2973 | debug_printf_clean(" redirect %d%s", |
| 2974 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2975 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 2976 | if (r->rd_filename) { |
| 2977 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 2978 | free(r->rd_filename); |
| 2979 | r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2980 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2981 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2982 | rnext = r->next; |
| 2983 | free(r); |
| 2984 | } |
| 2985 | command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2986 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2987 | free(pi->cmds); /* children are an array, they get freed all at once */ |
| 2988 | pi->cmds = NULL; |
| 2989 | #if ENABLE_HUSH_JOB |
| 2990 | free(pi->cmdtext); |
| 2991 | pi->cmdtext = NULL; |
| 2992 | #endif |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2993 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2994 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2995 | static void free_pipe_list(struct pipe *head) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2996 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2997 | struct pipe *pi, *next; |
| 2998 | |
| 2999 | for (pi = head; pi; pi = next) { |
| 3000 | #if HAS_KEYWORDS |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3001 | debug_printf_clean(" pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3002 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3003 | free_pipe(pi); |
| 3004 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3005 | next = pi->next; |
| 3006 | /*pi->next = NULL;*/ |
| 3007 | free(pi); |
| 3008 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
| 3011 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3012 | static int run_list(struct pipe *pi); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3013 | #if BB_MMU |
| 3014 | #define parse_stream(pstring, input, end_trigger) \ |
| 3015 | parse_stream(input, end_trigger) |
| 3016 | #endif |
| 3017 | static struct pipe *parse_stream(char **pstring, |
| 3018 | struct in_str *input, |
| 3019 | int end_trigger); |
| 3020 | static void parse_and_run_string(const char *s); |
| 3021 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3022 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 3023 | static char *find_in_path(const char *arg) |
| 3024 | { |
| 3025 | char *ret = NULL; |
| 3026 | const char *PATH = get_local_var_value("PATH"); |
| 3027 | |
| 3028 | if (!PATH) |
| 3029 | return NULL; |
| 3030 | |
| 3031 | while (1) { |
| 3032 | const char *end = strchrnul(PATH, ':'); |
| 3033 | int sz = end - PATH; /* must be int! */ |
| 3034 | |
| 3035 | free(ret); |
| 3036 | if (sz != 0) { |
| 3037 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 3038 | } else { |
| 3039 | /* We have xxx::yyyy in $PATH, |
| 3040 | * it means "use current dir" */ |
| 3041 | ret = xstrdup(arg); |
| 3042 | } |
| 3043 | if (access(ret, F_OK) == 0) |
| 3044 | break; |
| 3045 | |
| 3046 | if (*end == '\0') { |
| 3047 | free(ret); |
| 3048 | return NULL; |
| 3049 | } |
| 3050 | PATH = end + 1; |
| 3051 | } |
| 3052 | |
| 3053 | return ret; |
| 3054 | } |
| 3055 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3056 | static const struct built_in_command* find_builtin_helper(const char *name, |
| 3057 | const struct built_in_command *x, |
| 3058 | const struct built_in_command *end) |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3059 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3060 | while (x != end) { |
| 3061 | if (strcmp(name, x->cmd) != 0) { |
| 3062 | x++; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3063 | continue; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3064 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3065 | debug_printf_exec("found builtin '%s'\n", name); |
| 3066 | return x; |
| 3067 | } |
| 3068 | return NULL; |
| 3069 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3070 | static const struct built_in_command* find_builtin1(const char *name) |
| 3071 | { |
| 3072 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 3073 | } |
| 3074 | static const struct built_in_command* find_builtin(const char *name) |
| 3075 | { |
| 3076 | const struct built_in_command *x = find_builtin1(name); |
| 3077 | if (x) |
| 3078 | return x; |
| 3079 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 3080 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3081 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3082 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3083 | static struct function **find_function_slot(const char *name) |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3084 | { |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3085 | struct function **funcpp = &G.top_func; |
| 3086 | while (*funcpp) { |
| 3087 | if (strcmp(name, (*funcpp)->name) == 0) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3088 | break; |
| 3089 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3090 | funcpp = &(*funcpp)->next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3091 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3092 | return funcpp; |
| 3093 | } |
| 3094 | |
| 3095 | static const struct function *find_function(const char *name) |
| 3096 | { |
| 3097 | const struct function *funcp = *find_function_slot(name); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3098 | if (funcp) |
| 3099 | debug_printf_exec("found function '%s'\n", name); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3100 | return funcp; |
| 3101 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3102 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3103 | /* Note: takes ownership on name ptr */ |
| 3104 | static struct function *new_function(char *name) |
| 3105 | { |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3106 | struct function **funcpp = find_function_slot(name); |
| 3107 | struct function *funcp = *funcpp; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3108 | |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3109 | if (funcp != NULL) { |
| 3110 | struct command *cmd = funcp->parent_cmd; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3111 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 3112 | if (!cmd) { |
| 3113 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 3114 | free(funcp->name); |
| 3115 | /* Note: if !funcp->body, do not free body_as_string! |
| 3116 | * This is a special case of "-F name body" function: |
| 3117 | * body_as_string was not malloced! */ |
| 3118 | if (funcp->body) { |
| 3119 | free_pipe_list(funcp->body); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3120 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3121 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3122 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3123 | } |
| 3124 | } else { |
| 3125 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 3126 | cmd->argv[0] = funcp->name; |
| 3127 | cmd->group = funcp->body; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3128 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3129 | cmd->group_as_string = funcp->body_as_string; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3130 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3131 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3132 | } else { |
| 3133 | debug_printf_exec("remembering new function '%s'\n", name); |
| 3134 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 3135 | /*funcp->next = NULL;*/ |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3136 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3137 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3138 | funcp->name = name; |
| 3139 | return funcp; |
| 3140 | } |
| 3141 | |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3142 | static void unset_func(const char *name) |
| 3143 | { |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3144 | struct function **funcpp = find_function_slot(name); |
| 3145 | struct function *funcp = *funcpp; |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3146 | |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3147 | if (funcp != NULL) { |
| 3148 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 3149 | *funcpp = funcp->next; |
| 3150 | /* funcp is unlinked now, deleting it. |
| 3151 | * Note: if !funcp->body, the function was created by |
| 3152 | * "-F name body", do not free ->body_as_string |
| 3153 | * and ->name as they were not malloced. */ |
| 3154 | if (funcp->body) { |
| 3155 | free_pipe_list(funcp->body); |
| 3156 | free(funcp->name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3157 | # if !BB_MMU |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3158 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3159 | # endif |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3160 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3161 | free(funcp); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3162 | } |
| 3163 | } |
| 3164 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3165 | # if BB_MMU |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3166 | #define exec_function(to_free, funcp, argv) \ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3167 | exec_function(funcp, argv) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3168 | # endif |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3169 | static void exec_function(char ***to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3170 | const struct function *funcp, |
| 3171 | char **argv) NORETURN; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3172 | static void exec_function(char ***to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3173 | const struct function *funcp, |
| 3174 | char **argv) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3175 | { |
| 3176 | # if BB_MMU |
| 3177 | int n = 1; |
| 3178 | |
| 3179 | argv[0] = G.global_argv[0]; |
| 3180 | G.global_argv = argv; |
| 3181 | while (*++argv) |
| 3182 | n++; |
| 3183 | G.global_argc = n; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3184 | /* On MMU, funcp->body is always non-NULL */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3185 | n = run_list(funcp->body); |
| 3186 | fflush(NULL); |
| 3187 | _exit(n); |
| 3188 | # else |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3189 | re_execute_shell(to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3190 | funcp->body_as_string, |
| 3191 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3192 | argv + 1, |
| 3193 | NULL); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3194 | # endif |
| 3195 | } |
| 3196 | |
| 3197 | static int run_function(const struct function *funcp, char **argv) |
| 3198 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3199 | int rc; |
| 3200 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 3201 | smallint sv_flg; |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3202 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3203 | save_and_replace_G_args(&sv, argv); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3204 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3205 | /* "we are in function, ok to use return" */ |
| 3206 | sv_flg = G.flag_return_in_progress; |
| 3207 | G.flag_return_in_progress = -1; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3208 | #if ENABLE_HUSH_LOCAL |
| 3209 | G.func_nest_level++; |
| 3210 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3211 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3212 | /* On MMU, funcp->body is always non-NULL */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3213 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3214 | if (!funcp->body) { |
| 3215 | /* Function defined by -F */ |
| 3216 | parse_and_run_string(funcp->body_as_string); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3217 | rc = G.last_exitcode; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3218 | } else |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3219 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3220 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3221 | rc = run_list(funcp->body); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3222 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3223 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3224 | #if ENABLE_HUSH_LOCAL |
| 3225 | { |
| 3226 | struct variable *var; |
| 3227 | struct variable **var_pp; |
| 3228 | |
| 3229 | var_pp = &G.top_var; |
| 3230 | while ((var = *var_pp) != NULL) { |
| 3231 | if (var->func_nest_level < G.func_nest_level) { |
| 3232 | var_pp = &var->next; |
| 3233 | continue; |
| 3234 | } |
| 3235 | /* Unexport */ |
| 3236 | if (var->flg_export) |
| 3237 | bb_unsetenv(var->varstr); |
| 3238 | /* Remove from global list */ |
| 3239 | *var_pp = var->next; |
| 3240 | /* Free */ |
| 3241 | if (!var->max_len) |
| 3242 | free(var->varstr); |
| 3243 | free(var); |
| 3244 | } |
| 3245 | G.func_nest_level--; |
| 3246 | } |
| 3247 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3248 | G.flag_return_in_progress = sv_flg; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3249 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3250 | restore_G_args(&sv, argv); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3251 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3252 | return rc; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3253 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3254 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3255 | |
| 3256 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3257 | # if BB_MMU |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3258 | #define exec_builtin(to_free, x, argv) \ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3259 | exec_builtin(x, argv) |
| 3260 | # else |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3261 | #define exec_builtin(to_free, x, argv) \ |
| 3262 | exec_builtin(to_free, argv) |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3263 | # endif |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3264 | static void exec_builtin(char ***to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3265 | const struct built_in_command *x, |
| 3266 | char **argv) NORETURN; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3267 | static void exec_builtin(char ***to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3268 | const struct built_in_command *x, |
| 3269 | char **argv) |
| 3270 | { |
| 3271 | # if BB_MMU |
| 3272 | int rcode = x->function(argv); |
| 3273 | fflush(NULL); |
| 3274 | _exit(rcode); |
| 3275 | # else |
| 3276 | /* On NOMMU, we must never block! |
| 3277 | * Example: { sleep 99 | read line; } & echo Ok |
| 3278 | */ |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3279 | re_execute_shell(to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3280 | argv[0], |
| 3281 | G.global_argv[0], |
| 3282 | G.global_argv + 1, |
| 3283 | argv); |
| 3284 | # endif |
| 3285 | } |
| 3286 | |
| 3287 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 3288 | #if BB_MMU |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3289 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 3290 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 3291 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 3292 | pseudo_exec(command, argv_expanded) |
| 3293 | #endif |
| 3294 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3295 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3296 | * Never returns. |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3297 | * Don't exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3298 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3299 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3300 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 3301 | char **argv, int assignment_cnt, |
| 3302 | char **argv_expanded) NORETURN; |
| 3303 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 3304 | char **argv, int assignment_cnt, |
| 3305 | char **argv_expanded) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3306 | { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3307 | char **new_env; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3308 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3309 | /* Case when we are here: ... | var=val | ... */ |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3310 | if (!argv[assignment_cnt]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3311 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3312 | |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 3313 | new_env = expand_assignments(argv, assignment_cnt); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3314 | #if BB_MMU |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3315 | set_vars_and_save_old(new_env); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3316 | free(new_env); /* optional */ |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3317 | /* we can also destroy set_vars_and_save_old's return value, |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3318 | * to save memory */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3319 | #else |
| 3320 | nommu_save->new_env = new_env; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3321 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3322 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3323 | if (argv_expanded) { |
| 3324 | argv = argv_expanded; |
| 3325 | } else { |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 3326 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3327 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3328 | nommu_save->argv = argv; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3329 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3330 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 3331 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3332 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 3333 | if (strchr(argv[0], '/') != NULL) |
| 3334 | goto skip; |
| 3335 | #endif |
| 3336 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3337 | /* Check if the command matches any of the builtins. |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3338 | * Depending on context, this might be redundant. But it's |
| 3339 | * easier to waste a few CPU cycles than it is to figure out |
| 3340 | * if this is one of those cases. |
| 3341 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3342 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3343 | /* On NOMMU, it is more expensive to re-execute shell |
| 3344 | * just in order to run echo or test builtin. |
| 3345 | * It's better to skip it here and run corresponding |
| 3346 | * non-builtin later. */ |
| 3347 | const struct built_in_command *x; |
| 3348 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3349 | if (x) { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3350 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3351 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3352 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3353 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3354 | /* Check if the command matches any functions */ |
| 3355 | { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3356 | const struct function *funcp = find_function(argv[0]); |
| 3357 | if (funcp) { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3358 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3359 | } |
| 3360 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3361 | #endif |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3362 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 3363 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3364 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3365 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3366 | int a = find_applet_by_name(argv[0]); |
| 3367 | if (a >= 0) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3368 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3369 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3370 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3371 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3372 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3373 | # endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3374 | /* Re-exec ourselves */ |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3375 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3376 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 3377 | execv(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3378 | /* If they called chroot or otherwise made the binary no longer |
| 3379 | * executable, fall through */ |
| 3380 | } |
| 3381 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 3382 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3383 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3384 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3385 | skip: |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3386 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3387 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3388 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3389 | execvp(argv[0], argv); |
Denis Vlasenko | f9d4fc3 | 2009-04-21 20:40:51 +0000 | [diff] [blame] | 3390 | bb_perror_msg("can't execute '%s'", argv[0]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 3391 | _exit(EXIT_FAILURE); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3392 | } |
| 3393 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3394 | /* Called after [v]fork() in run_pipe |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3395 | */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3396 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3397 | struct command *command, |
| 3398 | char **argv_expanded) NORETURN; |
| 3399 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3400 | struct command *command, |
| 3401 | char **argv_expanded) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3402 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3403 | if (command->argv) { |
| 3404 | pseudo_exec_argv(nommu_save, command->argv, |
| 3405 | command->assignment_cnt, argv_expanded); |
| 3406 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3407 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3408 | if (command->group) { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3409 | /* Cases when we are here: |
| 3410 | * ( list ) |
| 3411 | * { list } & |
| 3412 | * ... | ( list ) | ... |
| 3413 | * ... | { list } | ... |
| 3414 | */ |
| 3415 | #if BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 3416 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3417 | debug_printf_exec("pseudo_exec: run_list\n"); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3418 | reset_traps_to_defaults(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3419 | rcode = run_list(command->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 3420 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3421 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3422 | _exit(rcode); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3423 | #else |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3424 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 3425 | command->group_as_string, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3426 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3427 | G.global_argv + 1, |
| 3428 | NULL); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3429 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3430 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3431 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3432 | /* Case when we are here: ... | >file */ |
| 3433 | debug_printf_exec("pseudo_exec'ed null command\n"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3434 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3437 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3438 | static const char *get_cmdtext(struct pipe *pi) |
| 3439 | { |
| 3440 | char **argv; |
| 3441 | char *p; |
| 3442 | int len; |
| 3443 | |
| 3444 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 3445 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3446 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3447 | if (pi->cmdtext) |
| 3448 | return pi->cmdtext; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3449 | argv = pi->cmds[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3450 | if (!argv || !argv[0]) { |
| 3451 | pi->cmdtext = xzalloc(1); |
| 3452 | return pi->cmdtext; |
| 3453 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3454 | |
| 3455 | len = 0; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3456 | do { |
| 3457 | len += strlen(*argv) + 1; |
| 3458 | } while (*++argv); |
| 3459 | p = xmalloc(len); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3460 | pi->cmdtext = p; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3461 | argv = pi->cmds[0].argv; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3462 | do { |
| 3463 | len = strlen(*argv); |
| 3464 | memcpy(p, *argv, len); |
| 3465 | p += len; |
| 3466 | *p++ = ' '; |
| 3467 | } while (*++argv); |
| 3468 | p[-1] = '\0'; |
| 3469 | return pi->cmdtext; |
| 3470 | } |
| 3471 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3472 | static void insert_bg_job(struct pipe *pi) |
| 3473 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3474 | struct pipe *job, **jobp; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3475 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3476 | |
| 3477 | /* Linear search for the ID of the job to use */ |
| 3478 | pi->jobid = 1; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3479 | for (job = G.job_list; job; job = job->next) |
| 3480 | if (job->jobid >= pi->jobid) |
| 3481 | pi->jobid = job->jobid + 1; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3482 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3483 | /* Add job to the list of running jobs */ |
| 3484 | jobp = &G.job_list; |
| 3485 | while ((job = *jobp) != NULL) |
| 3486 | jobp = &job->next; |
| 3487 | job = *jobp = xmalloc(sizeof(*job)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3488 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3489 | *job = *pi; /* physical copy */ |
| 3490 | job->next = NULL; |
| 3491 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 3492 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3493 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3494 | job->cmds[i].pid = pi->cmds[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3495 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3496 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3497 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3498 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3499 | if (G_interactive_fd) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3500 | printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext); |
| 3501 | /* Last command's pid goes to $! */ |
| 3502 | G.last_bg_pid = job->cmds[job->num_cmds - 1].pid; |
| 3503 | G.last_jobid = job->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3504 | } |
| 3505 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3506 | static void remove_bg_job(struct pipe *pi) |
| 3507 | { |
| 3508 | struct pipe *prev_pipe; |
| 3509 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3510 | if (pi == G.job_list) { |
| 3511 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3512 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3513 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3514 | while (prev_pipe->next != pi) |
| 3515 | prev_pipe = prev_pipe->next; |
| 3516 | prev_pipe->next = pi->next; |
| 3517 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3518 | if (G.job_list) |
| 3519 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3520 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3521 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3522 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3523 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3524 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3525 | static void delete_finished_bg_job(struct pipe *pi) |
| 3526 | { |
| 3527 | remove_bg_job(pi); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3528 | pi->stopped_cmds = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3529 | free_pipe(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3530 | free(pi); |
| 3531 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3532 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3533 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3534 | /* Check to see if any processes have exited -- if they |
| 3535 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3536 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3537 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3538 | int attributes; |
| 3539 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3540 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3541 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3542 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3543 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3544 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3545 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3546 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 3547 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3548 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3549 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3550 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3551 | |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3552 | errno = 0; |
| 3553 | #if ENABLE_HUSH_FAST |
| 3554 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 3555 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 3556 | //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] | 3557 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 3558 | /* Avoid doing waitpid syscall if possible */ |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3559 | if (!G.we_have_children) { |
| 3560 | errno = ECHILD; |
| 3561 | return -1; |
| 3562 | } |
| 3563 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 3564 | /* We have children, but they did not exit |
| 3565 | * or stop yet (we saw no SIGCHLD) */ |
| 3566 | return 0; |
| 3567 | } |
| 3568 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 3569 | } |
| 3570 | #endif |
| 3571 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3572 | /* Do we do this right? |
| 3573 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3574 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3575 | * [3]+ Stopped sleep 20 | false |
| 3576 | * bash-3.00# echo $? |
| 3577 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3578 | * [hush 1.14.0: yes we do it right] |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3579 | */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3580 | wait_more: |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3581 | while (1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3582 | int i; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3583 | int dead; |
| 3584 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3585 | #if ENABLE_HUSH_FAST |
| 3586 | i = G.count_SIGCHLD; |
| 3587 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3588 | childpid = waitpid(-1, &status, attributes); |
| 3589 | if (childpid <= 0) { |
| 3590 | if (childpid && errno != ECHILD) |
| 3591 | bb_perror_msg("waitpid"); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3592 | #if ENABLE_HUSH_FAST |
| 3593 | else { /* Until next SIGCHLD, waitpid's are useless */ |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3594 | G.we_have_children = (childpid == 0); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3595 | G.handled_SIGCHLD = i; |
| 3596 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 3597 | } |
| 3598 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3599 | break; |
| 3600 | } |
| 3601 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 3602 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3603 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3604 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3605 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3606 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 3607 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3608 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3609 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 3610 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3611 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3612 | childpid, WEXITSTATUS(status)); |
| 3613 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3614 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3615 | if (fg_pipe) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3616 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 3617 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 3618 | if (fg_pipe->cmds[i].pid != childpid) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3619 | continue; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3620 | if (dead) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3621 | fg_pipe->cmds[i].pid = 0; |
| 3622 | fg_pipe->alive_cmds--; |
| 3623 | if (i == fg_pipe->num_cmds - 1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3624 | /* last process gives overall exitstatus */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3625 | /* Note: is WIFSIGNALED, WEXITSTATUS = sig + 128 */ |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3626 | rcode = WEXITSTATUS(status); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3627 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3628 | /* bash prints killing signal's name for *last* |
| 3629 | * process in pipe (prints just newline for SIGINT). |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3630 | * Mimic this. Example: "sleep 5" + ^\ |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3631 | */ |
| 3632 | if (WIFSIGNALED(status)) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3633 | int sig = WTERMSIG(status); |
| 3634 | printf("%s\n", sig == SIGINT ? "" : get_signame(sig)); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3635 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3636 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3637 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3638 | fg_pipe->cmds[i].is_stopped = 1; |
| 3639 | fg_pipe->stopped_cmds++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3640 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3641 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 3642 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 3643 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3644 | /* All processes in fg pipe have exited or stopped */ |
| 3645 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 3646 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 3647 | * and "killall -STOP cat" */ |
| 3648 | if (G_interactive_fd) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3649 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3650 | if (fg_pipe->alive_cmds) |
| 3651 | insert_bg_job(fg_pipe); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3652 | #endif |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3653 | return rcode; |
| 3654 | } |
Denys Vlasenko | 6f22624 | 2009-06-03 14:37:30 +0200 | [diff] [blame] | 3655 | if (!fg_pipe->alive_cmds) |
| 3656 | return rcode; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3657 | } |
| 3658 | /* There are still running processes in the fg pipe */ |
| 3659 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3660 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3661 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3664 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3665 | /* We asked to wait for bg or orphaned children */ |
| 3666 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3667 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3668 | for (i = 0; i < pi->num_cmds; i++) { |
| 3669 | if (pi->cmds[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3670 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3671 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3672 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3673 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 3674 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3675 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 3676 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3677 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3678 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3679 | /* child exited */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3680 | pi->cmds[i].pid = 0; |
| 3681 | pi->alive_cmds--; |
| 3682 | if (!pi->alive_cmds) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3683 | if (G_interactive_fd) |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 3684 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 3685 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3686 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3687 | } |
| 3688 | } else { |
| 3689 | /* child stopped */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3690 | pi->cmds[i].is_stopped = 1; |
| 3691 | pi->stopped_cmds++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3692 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3693 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3694 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3695 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3696 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 3697 | } |
| 3698 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3699 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3700 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 3701 | { |
| 3702 | pid_t p; |
| 3703 | int rcode = checkjobs(fg_pipe); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 3704 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3705 | /* Job finished, move the shell to the foreground */ |
| 3706 | p = getpgrp(); /* our process group id */ |
| 3707 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 3708 | tcsetpgrp(G_interactive_fd, p); |
| 3709 | } |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3710 | return rcode; |
| 3711 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3712 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3713 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3714 | /* Start all the jobs, but don't wait for anything to finish. |
| 3715 | * See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3716 | * |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3717 | * 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] | 3718 | * to finish to determine the exit status of the pipe. If the pipe |
| 3719 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3720 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3721 | * return value. |
| 3722 | * |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3723 | * Returns -1 only if started some children. IOW: we have to |
| 3724 | * mask out retvals of builtins etc with 0xff! |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3725 | * |
| 3726 | * The only case when we do not need to [v]fork is when the pipe |
| 3727 | * is single, non-backgrounded, non-subshell command. Examples: |
| 3728 | * cmd ; ... { list } ; ... |
| 3729 | * cmd && ... { list } && ... |
| 3730 | * cmd || ... { list } || ... |
| 3731 | * If it is, then we can run cmd as a builtin, NOFORK [do we do this?], |
| 3732 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3733 | * 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] | 3734 | * |
| 3735 | * Cases when we must fork: |
| 3736 | * non-single: cmd | cmd |
| 3737 | * backgrounded: cmd & { list } & |
| 3738 | * subshell: ( list ) [&] |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3739 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3740 | static int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3741 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3742 | static const char *const null_ptr = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3743 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3744 | int nextin; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3745 | struct command *command; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3746 | char **argv_expanded; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3747 | char **argv; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3748 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3749 | /* it is not always needed, but we aim to smaller code */ |
| 3750 | int squirrel[] = { -1, -1, -1 }; |
| 3751 | int rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3752 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3753 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3754 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 3755 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 3756 | IF_HUSH_JOB(pi->pgrp = -1;) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3757 | pi->stopped_cmds = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3758 | command = &(pi->cmds[0]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3759 | argv_expanded = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3760 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3761 | if (pi->num_cmds != 1 |
| 3762 | || pi->followup == PIPE_BG |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3763 | || command->cmd_type == CMD_SUBSHELL |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3764 | ) { |
| 3765 | goto must_fork; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3766 | } |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3767 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3768 | pi->alive_cmds = 1; |
| 3769 | |
| 3770 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 3771 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 3772 | |
| 3773 | if (command->group) { |
| 3774 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3775 | if (command->cmd_type == CMD_FUNCDEF) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3776 | /* "executing" func () { list } */ |
| 3777 | struct function *funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3778 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3779 | funcp = new_function(command->argv[0]); |
| 3780 | /* funcp->name is already set to argv[0] */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3781 | funcp->body = command->group; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3782 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3783 | funcp->body_as_string = command->group_as_string; |
| 3784 | command->group_as_string = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3785 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3786 | command->group = NULL; |
| 3787 | command->argv[0] = NULL; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3788 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 3789 | funcp->parent_cmd = command; |
| 3790 | command->child_func = funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3791 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3792 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 3793 | debug_leave(); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3794 | return EXIT_SUCCESS; |
| 3795 | } |
| 3796 | #endif |
| 3797 | /* { list } */ |
| 3798 | debug_printf("non-subshell group\n"); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3799 | rcode = 1; /* exitcode if redir failed */ |
| 3800 | if (setup_redirects(command, squirrel) == 0) { |
| 3801 | debug_printf_exec(": run_list\n"); |
| 3802 | rcode = run_list(command->group) & 0xff; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3803 | } |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 3804 | restore_redirects(squirrel); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3805 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3806 | debug_leave(); |
| 3807 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3808 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3811 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 3812 | { |
| 3813 | const struct built_in_command *x; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3814 | #if ENABLE_HUSH_FUNCTIONS |
| 3815 | const struct function *funcp; |
| 3816 | #else |
| 3817 | enum { funcp = 0 }; |
| 3818 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3819 | char **new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3820 | struct variable *old_vars = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3821 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3822 | if (argv[command->assignment_cnt] == NULL) { |
| 3823 | /* Assignments, but no command */ |
| 3824 | /* Ensure redirects take effect. Try "a=t >file" */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3825 | rcode = setup_redirects(command, squirrel); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3826 | restore_redirects(squirrel); |
| 3827 | /* Set shell variables */ |
| 3828 | while (*argv) { |
| 3829 | p = expand_string_to_string(*argv); |
| 3830 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 3831 | *argv, p); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3832 | set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3833 | argv++; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3834 | } |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3835 | /* Do we need to flag set_local_var() errors? |
| 3836 | * "assignment to readonly var" and "putenv error" |
| 3837 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3838 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3839 | debug_leave(); |
| 3840 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3841 | return rcode; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3842 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3843 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3844 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3845 | if (0) {} |
| 3846 | #if ENABLE_HUSH_BASH_COMPAT |
| 3847 | else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3848 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
| 3849 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3850 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3851 | #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 3852 | else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB_COND) { |
| 3853 | argv_expanded = expand_strvec_to_strvec_singleword_noglob_cond(argv + command->assignment_cnt); |
| 3854 | |
| 3855 | } |
| 3856 | #endif |
| 3857 | else { |
| 3858 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
| 3859 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3860 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3861 | x = find_builtin(argv_expanded[0]); |
| 3862 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3863 | funcp = NULL; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3864 | if (!x) |
| 3865 | funcp = find_function(argv_expanded[0]); |
| 3866 | #endif |
| 3867 | if (x || funcp) { |
| 3868 | if (!funcp) { |
| 3869 | if (x->function == builtin_exec && argv_expanded[1] == NULL) { |
| 3870 | debug_printf("exec with redirects only\n"); |
| 3871 | rcode = setup_redirects(command, NULL); |
| 3872 | goto clean_up_and_ret1; |
| 3873 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3874 | } |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3875 | /* setup_redirects acts on file descriptors, not FILEs. |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3876 | * This is perfect for work that comes after exec(). |
| 3877 | * Is it really safe for inline use? Experimentally, |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3878 | * things seem to work. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3879 | rcode = setup_redirects(command, squirrel); |
| 3880 | if (rcode == 0) { |
| 3881 | new_env = expand_assignments(argv, command->assignment_cnt); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3882 | old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3883 | if (!funcp) { |
| 3884 | debug_printf_exec(": builtin '%s' '%s'...\n", |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3885 | x->cmd, argv_expanded[1]); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3886 | rcode = x->function(argv_expanded) & 0xff; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3887 | fflush(NULL); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3888 | } |
| 3889 | #if ENABLE_HUSH_FUNCTIONS |
| 3890 | else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3891 | # if ENABLE_HUSH_LOCAL |
| 3892 | struct variable **sv; |
| 3893 | sv = G.shadowed_vars_pp; |
| 3894 | G.shadowed_vars_pp = &old_vars; |
| 3895 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3896 | debug_printf_exec(": function '%s' '%s'...\n", |
| 3897 | funcp->name, argv_expanded[1]); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3898 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3899 | # if ENABLE_HUSH_LOCAL |
| 3900 | G.shadowed_vars_pp = sv; |
| 3901 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3902 | } |
| 3903 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3904 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3905 | #if ENABLE_FEATURE_SH_STANDALONE |
| 3906 | clean_up_and_ret: |
| 3907 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3908 | restore_redirects(squirrel); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3909 | unset_vars(new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3910 | add_vars(old_vars); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3911 | clean_up_and_ret1: |
| 3912 | free(argv_expanded); |
| 3913 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3914 | debug_leave(); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3915 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 3916 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3917 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3918 | |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3919 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3920 | i = find_applet_by_name(argv_expanded[0]); |
| 3921 | if (i >= 0 && APPLET_IS_NOFORK(i)) { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3922 | rcode = setup_redirects(command, squirrel); |
| 3923 | if (rcode == 0) { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3924 | new_env = expand_assignments(argv, command->assignment_cnt); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3925 | old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3926 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3927 | argv_expanded[0], argv_expanded[1]); |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 3928 | rcode = run_nofork_applet(i, argv_expanded); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3929 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3930 | goto clean_up_and_ret; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3931 | } |
| 3932 | #endif |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3933 | /* It is neither builtin nor applet. We must fork. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3936 | must_fork: |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3937 | /* NB: argv_expanded may already be created, and that |
| 3938 | * might include `cmd` runs! Do not rerun it! We *must* |
| 3939 | * use argv_expanded if it's non-NULL */ |
| 3940 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3941 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3942 | pi->alive_cmds = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3943 | nextin = 0; |
| 3944 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3945 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3946 | struct fd_pair pipefds; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3947 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3948 | volatile nommu_save_t nommu_save; |
| 3949 | nommu_save.new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3950 | nommu_save.old_vars = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3951 | nommu_save.argv = NULL; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3952 | nommu_save.argv_from_re_execing = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3953 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3954 | command = &(pi->cmds[i]); |
| 3955 | if (command->argv) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3956 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 3957 | command->argv[0], command->argv[1]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3958 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3959 | debug_printf_exec(": pipe member with no argv\n"); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3960 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3961 | |
| 3962 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3963 | pipefds.rd = 0; |
| 3964 | pipefds.wr = 1; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3965 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3966 | xpiped_pair(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3967 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3968 | command->pid = BB_MMU ? fork() : vfork(); |
| 3969 | if (!command->pid) { /* child */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3970 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3971 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3972 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3973 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3974 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3975 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3976 | pid_t pgrp; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3977 | pgrp = pi->pgrp; |
| 3978 | if (pgrp < 0) /* true for 1st process only */ |
| 3979 | pgrp = getpid(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3980 | if (setpgid(0, pgrp) == 0 |
| 3981 | && pi->followup != PIPE_BG |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 3982 | && G_saved_tty_pgrp /* we have ctty */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3983 | ) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3984 | /* We do it in *every* child, not just first, |
| 3985 | * to avoid races */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3986 | tcsetpgrp(G_interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3987 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3988 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3989 | #endif |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3990 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 3991 | /* 1st cmd in backgrounded pipe |
| 3992 | * should have its stdin /dev/null'ed */ |
| 3993 | close(0); |
| 3994 | if (open(bb_dev_null, O_RDONLY)) |
| 3995 | xopen("/", O_RDONLY); |
| 3996 | } else { |
| 3997 | xmove_fd(nextin, 0); |
| 3998 | } |
| 3999 | xmove_fd(pipefds.wr, 1); |
| 4000 | if (pipefds.rd > 1) |
| 4001 | close(pipefds.rd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4002 | /* Like bash, explicit redirects override pipes, |
| 4003 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4004 | if (setup_redirects(command, NULL)) |
| 4005 | _exit(1); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4006 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4007 | /* Restore default handlers just prior to exec */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 4008 | /*signal(SIGCHLD, SIG_DFL); - so far we don't have any handlers */ |
| 4009 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4010 | /* Stores to nommu_save list of env vars putenv'ed |
| 4011 | * (NOMMU, on MMU we don't need that) */ |
| 4012 | /* cast away volatility... */ |
| 4013 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4014 | /* pseudo_exec() does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4015 | } |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 4016 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 4017 | /* parent or error */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 4018 | #if ENABLE_HUSH_FAST |
| 4019 | G.count_SIGCHLD++; |
| 4020 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 4021 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4022 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4023 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4024 | /* Clean up after vforked child */ |
| 4025 | free(nommu_save.argv); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4026 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 4027 | unset_vars(nommu_save.new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4028 | add_vars(nommu_save.old_vars); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4029 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4030 | free(argv_expanded); |
| 4031 | argv_expanded = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4032 | if (command->pid < 0) { /* [v]fork failed */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4033 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 4034 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4035 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4036 | pi->alive_cmds++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4037 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4038 | /* Second and next children need to know pid of first one */ |
| 4039 | if (pi->pgrp < 0) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4040 | pi->pgrp = command->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4041 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4042 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4043 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4044 | if (i) |
| 4045 | close(nextin); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4046 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4047 | close(pipefds.wr); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4048 | /* Pass read (output) pipe end to next iteration */ |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4049 | nextin = pipefds.rd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4050 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4051 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4052 | if (!pi->alive_cmds) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4053 | debug_leave(); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4054 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 4055 | return 1; |
| 4056 | } |
| 4057 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4058 | debug_leave(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4059 | 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] | 4060 | return -1; |
| 4061 | } |
| 4062 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 4063 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4064 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 4065 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4066 | static const char *const PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4067 | [PIPE_SEQ] = "SEQ", |
| 4068 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4069 | [PIPE_OR ] = "OR" , |
| 4070 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4071 | }; |
| 4072 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4073 | [RES_NONE ] = "NONE" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4074 | #if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4075 | [RES_IF ] = "IF" , |
| 4076 | [RES_THEN ] = "THEN" , |
| 4077 | [RES_ELIF ] = "ELIF" , |
| 4078 | [RES_ELSE ] = "ELSE" , |
| 4079 | [RES_FI ] = "FI" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4080 | #endif |
| 4081 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4082 | [RES_FOR ] = "FOR" , |
| 4083 | [RES_WHILE] = "WHILE", |
| 4084 | [RES_UNTIL] = "UNTIL", |
| 4085 | [RES_DO ] = "DO" , |
| 4086 | [RES_DONE ] = "DONE" , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 4087 | #endif |
| 4088 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4089 | [RES_IN ] = "IN" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4090 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4091 | #if ENABLE_HUSH_CASE |
| 4092 | [RES_CASE ] = "CASE" , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4093 | [RES_CASE_IN ] = "CASE_IN" , |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4094 | [RES_MATCH] = "MATCH", |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4095 | [RES_CASE_BODY] = "CASE_BODY", |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4096 | [RES_ESAC ] = "ESAC" , |
| 4097 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4098 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4099 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4100 | }; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4101 | static const char *const CMDTYPE[] = { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4102 | "{}", |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 4103 | "()", |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4104 | "[noglob]", |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4105 | #if ENABLE_HUSH_FUNCTIONS |
| 4106 | "func()", |
| 4107 | #endif |
| 4108 | }; |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4109 | |
| 4110 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4111 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4112 | pin = 0; |
| 4113 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4114 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 4115 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4116 | prn = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4117 | while (prn < pi->num_cmds) { |
| 4118 | struct command *command = &pi->cmds[prn]; |
| 4119 | char **argv = command->argv; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4120 | |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4121 | fprintf(stderr, "%*s cmd %d assignment_cnt:%d", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4122 | lvl*2, "", prn, |
| 4123 | command->assignment_cnt); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4124 | if (command->group) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4125 | fprintf(stderr, " group %s: (argv=%p)\n", |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4126 | CMDTYPE[command->cmd_type], |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4127 | argv); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4128 | debug_print_tree(command->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4129 | prn++; |
| 4130 | continue; |
| 4131 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4132 | if (argv) while (*argv) { |
| 4133 | fprintf(stderr, " '%s'", *argv); |
| 4134 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 4135 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4136 | fprintf(stderr, "\n"); |
| 4137 | prn++; |
| 4138 | } |
| 4139 | pi = pi->next; |
| 4140 | pin++; |
| 4141 | } |
| 4142 | } |
| 4143 | #endif |
| 4144 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 4145 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 4146 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4147 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4148 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4149 | #if ENABLE_HUSH_CASE |
| 4150 | char *case_word = NULL; |
| 4151 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4152 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4153 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4154 | char **for_lcur = NULL; |
| 4155 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4156 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4157 | smallint last_followup; |
| 4158 | smalluint rcode; |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 4159 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4160 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4161 | #else |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4162 | enum { cond_code = 0 }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4163 | #endif |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4164 | #if HAS_KEYWORDS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4165 | smallint rword; /* enum reserved_style */ |
| 4166 | smallint last_rword; /* ditto */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4167 | #endif |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4168 | |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4169 | 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] | 4170 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4171 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4172 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4173 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4174 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 4175 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4176 | continue; |
| 4177 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4178 | if (cpipe->next == NULL) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4179 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4180 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4181 | 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] | 4182 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4183 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4184 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4185 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4186 | continue; |
| 4187 | /* 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] | 4188 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 4189 | || 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] | 4190 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4191 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4192 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4193 | 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] | 4194 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4195 | } |
| 4196 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4197 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4198 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4199 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 4200 | * in order to return, no direct "return" statements please. |
| 4201 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4202 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4203 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4204 | G.run_list_level++; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4205 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4206 | |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4207 | #if HAS_KEYWORDS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4208 | rword = RES_NONE; |
| 4209 | last_rword = RES_XXXX; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4210 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4211 | last_followup = PIPE_SEQ; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4212 | rcode = G.last_exitcode; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4213 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4214 | /* Go through list of pipes, (maybe) executing them. */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4215 | 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] | 4216 | if (G.flag_SIGINT) |
| 4217 | break; |
| 4218 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4219 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4220 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 4221 | rword, cond_code, last_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4222 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 4223 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4224 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 4225 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4226 | /* start of a loop: remember where loop starts */ |
| 4227 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4228 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4229 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4230 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4231 | /* Still in the same "if...", "then..." or "do..." branch? */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4232 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4233 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 4234 | || (rcode != 0 && last_followup == PIPE_AND) |
| 4235 | ) { |
| 4236 | /* It is "<true> || CMD" or "<false> && CMD" |
| 4237 | * and we should not execute CMD */ |
| 4238 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 4239 | last_followup = pi->followup; |
| 4240 | continue; |
| 4241 | } |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4242 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4243 | last_followup = pi->followup; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4244 | IF_HAS_KEYWORDS(last_rword = rword;) |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4245 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4246 | if (cond_code) { |
| 4247 | if (rword == RES_THEN) { |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4248 | /* if false; then ... fi has exitcode 0! */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4249 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4250 | /* "if <false> THEN cmd": skip cmd */ |
| 4251 | continue; |
| 4252 | } |
| 4253 | } else { |
| 4254 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 4255 | /* "if <true> then ... ELSE/ELIF cmd": |
| 4256 | * skip cmd and all following ones */ |
| 4257 | break; |
| 4258 | } |
| 4259 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4260 | #endif |
| 4261 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4262 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4263 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 4264 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4265 | |
| 4266 | static const char encoded_dollar_at[] ALIGN1 = { |
| 4267 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 4268 | }; /* encoded representation of "$@" */ |
| 4269 | static const char *const encoded_dollar_at_argv[] = { |
| 4270 | encoded_dollar_at, NULL |
| 4271 | }; /* argv list with one element: "$@" */ |
| 4272 | char **vals; |
| 4273 | |
| 4274 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4275 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4276 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4277 | if (!pi->next->cmds[0].argv) { |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4278 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4279 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4280 | break; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4281 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4282 | vals = pi->next->cmds[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4283 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4284 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4285 | debug_print_strings("for_list made from", vals); |
| 4286 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4287 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4288 | debug_print_strings("for_list", for_list); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4289 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4290 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 4291 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4292 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4293 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4294 | for_lcur = NULL; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4295 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4296 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4297 | /* Insert next value from for_lcur */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4298 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4299 | 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] | 4300 | continue; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4301 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4302 | if (rword == RES_IN) { |
| 4303 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 4304 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4305 | if (rword == RES_DONE) { |
| 4306 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4307 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4308 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4309 | #if ENABLE_HUSH_CASE |
| 4310 | if (rword == RES_CASE) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4311 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4312 | continue; |
| 4313 | } |
| 4314 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4315 | char **argv; |
| 4316 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4317 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 4318 | break; |
| 4319 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4320 | argv = pi->cmds->argv; |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4321 | while (*argv) { |
| 4322 | char *pattern = expand_string_to_string(*argv); |
| 4323 | /* TODO: which FNM_xxx flags to use? */ |
| 4324 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 4325 | free(pattern); |
| 4326 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 4327 | free(case_word); /* make future "word)" stop */ |
| 4328 | case_word = NULL; |
| 4329 | break; |
| 4330 | } |
| 4331 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4332 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4333 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4334 | } |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4335 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4336 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4337 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4338 | } |
| 4339 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4340 | /* Just pressing <enter> in shell should check for jobs. |
| 4341 | * OTOH, in non-interactive shell this is useless |
| 4342 | * and only leads to extra job checks */ |
| 4343 | if (pi->num_cmds == 0) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4344 | if (G_interactive_fd) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4345 | goto check_jobs_and_continue; |
| 4346 | continue; |
| 4347 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4348 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4349 | /* After analyzing all keywords and conditions, we decided |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4350 | * to execute this pipe. NB: have to do checkjobs(NULL) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4351 | * after run_pipe to collect any background children, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4352 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4353 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4354 | { |
| 4355 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4356 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4357 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4358 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4359 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 4360 | if (r != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4361 | /* We ran a builtin, function, or group. |
| 4362 | * rcode is already known |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4363 | * and we don't need to wait for anything. */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4364 | G.last_exitcode = rcode; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4365 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4366 | check_and_run_traps(0); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4367 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4368 | /* Was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4369 | if (G.flag_break_continue) { |
| 4370 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4371 | /* We might fall into outer *loop*, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4372 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4373 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4374 | G.depth_break_continue--; |
| 4375 | if (G.depth_break_continue == 0) |
| 4376 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4377 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 4378 | } /* 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] | 4379 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4380 | goto check_jobs_and_break; |
| 4381 | /* "continue": simulate end of loop */ |
| 4382 | rword = RES_DONE; |
| 4383 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4384 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4385 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4386 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 673e945 | 2009-05-27 14:39:35 +0200 | [diff] [blame] | 4387 | if (G.flag_return_in_progress == 1) { |
| 4388 | /* same as "goto check_jobs_and_break" */ |
| 4389 | checkjobs(NULL); |
| 4390 | break; |
| 4391 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4392 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4393 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4394 | /* What does bash do with attempts to background builtins? */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4395 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4396 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 4397 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4398 | check_and_run_traps(0); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4399 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4400 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4401 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4402 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4403 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4404 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4405 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4406 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4407 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4408 | /* Waits for completion, then fg's main shell */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4409 | rcode = checkjobs_and_fg_shell(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4410 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4411 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4412 | } else |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4413 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4414 | { /* This one just waits for completion */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4415 | rcode = checkjobs(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4416 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4417 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4418 | } |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4419 | G.last_exitcode = rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4420 | } |
| 4421 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4422 | |
| 4423 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4424 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4425 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4426 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4427 | #endif |
| 4428 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4429 | /* Beware of "while false; true; do ..."! */ |
| 4430 | if (pi->next && pi->next->res_word == RES_DO) { |
| 4431 | if (rword == RES_WHILE) { |
| 4432 | if (rcode) { |
| 4433 | /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4434 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4435 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
| 4436 | goto check_jobs_and_break; |
| 4437 | } |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 4438 | } |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4439 | if (rword == RES_UNTIL) { |
| 4440 | if (!rcode) { |
| 4441 | debug_printf_exec(": until expr is true: breaking\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4442 | check_jobs_and_break: |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4443 | checkjobs(NULL); |
| 4444 | break; |
| 4445 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4446 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4447 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4448 | #endif |
Mike Frysinger | 8ec1c9d | 2009-03-29 00:45:26 +0000 | [diff] [blame] | 4449 | |
| 4450 | check_jobs_and_continue: |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 4451 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4452 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4453 | |
| 4454 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4455 | G.run_list_level--; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4456 | #endif |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4457 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4458 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4459 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4460 | free(for_list); |
| 4461 | #endif |
| 4462 | #if ENABLE_HUSH_CASE |
| 4463 | free(case_word); |
| 4464 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4465 | debug_leave(); |
| 4466 | 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] | 4467 | return rcode; |
| 4468 | } |
| 4469 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4470 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4471 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4472 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4473 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4474 | debug_printf_exec("run_and_free_list entered\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4475 | if (!G.fake_mode) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4476 | 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] | 4477 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4478 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4479 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4480 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4481 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4482 | free_pipe_list(pi); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 4483 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4484 | return rcode; |
| 4485 | } |
| 4486 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4487 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 4488 | static struct pipe *new_pipe(void) |
| 4489 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4490 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4491 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4492 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4493 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4494 | return pi; |
| 4495 | } |
| 4496 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4497 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 4498 | * if ctx->command is NULL. |
| 4499 | * No errors possible here. |
| 4500 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4501 | static int done_command(struct parse_context *ctx) |
| 4502 | { |
| 4503 | /* The command is really already in the pipe structure, so |
| 4504 | * advance the pipe counter and make a new, null command. */ |
| 4505 | struct pipe *pi = ctx->pipe; |
| 4506 | struct command *command = ctx->command; |
| 4507 | |
| 4508 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4509 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4510 | 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] | 4511 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4512 | } |
| 4513 | pi->num_cmds++; |
| 4514 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4515 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4516 | } else { |
| 4517 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 4518 | } |
| 4519 | |
| 4520 | /* Only real trickiness here is that the uncommitted |
| 4521 | * command structure is not counted in pi->num_cmds. */ |
| 4522 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4523 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 4524 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4525 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4526 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 4527 | } |
| 4528 | |
| 4529 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 4530 | { |
| 4531 | int not_null; |
| 4532 | |
| 4533 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 4534 | /* Close previous command */ |
| 4535 | not_null = done_command(ctx); |
| 4536 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4537 | #if HAS_KEYWORDS |
| 4538 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 4539 | ctx->ctx_inverted = 0; |
| 4540 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 4541 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4542 | |
| 4543 | /* Without this check, even just <enter> on command line generates |
| 4544 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4545 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4546 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4547 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4548 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4549 | #endif |
| 4550 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4551 | || ctx->ctx_res_w == RES_DONE |
| 4552 | || ctx->ctx_res_w == RES_FOR |
| 4553 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4554 | #endif |
| 4555 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4556 | || ctx->ctx_res_w == RES_ESAC |
| 4557 | #endif |
| 4558 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4559 | struct pipe *new_p; |
| 4560 | debug_printf_parse("done_pipe: adding new pipe: " |
| 4561 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 4562 | not_null, ctx->ctx_res_w); |
| 4563 | new_p = new_pipe(); |
| 4564 | ctx->pipe->next = new_p; |
| 4565 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4566 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4567 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4568 | * This is used to control execution. |
| 4569 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 4570 | * cases where variable or value happens to match a keyword): |
| 4571 | */ |
| 4572 | #if ENABLE_HUSH_LOOPS |
| 4573 | if (ctx->ctx_res_w == RES_FOR |
| 4574 | || ctx->ctx_res_w == RES_IN) |
| 4575 | ctx->ctx_res_w = RES_NONE; |
| 4576 | #endif |
| 4577 | #if ENABLE_HUSH_CASE |
| 4578 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4579 | ctx->ctx_res_w = RES_CASE_BODY; |
| 4580 | if (ctx->ctx_res_w == RES_CASE) |
| 4581 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4582 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4583 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4584 | /* Create the memory for command, roughly: |
| 4585 | * ctx->pipe->cmds = new struct command; |
| 4586 | * ctx->command = &ctx->pipe->cmds[0]; |
| 4587 | */ |
| 4588 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4589 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4590 | } |
| 4591 | debug_printf_parse("done_pipe return\n"); |
| 4592 | } |
| 4593 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4594 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4595 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4596 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4597 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4598 | /* Create the memory for command, roughly: |
| 4599 | * ctx->pipe->cmds = new struct command; |
| 4600 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4601 | */ |
| 4602 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4603 | } |
| 4604 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4605 | /* If a reserved word is found and processed, parse context is modified |
| 4606 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4607 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4608 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4609 | struct reserved_combo { |
| 4610 | char literal[6]; |
| 4611 | unsigned char res; |
| 4612 | unsigned char assignment_flag; |
| 4613 | int flag; |
| 4614 | }; |
| 4615 | enum { |
| 4616 | FLAG_END = (1 << RES_NONE ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4617 | #if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4618 | FLAG_IF = (1 << RES_IF ), |
| 4619 | FLAG_THEN = (1 << RES_THEN ), |
| 4620 | FLAG_ELIF = (1 << RES_ELIF ), |
| 4621 | FLAG_ELSE = (1 << RES_ELSE ), |
| 4622 | FLAG_FI = (1 << RES_FI ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4623 | #endif |
| 4624 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4625 | FLAG_FOR = (1 << RES_FOR ), |
| 4626 | FLAG_WHILE = (1 << RES_WHILE), |
| 4627 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 4628 | FLAG_DO = (1 << RES_DO ), |
| 4629 | FLAG_DONE = (1 << RES_DONE ), |
| 4630 | FLAG_IN = (1 << RES_IN ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4631 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4632 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4633 | FLAG_MATCH = (1 << RES_MATCH), |
| 4634 | FLAG_ESAC = (1 << RES_ESAC ), |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4635 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4636 | FLAG_START = (1 << RES_XXXX ), |
| 4637 | }; |
| 4638 | |
| 4639 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 4640 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4641 | /* Mostly a list of accepted follow-up reserved words. |
| 4642 | * FLAG_END means we are done with the sequence, and are ready |
| 4643 | * to turn the compound list into a command. |
| 4644 | * FLAG_START means the word must start a new compound list. |
| 4645 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4646 | static const struct reserved_combo reserved_list[] = { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4647 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4648 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 4649 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 4650 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 4651 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 4652 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 4653 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4654 | #endif |
| 4655 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4656 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 4657 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 4658 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 4659 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 4660 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 4661 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4662 | #endif |
| 4663 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4664 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 4665 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4666 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4667 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4668 | const struct reserved_combo *r; |
| 4669 | |
| 4670 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 4671 | if (strcmp(word->data, r->literal) == 0) |
| 4672 | return r; |
| 4673 | } |
| 4674 | return NULL; |
| 4675 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4676 | /* Return 0: not a keyword, 1: keyword |
| 4677 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4678 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 4679 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4680 | #if ENABLE_HUSH_CASE |
| 4681 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4682 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4683 | }; |
| 4684 | #endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4685 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4686 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4687 | if (word->o_quoted) |
| 4688 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4689 | r = match_reserved_word(word); |
| 4690 | if (!r) |
| 4691 | return 0; |
| 4692 | |
| 4693 | debug_printf("found reserved word %s, res %d\n", r->literal, r->res); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4694 | #if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4695 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 4696 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4697 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4698 | } else |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4699 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4700 | if (r->flag == 0) { /* '!' */ |
| 4701 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4702 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4703 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4704 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4705 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4706 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4707 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4708 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4709 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4710 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4711 | old = xmalloc(sizeof(*old)); |
| 4712 | debug_printf_parse("push stack %p\n", old); |
| 4713 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4714 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4715 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4716 | } 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] | 4717 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4718 | ctx->ctx_res_w = RES_SNTX; |
| 4719 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4720 | } else { |
| 4721 | /* "{...} fi" is ok. "{...} if" is not |
| 4722 | * Example: |
| 4723 | * if { echo foo; } then { echo bar; } fi */ |
| 4724 | if (ctx->command->group) |
| 4725 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4726 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4727 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4728 | ctx->ctx_res_w = r->res; |
| 4729 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4730 | word->o_assignment = r->assignment_flag; |
| 4731 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4732 | if (ctx->old_flag & FLAG_END) { |
| 4733 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4734 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4735 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4736 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4737 | old = ctx->stack; |
| 4738 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4739 | old->command->cmd_type = CMD_NORMAL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4740 | #if !BB_MMU |
| 4741 | o_addstr(&old->as_string, ctx->as_string.data); |
| 4742 | o_free_unsafe(&ctx->as_string); |
| 4743 | old->command->group_as_string = xstrdup(old->as_string.data); |
| 4744 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 4745 | old->command->group_as_string); |
| 4746 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4747 | *ctx = *old; /* physical copy */ |
| 4748 | free(old); |
| 4749 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4750 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4751 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4752 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4753 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4754 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4755 | * Normal return is 0. Syntax errors return 1. |
| 4756 | * Note: on return, word is reset, but not o_free'd! |
| 4757 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4758 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4759 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4760 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4761 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4762 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4763 | if (word->length == 0 && word->o_quoted == 0) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4764 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 4765 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4766 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4767 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4768 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4769 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 4770 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4771 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4772 | * "2.7 Redirection |
| 4773 | * ...the word that follows the redirection operator |
| 4774 | * shall be subjected to tilde expansion, parameter expansion, |
| 4775 | * command substitution, arithmetic expansion, and quote |
| 4776 | * removal. Pathname expansion shall not be performed |
| 4777 | * on the word by a non-interactive shell; an interactive |
| 4778 | * shell may perform it, but shall do so only when |
| 4779 | * the expansion would result in one word." |
| 4780 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4781 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4782 | /* Cater for >\file case: |
| 4783 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 4784 | * Same with heredocs: |
| 4785 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 4786 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4787 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 4788 | unbackslash(ctx->pending_redirect->rd_filename); |
| 4789 | /* Is it <<"HEREDOC"? */ |
| 4790 | if (word->o_quoted) { |
| 4791 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 4792 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4793 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4794 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4795 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4796 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4797 | /* If this word wasn't an assignment, next ones definitely |
| 4798 | * can't be assignments. Even if they look like ones. */ |
| 4799 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 4800 | && word->o_assignment != WORD_IS_KEYWORD |
| 4801 | ) { |
| 4802 | word->o_assignment = NOT_ASSIGNMENT; |
| 4803 | } else { |
| 4804 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
| 4805 | command->assignment_cnt++; |
| 4806 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 4807 | } |
| 4808 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4809 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4810 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 4811 | if (ctx->ctx_dsemicolon |
| 4812 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 4813 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 4814 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4815 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 4816 | ctx->ctx_dsemicolon = 0; |
| 4817 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4818 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4819 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4820 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4821 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 4822 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4823 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4824 | # if ENABLE_HUSH_CASE |
| 4825 | && ctx->ctx_res_w != RES_CASE |
| 4826 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4827 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4828 | debug_printf_parse("checking '%s' for reserved-ness\n", word->data); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4829 | if (reserved_word(word, ctx)) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4830 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4831 | debug_printf_parse("done_word return %d\n", |
| 4832 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4833 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4834 | } |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4835 | # ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 4836 | if (strcmp(word->data, "export") == 0 |
| 4837 | # if ENABLE_HUSH_LOCAL |
| 4838 | || strcmp(word->data, "local") == 0 |
| 4839 | # endif |
| 4840 | ) { |
| 4841 | command->cmd_type = CMD_SINGLEWORD_NOGLOB_COND; |
| 4842 | } else |
| 4843 | # endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4844 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4845 | if (strcmp(word->data, "[[") == 0) { |
| 4846 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 4847 | } |
| 4848 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4849 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4850 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4851 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4852 | if (command->group) { |
| 4853 | /* "{ echo foo; } echo bar" - bad */ |
| 4854 | syntax_error_at(word->data); |
| 4855 | debug_printf_parse("done_word return 1: syntax error, " |
| 4856 | "groups and arglists don't mix\n"); |
| 4857 | return 1; |
| 4858 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4859 | 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] | 4860 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 4861 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4862 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4863 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4864 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 4865 | char *p = word->data; |
| 4866 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 4867 | && (p[1] & 0x7f) == '@' |
| 4868 | && p[2] == SPECIAL_VAR_SYMBOL |
| 4869 | ) { |
| 4870 | p += 3; |
| 4871 | } |
| 4872 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4873 | /* saw no "$@", or not only "$@" but some |
| 4874 | * real text is there too */ |
| 4875 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 4876 | * e.g. "", $empty"" etc to not disappear */ |
| 4877 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 4878 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 4879 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 4880 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4881 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4882 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4883 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4884 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4885 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4886 | if (ctx->ctx_res_w == RES_FOR) { |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4887 | if (word->o_quoted |
| 4888 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 4889 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4890 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4891 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4892 | return 1; |
| 4893 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4894 | /* Force FOR to have just one word (variable name) */ |
| 4895 | /* NB: basically, this makes hush see "for v in ..." |
| 4896 | * syntax as if it is "for v; in ...". FOR and IN become |
| 4897 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4898 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4899 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4900 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4901 | #if ENABLE_HUSH_CASE |
| 4902 | /* Force CASE to have just one word */ |
| 4903 | if (ctx->ctx_res_w == RES_CASE) { |
| 4904 | done_pipe(ctx, PIPE_SEQ); |
| 4905 | } |
| 4906 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4907 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4908 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4909 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4910 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4911 | return 0; |
| 4912 | } |
| 4913 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4914 | |
| 4915 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 4916 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4917 | * Return: |
| 4918 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 4919 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 4920 | * REDIRFD_TO_FILE if no & was seen, |
| 4921 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4922 | */ |
| 4923 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4924 | #define parse_redir_right_fd(as_string, input) \ |
| 4925 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4926 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4927 | 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] | 4928 | { |
| 4929 | int ch, d, ok; |
| 4930 | |
| 4931 | ch = i_peek(input); |
| 4932 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4933 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4934 | |
| 4935 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4936 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4937 | ch = i_peek(input); |
| 4938 | if (ch == '-') { |
| 4939 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4940 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4941 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4942 | } |
| 4943 | d = 0; |
| 4944 | ok = 0; |
| 4945 | while (ch != EOF && isdigit(ch)) { |
| 4946 | d = d*10 + (ch-'0'); |
| 4947 | ok = 1; |
| 4948 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4949 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4950 | ch = i_peek(input); |
| 4951 | } |
| 4952 | if (ok) return d; |
| 4953 | |
| 4954 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 4955 | |
| 4956 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4957 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4960 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4961 | */ |
| 4962 | static int parse_redirect(struct parse_context *ctx, |
| 4963 | int fd, |
| 4964 | redir_type style, |
| 4965 | struct in_str *input) |
| 4966 | { |
| 4967 | struct command *command = ctx->command; |
| 4968 | struct redir_struct *redir; |
| 4969 | struct redir_struct **redirp; |
| 4970 | int dup_num; |
| 4971 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4972 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4973 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4974 | /* Check for a '>&1' type redirect */ |
| 4975 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 4976 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 4977 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4978 | } else { |
| 4979 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4980 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4981 | if (dup_num) { /* <<-... */ |
| 4982 | ch = i_getch(input); |
| 4983 | nommu_addchr(&ctx->as_string, ch); |
| 4984 | ch = i_peek(input); |
| 4985 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4986 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4987 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4988 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4989 | int ch = i_peek(input); |
| 4990 | if (ch == '|') { |
| 4991 | /* >|FILE redirect ("clobbering" >). |
| 4992 | * Since we do not support "set -o noclobber" yet, |
| 4993 | * >| and > are the same for now. Just eat |. |
| 4994 | */ |
| 4995 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4996 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4997 | } |
| 4998 | } |
| 4999 | |
| 5000 | /* Create a new redir_struct and append it to the linked list */ |
| 5001 | redirp = &command->redirects; |
| 5002 | while ((redir = *redirp) != NULL) { |
| 5003 | redirp = &(redir->next); |
| 5004 | } |
| 5005 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 5006 | /* redir->next = NULL; */ |
| 5007 | /* redir->rd_filename = NULL; */ |
| 5008 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5009 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5010 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5011 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 5012 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5013 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5014 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5015 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5016 | /* Erik had a check here that the file descriptor in question |
| 5017 | * is legit; I postpone that to "run time" |
| 5018 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5019 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 5020 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5021 | } else { |
| 5022 | /* Set ctx->pending_redirect, so we know what to do at the |
| 5023 | * end of the next parsed word. */ |
| 5024 | ctx->pending_redirect = redir; |
| 5025 | } |
| 5026 | return 0; |
| 5027 | } |
| 5028 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5029 | /* If a redirect is immediately preceded by a number, that number is |
| 5030 | * supposed to tell which file descriptor to redirect. This routine |
| 5031 | * looks for such preceding numbers. In an ideal world this routine |
| 5032 | * needs to handle all the following classes of redirects... |
| 5033 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 5034 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 5035 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 5036 | * 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] | 5037 | * |
| 5038 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 5039 | * "2.7 Redirection |
| 5040 | * ... If n is quoted, the number shall not be recognized as part of |
| 5041 | * the redirection expression. For example: |
| 5042 | * echo \2>a |
| 5043 | * writes the character 2 into file a" |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5044 | * We are getting it right by setting ->o_quoted on any \<char> |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5045 | * |
| 5046 | * A -1 return means no valid number was found, |
| 5047 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5048 | */ |
| 5049 | static int redirect_opt_num(o_string *o) |
| 5050 | { |
| 5051 | int num; |
| 5052 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5053 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5054 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5055 | num = bb_strtou(o->data, NULL, 10); |
| 5056 | if (errno || num < 0) |
| 5057 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5058 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5059 | return num; |
| 5060 | } |
| 5061 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5062 | #if BB_MMU |
| 5063 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 5064 | fetch_till_str(input, word, skip_tabs) |
| 5065 | #endif |
| 5066 | static char *fetch_till_str(o_string *as_string, |
| 5067 | struct in_str *input, |
| 5068 | const char *word, |
| 5069 | int skip_tabs) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5070 | { |
| 5071 | o_string heredoc = NULL_O_STRING; |
| 5072 | int past_EOL = 0; |
| 5073 | int ch; |
| 5074 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5075 | goto jump_in; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5076 | while (1) { |
| 5077 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5078 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5079 | if (ch == '\n') { |
| 5080 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 5081 | heredoc.data[past_EOL] = '\0'; |
| 5082 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 5083 | return heredoc.data; |
| 5084 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5085 | do { |
| 5086 | o_addchr(&heredoc, ch); |
| 5087 | past_EOL = heredoc.length; |
| 5088 | jump_in: |
| 5089 | do { |
| 5090 | ch = i_getch(input); |
| 5091 | nommu_addchr(as_string, ch); |
| 5092 | } while (skip_tabs && ch == '\t'); |
| 5093 | } while (ch == '\n'); |
| 5094 | } |
| 5095 | if (ch == EOF) { |
| 5096 | o_free_unsafe(&heredoc); |
| 5097 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5098 | } |
| 5099 | o_addchr(&heredoc, ch); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5100 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5101 | } |
| 5102 | } |
| 5103 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5104 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 5105 | * and load them all. There should be exactly heredoc_cnt of them. |
| 5106 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5107 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 5108 | { |
| 5109 | struct pipe *pi = ctx->list_head; |
| 5110 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5111 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5112 | int i; |
| 5113 | struct command *cmd = pi->cmds; |
| 5114 | |
| 5115 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 5116 | pi->num_cmds, |
| 5117 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 5118 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5119 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5120 | |
| 5121 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 5122 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5123 | while (redir) { |
| 5124 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5125 | char *p; |
| 5126 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5127 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5128 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5129 | p = fetch_till_str(&ctx->as_string, input, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5130 | redir->rd_filename, redir->rd_dup & HEREDOC_SKIPTABS); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5131 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5132 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5133 | return 1; |
| 5134 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5135 | free(redir->rd_filename); |
| 5136 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5137 | heredoc_cnt--; |
| 5138 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5139 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5140 | } |
| 5141 | cmd++; |
| 5142 | } |
| 5143 | pi = pi->next; |
| 5144 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5145 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5146 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5147 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5148 | bb_error_msg_and_die("heredoc BUG 2"); |
| 5149 | #endif |
| 5150 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5151 | } |
| 5152 | |
| 5153 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5154 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5155 | static FILE *generate_stream_from_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5156 | { |
| 5157 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5158 | int pid, channel[2]; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5159 | #if !BB_MMU |
| 5160 | char **to_free; |
| 5161 | #endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 5162 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 5163 | xpipe(channel); |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 5164 | pid = BB_MMU ? fork() : vfork(); |
| 5165 | if (pid < 0) |
| 5166 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5167 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5168 | if (pid == 0) { /* child */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5169 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 5170 | /* Process substitution is not considered to be usual |
| 5171 | * 'command execution'. |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 5172 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 5173 | */ |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 5174 | bb_signals(0 |
| 5175 | + (1 << SIGTSTP) |
| 5176 | + (1 << SIGTTIN) |
| 5177 | + (1 << SIGTTOU) |
| 5178 | , SIG_IGN); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5179 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 5180 | xmove_fd(channel[1], 1); |
| 5181 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 5182 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denys Vlasenko | 91836ba | 2009-09-23 01:46:19 +0200 | [diff] [blame] | 5183 | /* Awful hack for `trap` or $(trap). |
| 5184 | * |
| 5185 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 5186 | * contains an example where "trap" is executed in a subshell: |
| 5187 | * |
| 5188 | * save_traps=$(trap) |
| 5189 | * ... |
| 5190 | * eval "$save_traps" |
| 5191 | * |
| 5192 | * Standard does not say that "trap" in subshell shall print |
| 5193 | * parent shell's traps. It only says that its output |
| 5194 | * must have suitable form, but then, in the above example |
| 5195 | * (which is not supposed to be normative), it implies that. |
| 5196 | * |
| 5197 | * bash (and probably other shell) does implement it |
| 5198 | * (traps are reset to defaults, but "trap" still shows them), |
| 5199 | * but as a result, "trap" logic is hopelessly messed up: |
| 5200 | * |
| 5201 | * # trap |
| 5202 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 5203 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 5204 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 5205 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 5206 | * trap -- 'echo Ho' SIGWINCH |
| 5207 | * # echo `(trap)` <--- in subshell in subshell - output |
| 5208 | * trap -- 'echo Ho' SIGWINCH |
| 5209 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 5210 | * trap -- 'echo Ho' SIGWINCH |
| 5211 | * |
| 5212 | * The rules when to forget and when to not forget traps |
| 5213 | * get really complex and nonsensical. |
| 5214 | * |
| 5215 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 5216 | */ |
| 5217 | s = skip_whitespace(s); |
| 5218 | if (strncmp(s, "trap", 4) == 0 && (*skip_whitespace(s + 4) == '\0')) |
| 5219 | { |
| 5220 | static const char *const argv[] = { NULL, NULL }; |
| 5221 | builtin_trap((char**)argv); |
| 5222 | exit(0); /* not _exit() - we need to fflush */ |
| 5223 | } |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5224 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5225 | reset_traps_to_defaults(); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5226 | parse_and_run_string(s); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5227 | _exit(G.last_exitcode); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5228 | #else |
| 5229 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5230 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 5231 | * huge=`cat BIG` # was blocking here forever |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5232 | * echo OK |
| 5233 | */ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5234 | re_execute_shell(&to_free, |
| 5235 | s, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5236 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 5237 | G.global_argv + 1, |
| 5238 | NULL); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5239 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5240 | } |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5241 | |
| 5242 | /* parent */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 5243 | #if ENABLE_HUSH_FAST |
| 5244 | G.count_SIGCHLD++; |
| 5245 | //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); |
| 5246 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5247 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5248 | #if !BB_MMU |
| 5249 | free(to_free); |
| 5250 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5251 | close(channel[1]); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 5252 | pf = fdopen(channel[0], "r"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5253 | return pf; |
| 5254 | } |
| 5255 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 5256 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5257 | static int process_command_subs(o_string *dest, const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5258 | { |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5259 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5260 | struct in_str pipe_str; |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5261 | int ch, eol_cnt; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5262 | |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5263 | pf = generate_stream_from_string(s); |
| 5264 | if (pf == NULL) |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5265 | return 1; |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5266 | close_on_exec_on(fileno(pf)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5267 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5268 | /* Now send results of command back into original context */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5269 | setup_file_in_str(&pipe_str, pf); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5270 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5271 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5272 | if (ch == '\n') { |
| 5273 | eol_cnt++; |
| 5274 | continue; |
| 5275 | } |
| 5276 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5277 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5278 | eol_cnt--; |
| 5279 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 5280 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5281 | } |
| 5282 | |
| 5283 | debug_printf("done reading from pipe, pclose()ing\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5284 | /* Note: we got EOF, and we just close the read end of the pipe. |
| 5285 | * We do not wait for the `cmd` child to terminate. bash and ash do. |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5286 | * Try these: |
| 5287 | * echo `echo Hi; exec 1>&-; sleep 2` - bash waits 2 sec |
| 5288 | * `false`; echo $? - bash outputs "1" |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5289 | */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5290 | fclose(pf); |
| 5291 | debug_printf("closed FILE from child. return 0\n"); |
| 5292 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5293 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5294 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5295 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5296 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5297 | struct in_str *input, int ch) |
| 5298 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5299 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5300 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5301 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5302 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5303 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5304 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5305 | |
| 5306 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5307 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5308 | if (ch == '(' && !dest->o_quoted) { |
| 5309 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5310 | if (done_word(dest, ctx)) |
| 5311 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5312 | if (!command->argv) |
| 5313 | goto skip; /* (... */ |
| 5314 | if (command->argv[1]) { /* word word ... (... */ |
| 5315 | syntax_error_unexpected_ch('('); |
| 5316 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5317 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5318 | /* it is "word(..." or "word (..." */ |
| 5319 | do |
| 5320 | ch = i_getch(input); |
| 5321 | while (ch == ' ' || ch == '\t'); |
| 5322 | if (ch != ')') { |
| 5323 | syntax_error_unexpected_ch(ch); |
| 5324 | return 1; |
| 5325 | } |
| 5326 | nommu_addchr(&ctx->as_string, ch); |
| 5327 | do |
| 5328 | ch = i_getch(input); |
| 5329 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 5330 | if (ch != '{') { |
| 5331 | syntax_error_unexpected_ch(ch); |
| 5332 | return 1; |
| 5333 | } |
| 5334 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5335 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5336 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5337 | } |
| 5338 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5339 | if (command->argv /* word [word]{... */ |
| 5340 | || dest->length /* word{... */ |
| 5341 | || dest->o_quoted /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5342 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5343 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5344 | debug_printf_parse("parse_group return 1: " |
| 5345 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5346 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5347 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5348 | |
| 5349 | #if ENABLE_HUSH_FUNCTIONS |
| 5350 | skip: |
| 5351 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5352 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 5353 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5354 | endch = ')'; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5355 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5356 | } else { |
| 5357 | /* bash does not allow "{echo...", requires whitespace */ |
| 5358 | ch = i_getch(input); |
| 5359 | if (ch != ' ' && ch != '\t' && ch != '\n') { |
| 5360 | syntax_error_unexpected_ch(ch); |
| 5361 | return 1; |
| 5362 | } |
| 5363 | nommu_addchr(&ctx->as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5364 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5365 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5366 | { |
| 5367 | #if !BB_MMU |
| 5368 | char *as_string = NULL; |
| 5369 | #endif |
| 5370 | pipe_list = parse_stream(&as_string, input, endch); |
| 5371 | #if !BB_MMU |
| 5372 | if (as_string) |
| 5373 | o_addstr(&ctx->as_string, as_string); |
| 5374 | #endif |
| 5375 | /* empty ()/{} or parse error? */ |
| 5376 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5377 | /* parse_stream already emitted error msg */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5378 | #if !BB_MMU |
| 5379 | free(as_string); |
| 5380 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5381 | debug_printf_parse("parse_group return 1: " |
| 5382 | "parse_stream returned %p\n", pipe_list); |
| 5383 | return 1; |
| 5384 | } |
| 5385 | command->group = pipe_list; |
| 5386 | #if !BB_MMU |
| 5387 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 5388 | command->group_as_string = as_string; |
| 5389 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 5390 | command->group_as_string); |
| 5391 | #endif |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5392 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5393 | debug_printf_parse("parse_group return 0\n"); |
| 5394 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5395 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5396 | } |
| 5397 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5398 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5399 | /* Subroutines for copying $(...) and `...` things */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5400 | static void add_till_backquote(o_string *dest, struct in_str *input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5401 | /* '...' */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5402 | 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] | 5403 | { |
| 5404 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5405 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5406 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5407 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5408 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5409 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5410 | if (ch == '\'') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5411 | return; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5412 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5413 | } |
| 5414 | } |
| 5415 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5416 | 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] | 5417 | { |
| 5418 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5419 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5420 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5421 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5422 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5423 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5424 | if (ch == '"') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5425 | return; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5426 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5427 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5428 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5429 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5430 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5431 | if (ch == '`') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5432 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5433 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5434 | continue; |
| 5435 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 5436 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5437 | } |
| 5438 | } |
| 5439 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 5440 | * \` quoting. |
| 5441 | * "Within the backquoted style of command substitution, backslash |
| 5442 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 5443 | * The search for the matching backquote shall be satisfied by the first |
| 5444 | * backquote found without a preceding backslash; during this search, |
| 5445 | * if a non-escaped backquote is encountered within a shell comment, |
| 5446 | * a here-document, an embedded command substitution of the $(command) |
| 5447 | * form, or a quoted string, undefined results occur. A single-quoted |
| 5448 | * or double-quoted string that begins, but does not end, within the |
| 5449 | * "`...`" sequence produces undefined results." |
| 5450 | * Example Output |
| 5451 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 5452 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5453 | static void add_till_backquote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5454 | { |
| 5455 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5456 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5457 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5458 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5459 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5460 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5461 | if (ch == '`') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5462 | return; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5463 | if (ch == '\\') { |
| 5464 | /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5465 | int ch2 = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5466 | if (ch2 == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5467 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5468 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5469 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5470 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5471 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5472 | ch = ch2; |
| 5473 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5474 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5475 | } |
| 5476 | } |
| 5477 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 5478 | * quoting and nested ()s. |
| 5479 | * "With the $(command) style of command substitution, all characters |
| 5480 | * following the open parenthesis to the matching closing parenthesis |
| 5481 | * constitute the command. Any valid shell script can be used for command, |
| 5482 | * except a script consisting solely of redirections which produces |
| 5483 | * unspecified results." |
| 5484 | * Example Output |
| 5485 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 5486 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 5487 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 5488 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5489 | 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] | 5490 | { |
| 5491 | int count = 0; |
| 5492 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5493 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5494 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5495 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5496 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5497 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5498 | if (ch == '(') |
| 5499 | count++; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5500 | if (ch == ')') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5501 | if (--count < 0) { |
| 5502 | if (!dbl) |
| 5503 | break; |
| 5504 | if (i_peek(input) == ')') { |
| 5505 | i_getch(input); |
| 5506 | break; |
| 5507 | } |
| 5508 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5509 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5510 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5511 | if (ch == '\'') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5512 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5513 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5514 | continue; |
| 5515 | } |
| 5516 | if (ch == '"') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5517 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5518 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5519 | continue; |
| 5520 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5521 | if (ch == '\\') { |
| 5522 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5523 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5524 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5525 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5526 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5527 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5528 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5529 | continue; |
| 5530 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5531 | } |
| 5532 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5533 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5534 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5535 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5536 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5537 | #define handle_dollar(as_string, dest, input) \ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5538 | handle_dollar(dest, input) |
| 5539 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5540 | static int handle_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5541 | o_string *dest, |
| 5542 | struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5543 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5544 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5545 | unsigned char quote_mask = dest->o_escape ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5546 | |
| 5547 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5548 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5549 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5550 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 5551 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5552 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5553 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5554 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5555 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5556 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5557 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5558 | if (!isalnum(ch) && ch != '_') |
| 5559 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5560 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5561 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5562 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5563 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5564 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5565 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5566 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5567 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5568 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5569 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5570 | o_addchr(dest, ch | quote_mask); |
| 5571 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5572 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5573 | case '$': /* pid */ |
| 5574 | case '!': /* last bg pid */ |
| 5575 | case '?': /* last exit code */ |
| 5576 | case '#': /* number of args */ |
| 5577 | case '*': /* args */ |
| 5578 | case '@': /* args */ |
| 5579 | goto make_one_char_var; |
| 5580 | case '{': { |
| 5581 | bool first_char, all_digits; |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 5582 | int expansion; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 5583 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5584 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5585 | nommu_addchr(as_string, ch); |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 5586 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5587 | |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 5588 | /* TODO: maybe someone will try to escape the '}' */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5589 | expansion = 0; |
| 5590 | first_char = true; |
| 5591 | all_digits = false; |
| 5592 | while (1) { |
| 5593 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5594 | nommu_addchr(as_string, ch); |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5595 | if (ch == '}') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5596 | break; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5597 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5598 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5599 | if (first_char) { |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5600 | if (ch == '#') { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5601 | /* ${#var}: length of var contents */ |
| 5602 | goto char_ok; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5603 | } |
| 5604 | if (isdigit(ch)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5605 | all_digits = true; |
| 5606 | goto char_ok; |
| 5607 | } |
Mike Frysinger | dc3bc40 | 2009-06-01 14:09:09 -0400 | [diff] [blame] | 5608 | /* They're being verbose and doing ${?} */ |
| 5609 | if (i_peek(input) == '}' && strchr("$!?#*@_", ch)) |
| 5610 | goto char_ok; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5611 | } |
| 5612 | |
| 5613 | if (expansion < 2 |
| 5614 | && ( (all_digits && !isdigit(ch)) |
| 5615 | || (!all_digits && !isalnum(ch) && ch != '_') |
| 5616 | ) |
| 5617 | ) { |
| 5618 | /* handle parameter expansions |
| 5619 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 5620 | */ |
| 5621 | if (first_char) |
| 5622 | goto case_default; |
| 5623 | switch (ch) { |
| 5624 | case ':': /* null modifier */ |
| 5625 | if (expansion == 0) { |
| 5626 | debug_printf_parse(": null modifier\n"); |
| 5627 | ++expansion; |
| 5628 | break; |
| 5629 | } |
| 5630 | goto case_default; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5631 | case '#': /* remove prefix */ |
| 5632 | case '%': /* remove suffix */ |
| 5633 | if (expansion == 0) { |
| 5634 | debug_printf_parse(": remove suffix/prefix\n"); |
| 5635 | expansion = 2; |
| 5636 | break; |
| 5637 | } |
| 5638 | goto case_default; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5639 | case '-': /* default value */ |
| 5640 | case '=': /* assign default */ |
| 5641 | case '+': /* alternative */ |
| 5642 | case '?': /* error indicate */ |
| 5643 | debug_printf_parse(": parameter expansion\n"); |
| 5644 | expansion = 2; |
| 5645 | break; |
| 5646 | default: |
| 5647 | case_default: |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5648 | syntax_error_unterm_str("${name}"); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5649 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 5650 | return 1; |
| 5651 | } |
| 5652 | } |
| 5653 | char_ok: |
| 5654 | debug_printf_parse(": '%c'\n", ch); |
| 5655 | o_addchr(dest, ch | quote_mask); |
| 5656 | quote_mask = 0; |
| 5657 | first_char = false; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5658 | } /* while (1) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5659 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5660 | break; |
| 5661 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5662 | #if (ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK) |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5663 | case '(': { |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5664 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5665 | int pos; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5666 | # endif |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5667 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5668 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5669 | # if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5670 | if (i_peek(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5671 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5672 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5673 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5674 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5675 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5676 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5677 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5678 | add_till_closing_paren(dest, input, true); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5679 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5680 | if (as_string) { |
| 5681 | o_addstr(as_string, dest->data + pos); |
| 5682 | o_addchr(as_string, ')'); |
| 5683 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5684 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5685 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5686 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5687 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5688 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5689 | # endif |
| 5690 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5691 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5692 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5693 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5694 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5695 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5696 | add_till_closing_paren(dest, input, false); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5697 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5698 | if (as_string) { |
| 5699 | o_addstr(as_string, dest->data + pos); |
| 5700 | o_addchr(as_string, '`'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5701 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5702 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5703 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5704 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5705 | break; |
| 5706 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5707 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5708 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5709 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5710 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5711 | ch = i_peek(input); |
| 5712 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 5713 | ch = '_'; |
| 5714 | goto make_var; |
| 5715 | } |
| 5716 | /* else: it's $_ */ |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 5717 | /* TODO: $_ and $-: */ |
| 5718 | /* $_ Shell or shell script name; or last argument of last command |
| 5719 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 5720 | * 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] | 5721 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 5722 | default: |
| 5723 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5724 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5725 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5726 | return 0; |
| 5727 | } |
| 5728 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5729 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5730 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5731 | parse_stream_dquoted(dest, input, dquote_end) |
| 5732 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5733 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5734 | o_string *dest, |
| 5735 | struct in_str *input, |
| 5736 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5737 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5738 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5739 | int next; |
| 5740 | |
| 5741 | again: |
| 5742 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5743 | if (ch != EOF) |
| 5744 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5745 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5746 | if (dest->o_assignment == NOT_ASSIGNMENT) |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5747 | dest->o_escape ^= 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5748 | debug_printf_parse("parse_stream_dquoted return 0\n"); |
| 5749 | return 0; |
| 5750 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5751 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5752 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5753 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5754 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5755 | } |
| 5756 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5757 | if (ch != '\n') { |
| 5758 | next = i_peek(input); |
| 5759 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5760 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 5761 | ch, ch, dest->o_escape); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5762 | if (ch == '\\') { |
| 5763 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5764 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5765 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5766 | } |
| 5767 | /* bash: |
| 5768 | * "The backslash retains its special meaning [in "..."] |
| 5769 | * only when followed by one of the following characters: |
| 5770 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 5771 | * within double quotes by preceding it with a backslash." |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5772 | */ |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 5773 | if (strchr("$`\"\\\n", next) != NULL) { |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 5774 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 5775 | if (ch != '\n') { |
| 5776 | o_addqchr(dest, ch); |
| 5777 | nommu_addchr(as_string, ch); |
| 5778 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5779 | } else { |
| 5780 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 5781 | nommu_addchr(as_string, '\\'); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5782 | } |
| 5783 | goto again; |
| 5784 | } |
| 5785 | if (ch == '$') { |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5786 | if (handle_dollar(as_string, dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5787 | debug_printf_parse("parse_stream_dquoted return 1: " |
| 5788 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5789 | return 1; |
| 5790 | } |
| 5791 | goto again; |
| 5792 | } |
| 5793 | #if ENABLE_HUSH_TICK |
| 5794 | if (ch == '`') { |
| 5795 | //int pos = dest->length; |
| 5796 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5797 | o_addchr(dest, 0x80 | '`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5798 | add_till_backquote(dest, input); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5799 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5800 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5801 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5802 | } |
| 5803 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5804 | o_addQchr(dest, ch); |
| 5805 | if (ch == '=' |
| 5806 | && (dest->o_assignment == MAYBE_ASSIGNMENT |
| 5807 | || dest->o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5808 | && is_well_formed_var_name(dest->data, '=') |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5809 | ) { |
| 5810 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 5811 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5812 | goto again; |
| 5813 | } |
| 5814 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5815 | /* |
| 5816 | * Scan input until EOF or end_trigger char. |
| 5817 | * Return a list of pipes to execute, or NULL on EOF |
| 5818 | * or if end_trigger character is met. |
| 5819 | * On syntax error, exit is shell is not interactive, |
| 5820 | * reset parsing machinery and start parsing anew, |
| 5821 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5822 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5823 | static struct pipe *parse_stream(char **pstring, |
| 5824 | struct in_str *input, |
| 5825 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5826 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5827 | struct parse_context ctx; |
| 5828 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5829 | int is_in_dquote; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5830 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5831 | |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5832 | /* Double-quote state is handled in the state variable is_in_dquote. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5833 | * 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] | 5834 | * found. When recursing, quote state is passed in via dest->o_escape. |
| 5835 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5836 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 5837 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5838 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5839 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5840 | G.ifs = get_local_var_value("IFS"); |
| 5841 | if (G.ifs == NULL) |
| 5842 | G.ifs = " \t\n"; |
| 5843 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5844 | reset: |
| 5845 | #if ENABLE_HUSH_INTERACTIVE |
| 5846 | input->promptmode = 0; /* PS1 */ |
| 5847 | #endif |
| 5848 | /* dest.o_assignment = MAYBE_ASSIGNMENT; - already is */ |
| 5849 | initialize_context(&ctx); |
| 5850 | is_in_dquote = 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5851 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5852 | while (1) { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5853 | const char *is_ifs; |
| 5854 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5855 | int ch; |
| 5856 | int next; |
| 5857 | int redir_fd; |
| 5858 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5859 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5860 | if (is_in_dquote) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5861 | /* dest.o_quoted = 1; - already is (see below) */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5862 | if (parse_stream_dquoted(&ctx.as_string, &dest, input, '"')) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5863 | goto parse_error; |
| 5864 | } |
| 5865 | /* We reached closing '"' */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5866 | is_in_dquote = 0; |
| 5867 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5868 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5869 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 5870 | ch, ch, dest.o_escape); |
| 5871 | if (ch == EOF) { |
| 5872 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5873 | |
| 5874 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5875 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5876 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5877 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5878 | /* end_trigger == '}' case errors out earlier, |
| 5879 | * checking only ')' */ |
| 5880 | if (end_trigger == ')') { |
| 5881 | syntax_error_unterm_ch('('); /* exits */ |
| 5882 | /* goto parse_error; */ |
| 5883 | } |
| 5884 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5885 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5886 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5887 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5888 | o_free(&dest); |
| 5889 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5890 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5891 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5892 | /* (this makes bare "&" cmd a no-op. |
| 5893 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5894 | if (pi->num_cmds == 0 |
| 5895 | IF_HAS_KEYWORDS( && pi->res_word == RES_NONE) |
| 5896 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5897 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5898 | pi = NULL; |
| 5899 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5900 | #if !BB_MMU |
| 5901 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 5902 | if (pstring) |
| 5903 | *pstring = ctx.as_string.data; |
| 5904 | else |
| 5905 | o_free_unsafe(&ctx.as_string); |
| 5906 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5907 | debug_leave(); |
| 5908 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5909 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5910 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5911 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5912 | is_ifs = strchr(G.ifs, ch); |
| 5913 | is_special = strchr("<>;&|(){}#'" /* special outside of "str" */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 5914 | "\\$\"" IF_HUSH_TICK("`") /* always special */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5915 | , ch); |
| 5916 | |
| 5917 | if (!is_special && !is_ifs) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5918 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5919 | o_addQchr(&dest, ch); |
| 5920 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 5921 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5922 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5923 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5924 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5925 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5926 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5927 | continue; |
| 5928 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5929 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5930 | if (is_ifs) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5931 | if (done_word(&dest, &ctx)) { |
| 5932 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 5933 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5934 | if (ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5935 | #if ENABLE_HUSH_CASE |
| 5936 | /* "case ... in <newline> word) ..." - |
| 5937 | * newlines are ignored (but ';' wouldn't be) */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5938 | if (ctx.command->argv == NULL |
| 5939 | && ctx.ctx_res_w == RES_MATCH |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5940 | ) { |
| 5941 | continue; |
| 5942 | } |
| 5943 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5944 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5945 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5946 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 5947 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5948 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5949 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5950 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5951 | heredoc_cnt = 0; |
| 5952 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5953 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5954 | ch = ';'; |
Denis Vlasenko | 7c98612 | 2009-04-04 12:15:42 +0000 | [diff] [blame] | 5955 | /* note: if (is_ifs) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5956 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5957 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5958 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5959 | |
| 5960 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 5961 | * } is an ordinary char in this case, even inside { cmd; } |
| 5962 | * Pathological example: { ""}; } should exec "}" cmd |
| 5963 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5964 | if (ch == '}') { |
| 5965 | if (!IS_NULL_CMD(ctx.command) /* cmd } */ |
| 5966 | || dest.length != 0 /* word} */ |
| 5967 | || dest.o_quoted /* ""} */ |
| 5968 | ) { |
| 5969 | goto ordinary_char; |
| 5970 | } |
| 5971 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
| 5972 | goto skip_end_trigger; |
| 5973 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5974 | } |
| 5975 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5976 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5977 | && (ch != ';' || heredoc_cnt == 0) |
| 5978 | #if ENABLE_HUSH_CASE |
| 5979 | && (ch != ')' |
| 5980 | || ctx.ctx_res_w != RES_MATCH |
| 5981 | || (!dest.o_quoted && strcmp(dest.data, "esac") == 0) |
| 5982 | ) |
| 5983 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5984 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5985 | if (heredoc_cnt) { |
| 5986 | /* This is technically valid: |
| 5987 | * { cat <<HERE; }; echo Ok |
| 5988 | * heredoc |
| 5989 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5990 | * HERE |
| 5991 | * but we don't support this. |
| 5992 | * We require heredoc to be in enclosing {}/(), |
| 5993 | * if any. |
| 5994 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5995 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5996 | goto parse_error; |
| 5997 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5998 | if (done_word(&dest, &ctx)) { |
| 5999 | goto parse_error; |
| 6000 | } |
| 6001 | done_pipe(&ctx, PIPE_SEQ); |
| 6002 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6003 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 6004 | if (!HAS_KEYWORDS |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6005 | 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] | 6006 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6007 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6008 | #if !BB_MMU |
| 6009 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 6010 | if (pstring) |
| 6011 | *pstring = ctx.as_string.data; |
| 6012 | else |
| 6013 | o_free_unsafe(&ctx.as_string); |
| 6014 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6015 | debug_leave(); |
| 6016 | debug_printf_parse("parse_stream return %p: " |
| 6017 | "end_trigger char found\n", |
| 6018 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6019 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6020 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6021 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 6022 | skip_end_trigger: |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6023 | if (is_ifs) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6024 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6025 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6026 | next = '\0'; |
| 6027 | if (ch != '\n') { |
| 6028 | next = i_peek(input); |
| 6029 | } |
| 6030 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 6031 | /* Catch <, > before deciding whether this word is |
| 6032 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 6033 | switch (ch) { |
| 6034 | case '>': |
| 6035 | redir_fd = redirect_opt_num(&dest); |
| 6036 | if (done_word(&dest, &ctx)) { |
| 6037 | goto parse_error; |
| 6038 | } |
| 6039 | redir_style = REDIRECT_OVERWRITE; |
| 6040 | if (next == '>') { |
| 6041 | redir_style = REDIRECT_APPEND; |
| 6042 | ch = i_getch(input); |
| 6043 | nommu_addchr(&ctx.as_string, ch); |
| 6044 | } |
| 6045 | #if 0 |
| 6046 | else if (next == '(') { |
| 6047 | syntax_error(">(process) not supported"); |
| 6048 | goto parse_error; |
| 6049 | } |
| 6050 | #endif |
| 6051 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 6052 | goto parse_error; |
| 6053 | continue; /* back to top of while (1) */ |
| 6054 | case '<': |
| 6055 | redir_fd = redirect_opt_num(&dest); |
| 6056 | if (done_word(&dest, &ctx)) { |
| 6057 | goto parse_error; |
| 6058 | } |
| 6059 | redir_style = REDIRECT_INPUT; |
| 6060 | if (next == '<') { |
| 6061 | redir_style = REDIRECT_HEREDOC; |
| 6062 | heredoc_cnt++; |
| 6063 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 6064 | ch = i_getch(input); |
| 6065 | nommu_addchr(&ctx.as_string, ch); |
| 6066 | } else if (next == '>') { |
| 6067 | redir_style = REDIRECT_IO; |
| 6068 | ch = i_getch(input); |
| 6069 | nommu_addchr(&ctx.as_string, ch); |
| 6070 | } |
| 6071 | #if 0 |
| 6072 | else if (next == '(') { |
| 6073 | syntax_error("<(process) not supported"); |
| 6074 | goto parse_error; |
| 6075 | } |
| 6076 | #endif |
| 6077 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 6078 | goto parse_error; |
| 6079 | continue; /* back to top of while (1) */ |
| 6080 | } |
| 6081 | |
| 6082 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 6083 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 6084 | && !ctx.pending_redirect |
| 6085 | ) { |
| 6086 | /* ch is a special char and thus this word |
| 6087 | * cannot be an assignment */ |
| 6088 | dest.o_assignment = NOT_ASSIGNMENT; |
| 6089 | } |
| 6090 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6091 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 6092 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6093 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6094 | case '#': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6095 | if (dest.length == 0) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6096 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6097 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6098 | if (ch == EOF || ch == '\n') |
| 6099 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6100 | i_getch(input); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6101 | /* note: we do not add it to &ctx.as_string */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6102 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6103 | nommu_addchr(&ctx.as_string, '\n'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6104 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6105 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6106 | } |
| 6107 | break; |
| 6108 | case '\\': |
| 6109 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6110 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6111 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6112 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6113 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6114 | if (ch != '\n') { |
| 6115 | o_addchr(&dest, '\\'); |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6116 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6117 | o_addchr(&dest, ch); |
| 6118 | nommu_addchr(&ctx.as_string, ch); |
| 6119 | /* Example: echo Hello \2>file |
| 6120 | * we need to know that word 2 is quoted */ |
| 6121 | dest.o_quoted = 1; |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6122 | } else { |
| 6123 | #if !BB_MMU |
| 6124 | /* It's "\<newline>". Remove trailing '\' from ctx.as_string */ |
| 6125 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
| 6126 | #endif |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6127 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6128 | break; |
| 6129 | case '$': |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6130 | if (handle_dollar(&ctx.as_string, &dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6131 | debug_printf_parse("parse_stream parse error: " |
| 6132 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6133 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6134 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6135 | break; |
| 6136 | case '\'': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6137 | dest.o_quoted = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6138 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6139 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6140 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6141 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6142 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6143 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6144 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6145 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6146 | break; |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 6147 | o_addqchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6148 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6149 | break; |
| 6150 | case '"': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6151 | dest.o_quoted = 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6152 | is_in_dquote ^= 1; /* invert */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6153 | if (dest.o_assignment == NOT_ASSIGNMENT) |
| 6154 | dest.o_escape ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6155 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 6156 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6157 | case '`': { |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6158 | #if !BB_MMU |
| 6159 | int pos; |
| 6160 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6161 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6162 | o_addchr(&dest, '`'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6163 | #if !BB_MMU |
| 6164 | pos = dest.length; |
| 6165 | #endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6166 | add_till_backquote(&dest, input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6167 | #if !BB_MMU |
| 6168 | o_addstr(&ctx.as_string, dest.data + pos); |
| 6169 | o_addchr(&ctx.as_string, '`'); |
| 6170 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6171 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6172 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6173 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6174 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 6175 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6176 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6177 | #if ENABLE_HUSH_CASE |
| 6178 | case_semi: |
| 6179 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6180 | if (done_word(&dest, &ctx)) { |
| 6181 | goto parse_error; |
| 6182 | } |
| 6183 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6184 | #if ENABLE_HUSH_CASE |
| 6185 | /* Eat multiple semicolons, detect |
| 6186 | * whether it means something special */ |
| 6187 | while (1) { |
| 6188 | ch = i_peek(input); |
| 6189 | if (ch != ';') |
| 6190 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6191 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6192 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 6193 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6194 | ctx.ctx_dsemicolon = 1; |
| 6195 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6196 | break; |
| 6197 | } |
| 6198 | } |
| 6199 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6200 | new_cmd: |
| 6201 | /* We just finished a cmd. New one may start |
| 6202 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6203 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6204 | break; |
| 6205 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6206 | if (done_word(&dest, &ctx)) { |
| 6207 | goto parse_error; |
| 6208 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6209 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6210 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6211 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6212 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6213 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6214 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6215 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6216 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6217 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6218 | if (done_word(&dest, &ctx)) { |
| 6219 | goto parse_error; |
| 6220 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 6221 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6222 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6223 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 6224 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6225 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6226 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6227 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6228 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6229 | } else { |
| 6230 | /* we could pick up a file descriptor choice here |
| 6231 | * with redirect_opt_num(), but bash doesn't do it. |
| 6232 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6233 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6234 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6235 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6236 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6237 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6238 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6239 | if (ctx.ctx_res_w == RES_MATCH |
| 6240 | && ctx.command->argv == NULL /* not (word|(... */ |
| 6241 | && dest.length == 0 /* not word(... */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6242 | && dest.o_quoted == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6243 | ) { |
| 6244 | continue; |
| 6245 | } |
| 6246 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6247 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6248 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 6249 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6250 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6251 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6252 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6253 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6254 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6255 | goto case_semi; |
| 6256 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6257 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 6258 | /* proper use of this character is caught by end_trigger: |
| 6259 | * if we see {, we call parse_group(..., end_trigger='}') |
| 6260 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 6261 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6262 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6263 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 6264 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 6265 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6266 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6267 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6268 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6269 | parse_error: |
| 6270 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6271 | struct parse_context *pctx; |
| 6272 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6273 | |
| 6274 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 6275 | * Sample for finding leaks on syntax error recovery path. |
| 6276 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6277 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 6278 | * Samples to catch leaks at execution: |
| 6279 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 6280 | * 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] | 6281 | */ |
| 6282 | pctx = &ctx; |
| 6283 | do { |
| 6284 | /* Update pipe/command counts, |
| 6285 | * otherwise freeing may miss some */ |
| 6286 | done_pipe(pctx, PIPE_SEQ); |
| 6287 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 6288 | pctx->list_head, pctx); |
| 6289 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6290 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6291 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6292 | #if !BB_MMU |
| 6293 | o_free_unsafe(&pctx->as_string); |
| 6294 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6295 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6296 | if (pctx != &ctx) { |
| 6297 | free(pctx); |
| 6298 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6299 | IF_HAS_KEYWORDS(pctx = p2;) |
| 6300 | } while (HAS_KEYWORDS && pctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6301 | /* Free text, clear all dest fields */ |
| 6302 | o_free(&dest); |
| 6303 | /* If we are not in top-level parse, we return, |
| 6304 | * our caller will propagate error. |
| 6305 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6306 | if (end_trigger != ';') { |
| 6307 | #if !BB_MMU |
| 6308 | if (pstring) |
| 6309 | *pstring = NULL; |
| 6310 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6311 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6312 | return ERR_PTR; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6313 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6314 | /* Discard cached input, force prompt */ |
| 6315 | input->p = NULL; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6316 | IF_HUSH_INTERACTIVE(input->promptme = 1;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6317 | goto reset; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6318 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6319 | } |
| 6320 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6321 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6322 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 6323 | * end_trigger controls how often we stop parsing |
| 6324 | * NUL: parse all, execute, return |
| 6325 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 6326 | */ |
| 6327 | 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] | 6328 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6329 | while (1) { |
| 6330 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6331 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6332 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6333 | if (!pipe_list) /* EOF */ |
| 6334 | break; |
| 6335 | debug_print_tree(pipe_list, 0); |
| 6336 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 6337 | run_and_free_list(pipe_list); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6338 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6339 | } |
| 6340 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6341 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6342 | { |
| 6343 | struct in_str input; |
| 6344 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6345 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6346 | } |
| 6347 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6348 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6349 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6350 | struct in_str input; |
| 6351 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6352 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6353 | } |
| 6354 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6355 | /* Called a few times only (or even once if "sh -c") */ |
| 6356 | static void block_signals(int second_time) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 6357 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6358 | unsigned sig; |
| 6359 | unsigned mask; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 6360 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6361 | mask = (1 << SIGQUIT); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6362 | if (G_interactive_fd) { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 6363 | mask = (1 << SIGQUIT) | SPECIAL_INTERACTIVE_SIGS; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6364 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6365 | mask |= SPECIAL_JOB_SIGS; |
| 6366 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6367 | G.non_DFL_mask = mask; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 6368 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6369 | if (!second_time) |
| 6370 | sigprocmask(SIG_SETMASK, NULL, &G.blocked_set); |
| 6371 | sig = 0; |
| 6372 | while (mask) { |
| 6373 | if (mask & 1) |
| 6374 | sigaddset(&G.blocked_set, sig); |
| 6375 | mask >>= 1; |
| 6376 | sig++; |
| 6377 | } |
| 6378 | sigdelset(&G.blocked_set, SIGCHLD); |
| 6379 | |
| 6380 | sigprocmask(SIG_SETMASK, &G.blocked_set, |
| 6381 | second_time ? NULL : &G.inherited_set); |
| 6382 | /* POSIX allows shell to re-enable SIGCHLD |
| 6383 | * even if it was SIG_IGN on entry */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 6384 | #if ENABLE_HUSH_FAST |
| 6385 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6386 | if (!second_time) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 6387 | signal(SIGCHLD, SIGCHLD_handler); |
| 6388 | #else |
| 6389 | if (!second_time) |
| 6390 | signal(SIGCHLD, SIG_DFL); |
| 6391 | #endif |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6392 | } |
| 6393 | |
| 6394 | #if ENABLE_HUSH_JOB |
| 6395 | /* helper */ |
| 6396 | static void maybe_set_to_sigexit(int sig) |
| 6397 | { |
| 6398 | void (*handler)(int); |
| 6399 | /* non_DFL_mask'ed signals are, well, masked, |
| 6400 | * no need to set handler for them. |
| 6401 | */ |
| 6402 | if (!((G.non_DFL_mask >> sig) & 1)) { |
| 6403 | handler = signal(sig, sigexit); |
| 6404 | if (handler == SIG_IGN) /* oops... restore back to IGN! */ |
| 6405 | signal(sig, handler); |
| 6406 | } |
| 6407 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6408 | /* Set handlers to restore tty pgrp and exit */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6409 | static void set_fatal_handlers(void) |
| 6410 | { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 6411 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6412 | if (HUSH_DEBUG) { |
| 6413 | maybe_set_to_sigexit(SIGILL ); |
| 6414 | maybe_set_to_sigexit(SIGFPE ); |
| 6415 | maybe_set_to_sigexit(SIGBUS ); |
| 6416 | maybe_set_to_sigexit(SIGSEGV); |
| 6417 | maybe_set_to_sigexit(SIGTRAP); |
| 6418 | } /* else: hush is perfect. what SEGV? */ |
| 6419 | maybe_set_to_sigexit(SIGABRT); |
| 6420 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 6421 | maybe_set_to_sigexit(SIGPIPE); |
| 6422 | maybe_set_to_sigexit(SIGALRM); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 6423 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are masked. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6424 | * if we aren't interactive... but in this case |
| 6425 | * 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] | 6426 | /*maybe_set_to_sigexit(SIGHUP );*/ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6427 | /*maybe_set_to_sigexit(SIGTERM);*/ |
| 6428 | /*maybe_set_to_sigexit(SIGINT );*/ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 6429 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 6430 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 6431 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6432 | static int set_mode(const char cstate, const char mode) |
| 6433 | { |
| 6434 | int state = (cstate == '-' ? 1 : 0); |
| 6435 | switch (mode) { |
| 6436 | case 'n': G.fake_mode = state; break; |
| 6437 | case 'x': /*G.debug_mode = state;*/ break; |
| 6438 | default: return EXIT_FAILURE; |
| 6439 | } |
| 6440 | return EXIT_SUCCESS; |
| 6441 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6442 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 6443 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 6444 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6445 | { |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6446 | static const struct variable const_shell_ver = { |
| 6447 | .next = NULL, |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 6448 | .varstr = (char*)hush_version_str, |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6449 | .max_len = 1, /* 0 can provoke free(name) */ |
| 6450 | .flg_export = 1, |
| 6451 | .flg_read_only = 1, |
| 6452 | }; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6453 | int signal_mask_is_inited = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6454 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6455 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6456 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6457 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 6458 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 6459 | INIT_G(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6460 | if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6461 | G.last_exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6462 | #if !BB_MMU |
| 6463 | G.argv0_for_re_execing = argv[0]; |
| 6464 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6465 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6466 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 6467 | G.top_var = &G.shell_ver; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6468 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6469 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 6470 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6471 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6472 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6473 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6474 | if (e) while (*e) { |
| 6475 | char *value = strchr(*e, '='); |
| 6476 | if (value) { /* paranoia */ |
| 6477 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 6478 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 6479 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6480 | cur_var->max_len = strlen(*e); |
| 6481 | cur_var->flg_export = 1; |
| 6482 | } |
| 6483 | e++; |
| 6484 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 6485 | /* reinstate HUSH_VERSION */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6486 | debug_printf_env("putenv '%s'\n", hush_version_str); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 6487 | putenv((char *)hush_version_str); |
| 6488 | |
| 6489 | /* Export PWD */ |
| 6490 | set_pwd_var(/*exp:*/ 1); |
| 6491 | /* bash also exports SHLVL and _, |
| 6492 | * and sets (but doesn't export) the following variables: |
| 6493 | * BASH=/bin/bash |
| 6494 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 6495 | * BASH_VERSION='3.2.0(1)-release' |
| 6496 | * HOSTTYPE=i386 |
| 6497 | * MACHTYPE=i386-pc-linux-gnu |
| 6498 | * OSTYPE=linux-gnu |
| 6499 | * HOSTNAME=<xxxxxxxxxx> |
| 6500 | * PPID=<NNNNN> |
| 6501 | * EUID=<NNNNN> |
| 6502 | * UID=<NNNNN> |
| 6503 | * GROUPS=() |
| 6504 | * LINES=<NNN> |
| 6505 | * COLUMNS=<NNN> |
| 6506 | * BASH_ARGC=() |
| 6507 | * BASH_ARGV=() |
| 6508 | * BASH_LINENO=() |
| 6509 | * BASH_SOURCE=() |
| 6510 | * DIRSTACK=() |
| 6511 | * PIPESTATUS=([0]="0") |
| 6512 | * HISTFILE=/<xxx>/.bash_history |
| 6513 | * HISTFILESIZE=500 |
| 6514 | * HISTSIZE=500 |
| 6515 | * MAILCHECK=60 |
| 6516 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 6517 | * SHELL=/bin/bash |
| 6518 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 6519 | * TERM=dumb |
| 6520 | * OPTERR=1 |
| 6521 | * OPTIND=1 |
| 6522 | * IFS=$' \t\n' |
| 6523 | * PS1='\s-\v\$ ' |
| 6524 | * PS2='> ' |
| 6525 | * PS4='+ ' |
| 6526 | */ |
| 6527 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 6528 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6529 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 6530 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6531 | G.global_argc = argc; |
| 6532 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 6533 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 6534 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 6535 | |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 6536 | if (setjmp(die_jmp)) { |
| 6537 | /* xfunc has failed! die die die */ |
| 6538 | /* no EXIT traps, this is an escape hatch! */ |
| 6539 | G.exiting = 1; |
| 6540 | hush_exit(xfunc_error_retval); |
| 6541 | } |
| 6542 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6543 | /* Shell is non-interactive at first. We need to call |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6544 | * block_signals(0) if we are going to execute "sh <script>", |
| 6545 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6546 | * If we later decide that we are interactive, we run block_signals(0) |
| 6547 | * (or re-run block_signals(1) if we ran block_signals(0) before) |
| 6548 | * in order to intercept (more) signals. |
| 6549 | */ |
| 6550 | |
| 6551 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 6552 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6553 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6554 | while (1) { |
Denys Vlasenko | a67a962 | 2009-08-20 03:38:58 +0200 | [diff] [blame] | 6555 | opt = getopt(argc, argv, "+c:xins" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6556 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 6557 | "<:$:R:V:" |
| 6558 | # if ENABLE_HUSH_FUNCTIONS |
| 6559 | "F:" |
| 6560 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6561 | #endif |
| 6562 | ); |
| 6563 | if (opt <= 0) |
| 6564 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6565 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6566 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6567 | /* Possibilities: |
| 6568 | * sh ... -c 'script' |
| 6569 | * sh ... -c 'script' ARG0 [ARG1...] |
| 6570 | * On NOMMU, if builtin_argc != 0, |
| 6571 | * sh ... -c 'builtin' [BARGV...] "" ARG0 [ARG1...] |
| 6572 | * "" needs to be replaced with NULL |
| 6573 | * and BARGV vector fed to builtin function. |
| 6574 | * Note: this form never happens: |
| 6575 | * sh ... -c 'builtin' [BARGV...] "" |
| 6576 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6577 | if (!G.root_pid) |
| 6578 | G.root_pid = getpid(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6579 | G.global_argv = argv + optind; |
| 6580 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6581 | if (builtin_argc) { |
| 6582 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 6583 | const struct built_in_command *x; |
| 6584 | |
| 6585 | block_signals(0); /* 0: called 1st time */ |
| 6586 | x = find_builtin(optarg); |
| 6587 | if (x) { /* paranoia */ |
| 6588 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 6589 | G.global_argv += builtin_argc; |
| 6590 | G.global_argv[-1] = NULL; /* replace "" */ |
| 6591 | G.last_exitcode = x->function(argv + optind - 1); |
| 6592 | } |
| 6593 | goto final_return; |
| 6594 | } |
| 6595 | if (!G.global_argv[0]) { |
| 6596 | /* -c 'script' (no params): prevent empty $0 */ |
| 6597 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 6598 | G.global_argv[0] = argv[0]; |
| 6599 | G.global_argc--; |
| 6600 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6601 | block_signals(0); /* 0: called 1st time */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6602 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6603 | goto final_return; |
| 6604 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 6605 | /* Well, we cannot just declare interactiveness, |
| 6606 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6607 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6608 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 6609 | case 's': |
| 6610 | /* "-s" means "read from stdin", but this is how we always |
| 6611 | * operate, so simply do nothing here. */ |
| 6612 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6613 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 6614 | case '<': /* "big heredoc" support */ |
| 6615 | full_write(STDOUT_FILENO, optarg, strlen(optarg)); |
| 6616 | _exit(0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6617 | case '$': |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6618 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 6619 | optarg++; |
| 6620 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 6621 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6622 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6623 | optarg++; |
| 6624 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6625 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6626 | optarg++; |
| 6627 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6628 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6629 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6630 | case 'R': |
| 6631 | case 'V': |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6632 | set_local_var(xstrdup(optarg), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ opt == 'R'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6633 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 6634 | # if ENABLE_HUSH_FUNCTIONS |
| 6635 | case 'F': { |
| 6636 | struct function *funcp = new_function(optarg); |
| 6637 | /* funcp->name is already set to optarg */ |
| 6638 | /* funcp->body is set to NULL. It's a special case. */ |
| 6639 | funcp->body_as_string = argv[optind]; |
| 6640 | optind++; |
| 6641 | break; |
| 6642 | } |
| 6643 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6644 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6645 | case 'n': |
| 6646 | case 'x': |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6647 | if (!set_mode('-', opt)) |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6648 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6649 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6650 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6651 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 6652 | " or: sh -c command [args]...\n\n"); |
| 6653 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6654 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6655 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6656 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6657 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6658 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6659 | |
| 6660 | if (!G.root_pid) |
| 6661 | G.root_pid = getpid(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6662 | |
| 6663 | /* If we are login shell... */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6664 | if (argv[0] && argv[0][0] == '-') { |
| 6665 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6666 | debug_printf("sourcing /etc/profile\n"); |
| 6667 | input = fopen_for_read("/etc/profile"); |
| 6668 | if (input != NULL) { |
| 6669 | close_on_exec_on(fileno(input)); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6670 | block_signals(0); /* 0: called 1st time */ |
| 6671 | signal_mask_is_inited = 1; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6672 | parse_and_run_file(input); |
| 6673 | fclose(input); |
| 6674 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6675 | /* bash: after sourcing /etc/profile, |
| 6676 | * tries to source (in the given order): |
| 6677 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 6678 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6679 | * bash also sources ~/.bash_logout on exit. |
| 6680 | * If called as sh, skips .bash_XXX files. |
| 6681 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6682 | } |
| 6683 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6684 | if (argv[optind]) { |
| 6685 | FILE *input; |
| 6686 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6687 | * "bash <script>" (which is never interactive (unless -i?)) |
| 6688 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6689 | * If called as sh, does the same but with $ENV. |
| 6690 | */ |
| 6691 | debug_printf("running script '%s'\n", argv[optind]); |
| 6692 | G.global_argv = argv + optind; |
| 6693 | G.global_argc = argc - optind; |
| 6694 | input = xfopen_for_read(argv[optind]); |
| 6695 | close_on_exec_on(fileno(input)); |
| 6696 | if (!signal_mask_is_inited) |
| 6697 | block_signals(0); /* 0: called 1st time */ |
| 6698 | parse_and_run_file(input); |
| 6699 | #if ENABLE_FEATURE_CLEAN_UP |
| 6700 | fclose(input); |
| 6701 | #endif |
| 6702 | goto final_return; |
| 6703 | } |
| 6704 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6705 | /* Up to here, shell was non-interactive. Now it may become one. |
| 6706 | * NB: don't forget to (re)run block_signals(0/1) as needed. |
| 6707 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6708 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 6709 | /* A shell is interactive if the '-i' flag was given, |
| 6710 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 6711 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6712 | * no arguments remaining or the -s flag given |
| 6713 | * standard input is a terminal |
| 6714 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6715 | * Refer to Posix.2, the description of the 'sh' utility. |
| 6716 | */ |
| 6717 | #if ENABLE_HUSH_JOB |
| 6718 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6719 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 6720 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 6721 | if (G_saved_tty_pgrp < 0) |
| 6722 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6723 | |
| 6724 | /* try to dup stdin to high fd#, >= 255 */ |
| 6725 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 6726 | if (G_interactive_fd < 0) { |
| 6727 | /* try to dup to any fd */ |
| 6728 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6729 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6730 | /* give up */ |
| 6731 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6732 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 6733 | } |
| 6734 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6735 | // TODO: track & disallow any attempts of user |
| 6736 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6737 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6738 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6739 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6740 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6741 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6742 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6743 | /* If we were run as 'hush &', sleep until we are |
| 6744 | * in the foreground (tty pgrp == our pgrp). |
| 6745 | * If we get started under a job aware app (like bash), |
| 6746 | * make sure we are now in charge so we don't fight over |
| 6747 | * who gets the foreground */ |
| 6748 | while (1) { |
| 6749 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6750 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 6751 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6752 | break; |
| 6753 | /* send TTIN to ourself (should stop us) */ |
| 6754 | kill(- shell_pgrp, SIGTTIN); |
| 6755 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6756 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6757 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6758 | /* Block some signals */ |
| 6759 | block_signals(signal_mask_is_inited); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6760 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6761 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6762 | /* Set other signals to restore saved_tty_pgrp */ |
| 6763 | set_fatal_handlers(); |
| 6764 | /* Put ourselves in our own process group |
| 6765 | * (bash, too, does this only if ctty is available) */ |
| 6766 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 6767 | /* Grab control of the terminal */ |
| 6768 | tcsetpgrp(G_interactive_fd, getpid()); |
| 6769 | } |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 6770 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 6771 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6772 | enable_restore_tty_pgrp_on_exit(); /* sets die_sleep = -1 */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6773 | } else if (!signal_mask_is_inited) { |
| 6774 | block_signals(0); /* 0: called 1st time */ |
| 6775 | } /* else: block_signals(0) was done before */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6776 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6777 | /* No job control compiled in, only prompt/line editing */ |
| 6778 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6779 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 6780 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6781 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6782 | G_interactive_fd = dup(STDIN_FILENO); |
| 6783 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6784 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6785 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6786 | } |
| 6787 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6788 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6789 | close_on_exec_on(G_interactive_fd); |
| 6790 | block_signals(signal_mask_is_inited); |
| 6791 | } else if (!signal_mask_is_inited) { |
| 6792 | block_signals(0); |
| 6793 | } |
| 6794 | #else |
| 6795 | /* We have interactiveness code disabled */ |
| 6796 | if (!signal_mask_is_inited) { |
| 6797 | block_signals(0); |
| 6798 | } |
| 6799 | #endif |
| 6800 | /* bash: |
| 6801 | * if interactive but not a login shell, sources ~/.bashrc |
| 6802 | * (--norc turns this off, --rcfile <file> overrides) |
| 6803 | */ |
| 6804 | |
| 6805 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Mike Frysinger | 258275d | 2009-04-05 21:19:43 +0000 | [diff] [blame] | 6806 | printf("\n\n%s hush - the humble shell\n", bb_banner); |
Mike Frysinger | 26cf283 | 2009-04-24 06:40:30 +0000 | [diff] [blame] | 6807 | if (ENABLE_HUSH_HELP) |
| 6808 | puts("Enter 'help' for a list of built-in commands."); |
| 6809 | puts(""); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 6810 | } |
| 6811 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6812 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6813 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6814 | final_return: |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 6815 | #if ENABLE_FEATURE_CLEAN_UP |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6816 | if (G.cwd != bb_msg_unknown) |
| 6817 | free((char*)G.cwd); |
| 6818 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6819 | while (cur_var) { |
| 6820 | struct variable *tmp = cur_var; |
| 6821 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 6822 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6823 | cur_var = cur_var->next; |
| 6824 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 6825 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6826 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6827 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6828 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 6829 | |
| 6830 | |
| 6831 | #if ENABLE_LASH |
| 6832 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 6833 | int lash_main(int argc, char **argv) |
| 6834 | { |
Denys Vlasenko | 0793491 | 2009-08-20 03:40:04 +0200 | [diff] [blame] | 6835 | bb_error_msg("lash is deprecated, please use hush instead"); |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 6836 | return hush_main(argc, argv); |
| 6837 | } |
| 6838 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6839 | |
Denys Vlasenko | 1cc4b13 | 2009-08-21 00:05:51 +0200 | [diff] [blame] | 6840 | #if ENABLE_MSH |
| 6841 | int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 6842 | int msh_main(int argc, char **argv) |
| 6843 | { |
| 6844 | //bb_error_msg("msh is deprecated, please use hush instead"); |
| 6845 | return hush_main(argc, argv); |
| 6846 | } |
| 6847 | #endif |
| 6848 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6849 | |
| 6850 | /* |
| 6851 | * Built-ins |
| 6852 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6853 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6854 | { |
| 6855 | return 0; |
| 6856 | } |
| 6857 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6858 | static int FAST_FUNC builtin_test(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6859 | { |
| 6860 | int argc = 0; |
| 6861 | while (*argv) { |
| 6862 | argc++; |
| 6863 | argv++; |
| 6864 | } |
| 6865 | return test_main(argc, argv - argc); |
| 6866 | } |
| 6867 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6868 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6869 | { |
| 6870 | int argc = 0; |
| 6871 | while (*argv) { |
| 6872 | argc++; |
| 6873 | argv++; |
| 6874 | } |
| 6875 | return echo_main(argc, argv - argc); |
| 6876 | } |
| 6877 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6878 | static int FAST_FUNC builtin_eval(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6879 | { |
| 6880 | int rcode = EXIT_SUCCESS; |
| 6881 | |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6882 | if (*++argv) { |
| 6883 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6884 | /* bash: |
| 6885 | * eval "echo Hi; done" ("done" is syntax error): |
| 6886 | * "echo Hi" will not execute too. |
| 6887 | */ |
| 6888 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6889 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6890 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6891 | } |
| 6892 | return rcode; |
| 6893 | } |
| 6894 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6895 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6896 | { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6897 | const char *newdir = argv[1]; |
| 6898 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6899 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6900 | * bash says "bash: cd: HOME not set" and does nothing |
| 6901 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6902 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 6903 | const char *home = get_local_var_value("HOME"); |
| 6904 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6905 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6906 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6907 | /* Mimic bash message exactly */ |
| 6908 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6909 | return EXIT_FAILURE; |
| 6910 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 6911 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 6912 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 6913 | * set it again, but do not export. bash does the same. |
| 6914 | */ |
| 6915 | set_pwd_var(/*exp:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6916 | return EXIT_SUCCESS; |
| 6917 | } |
| 6918 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6919 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6920 | { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6921 | if (*++argv == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6922 | return EXIT_SUCCESS; /* bash does this */ |
| 6923 | { |
| 6924 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6925 | nommu_save_t dummy; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6926 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 6927 | /* TODO: if exec fails, bash does NOT exit! We do... */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6928 | pseudo_exec_argv(&dummy, argv, 0, NULL); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6929 | /* never returns */ |
| 6930 | } |
| 6931 | } |
| 6932 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6933 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6934 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 6935 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 6936 | |
| 6937 | /* interactive bash: |
| 6938 | * # trap "echo EEE" EXIT |
| 6939 | * # exit |
| 6940 | * exit |
| 6941 | * There are stopped jobs. |
| 6942 | * (if there are _stopped_ jobs, running ones don't count) |
| 6943 | * # exit |
| 6944 | * exit |
| 6945 | # EEE (then bash exits) |
| 6946 | * |
| 6947 | * we can use G.exiting = -1 as indicator "last cmd was exit" |
| 6948 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 6949 | |
| 6950 | /* note: EXIT trap is run by hush_exit */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6951 | if (*++argv == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6952 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6953 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 6954 | xfunc_error_retval = 255; |
| 6955 | /* bash: exit -2 == exit 254, no error msg */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6956 | hush_exit(xatoi(*argv) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6957 | } |
| 6958 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6959 | static void print_escaped(const char *s) |
| 6960 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6961 | if (*s == '\'') |
| 6962 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6963 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6964 | const char *p = strchrnul(s, '\''); |
| 6965 | /* print 'xxxx', possibly just '' */ |
| 6966 | printf("'%.*s'", (int)(p - s), s); |
| 6967 | if (*p == '\0') |
| 6968 | break; |
| 6969 | s = p; |
| 6970 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6971 | /* s points to '; print "'''...'''" */ |
| 6972 | putchar('"'); |
| 6973 | do putchar('\''); while (*++s == '\''); |
| 6974 | putchar('"'); |
| 6975 | } while (*s); |
| 6976 | } |
| 6977 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6978 | #if !ENABLE_HUSH_LOCAL |
| 6979 | #define helper_export_local(argv, exp, lvl) \ |
| 6980 | helper_export_local(argv, exp) |
| 6981 | #endif |
| 6982 | static void helper_export_local(char **argv, int exp, int lvl) |
| 6983 | { |
| 6984 | do { |
| 6985 | char *name = *argv; |
| 6986 | |
| 6987 | /* So far we do not check that name is valid (TODO?) */ |
| 6988 | |
| 6989 | if (strchr(name, '=') == NULL) { |
| 6990 | struct variable *var; |
| 6991 | |
| 6992 | var = get_local_var(name); |
| 6993 | if (exp == -1) { /* unexporting? */ |
| 6994 | /* export -n NAME (without =VALUE) */ |
| 6995 | if (var) { |
| 6996 | var->flg_export = 0; |
| 6997 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 6998 | unsetenv(name); |
| 6999 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 7000 | continue; |
| 7001 | } |
| 7002 | if (exp == 1) { /* exporting? */ |
| 7003 | /* export NAME (without =VALUE) */ |
| 7004 | if (var) { |
| 7005 | var->flg_export = 1; |
| 7006 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 7007 | putenv(var->varstr); |
| 7008 | continue; |
| 7009 | } |
| 7010 | } |
| 7011 | /* Exporting non-existing variable. |
| 7012 | * bash does not put it in environment, |
| 7013 | * but remembers that it is exported, |
| 7014 | * and does put it in env when it is set later. |
| 7015 | * We just set it to "" and export. */ |
| 7016 | /* Or, it's "local NAME" (without =VALUE). |
| 7017 | * bash sets the value to "". */ |
| 7018 | name = xasprintf("%s=", name); |
| 7019 | } else { |
| 7020 | /* (Un)exporting/making local NAME=VALUE */ |
| 7021 | name = xstrdup(name); |
| 7022 | } |
| 7023 | set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0); |
| 7024 | } while (*++argv); |
| 7025 | } |
| 7026 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7027 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7028 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 7029 | unsigned opt_unexport; |
| 7030 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 7031 | #if ENABLE_HUSH_EXPORT_N |
| 7032 | /* "!": do not abort on errors */ |
| 7033 | opt_unexport = getopt32(argv, "!n"); |
| 7034 | if (opt_unexport == (uint32_t)-1) |
| 7035 | return EXIT_FAILURE; |
| 7036 | argv += optind; |
| 7037 | #else |
| 7038 | opt_unexport = 0; |
| 7039 | argv++; |
| 7040 | #endif |
| 7041 | |
| 7042 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7043 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7044 | if (e) { |
| 7045 | while (*e) { |
| 7046 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7047 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7048 | #else |
| 7049 | /* ash emits: export VAR='VAL' |
| 7050 | * bash: declare -x VAR="VAL" |
| 7051 | * we follow ash example */ |
| 7052 | const char *s = *e++; |
| 7053 | const char *p = strchr(s, '='); |
| 7054 | |
| 7055 | if (!p) /* wtf? take next variable */ |
| 7056 | continue; |
| 7057 | /* export var= */ |
| 7058 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7059 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7060 | putchar('\n'); |
| 7061 | #endif |
| 7062 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7063 | /*fflush(stdout); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7064 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7065 | return EXIT_SUCCESS; |
| 7066 | } |
| 7067 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7068 | helper_export_local(argv, (opt_unexport ? -1 : 1), 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7069 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7070 | return EXIT_SUCCESS; |
| 7071 | } |
| 7072 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7073 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7074 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7075 | { |
| 7076 | if (G.func_nest_level == 0) { |
| 7077 | bb_error_msg("%s: not in a function", argv[0]); |
| 7078 | return EXIT_FAILURE; /* bash compat */ |
| 7079 | } |
| 7080 | helper_export_local(argv, 0, G.func_nest_level); |
| 7081 | return EXIT_SUCCESS; |
| 7082 | } |
| 7083 | #endif |
| 7084 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7085 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7086 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7087 | int sig; |
| 7088 | char *new_cmd; |
| 7089 | |
| 7090 | if (!G.traps) |
| 7091 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 7092 | |
| 7093 | argv++; |
| 7094 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7095 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7096 | /* No args: print all trapped */ |
| 7097 | for (i = 0; i < NSIG; ++i) { |
| 7098 | if (G.traps[i]) { |
| 7099 | printf("trap -- "); |
| 7100 | print_escaped(G.traps[i]); |
Denys Vlasenko | 91836ba | 2009-09-23 01:46:19 +0200 | [diff] [blame] | 7101 | /* bash compat: it says SIGxxx, not just xxx */ |
Denys Vlasenko | 2f7894b | 2009-09-24 01:43:21 +0200 | [diff] [blame] | 7102 | printf(" %s%s\n", i == 0 ? "" : "SIG", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7103 | } |
| 7104 | } |
| 7105 | /*fflush(stdout); - done after each builtin anyway */ |
| 7106 | return EXIT_SUCCESS; |
| 7107 | } |
| 7108 | |
| 7109 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7110 | /* If first arg is a number: reset all specified signals */ |
| 7111 | sig = bb_strtou(*argv, NULL, 10); |
| 7112 | if (errno == 0) { |
| 7113 | int ret; |
| 7114 | process_sig_list: |
| 7115 | ret = EXIT_SUCCESS; |
| 7116 | while (*argv) { |
| 7117 | sig = get_signum(*argv++); |
| 7118 | if (sig < 0 || sig >= NSIG) { |
| 7119 | ret = EXIT_FAILURE; |
| 7120 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7121 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7122 | continue; |
| 7123 | } |
| 7124 | |
| 7125 | free(G.traps[sig]); |
| 7126 | G.traps[sig] = xstrdup(new_cmd); |
| 7127 | |
| 7128 | debug_printf("trap: setting SIG%s (%i) to '%s'", |
| 7129 | get_signame(sig), sig, G.traps[sig]); |
| 7130 | |
| 7131 | /* There is no signal for 0 (EXIT) */ |
| 7132 | if (sig == 0) |
| 7133 | continue; |
| 7134 | |
| 7135 | if (new_cmd) { |
| 7136 | sigaddset(&G.blocked_set, sig); |
| 7137 | } else { |
| 7138 | /* There was a trap handler, we are removing it |
| 7139 | * (if sig has non-DFL handling, |
| 7140 | * we don't need to do anything) */ |
| 7141 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 7142 | continue; |
| 7143 | sigdelset(&G.blocked_set, sig); |
| 7144 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7145 | } |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7146 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7147 | return ret; |
| 7148 | } |
| 7149 | |
| 7150 | if (!argv[1]) { /* no second arg */ |
| 7151 | bb_error_msg("trap: invalid arguments"); |
| 7152 | return EXIT_FAILURE; |
| 7153 | } |
| 7154 | |
| 7155 | /* First arg is "-": reset all specified to default */ |
| 7156 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 7157 | /* Everything else: set arg as signal handler |
| 7158 | * (includes "" case, which ignores signal) */ |
| 7159 | if (argv[0][0] == '-') { |
| 7160 | if (argv[0][1] == '\0') { /* "-" */ |
| 7161 | /* new_cmd remains NULL: "reset these sigs" */ |
| 7162 | goto reset_traps; |
| 7163 | } |
| 7164 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 7165 | argv++; |
| 7166 | } |
| 7167 | /* else: "-something", no special meaning */ |
| 7168 | } |
| 7169 | new_cmd = *argv; |
| 7170 | reset_traps: |
| 7171 | argv++; |
| 7172 | goto process_sig_list; |
| 7173 | } |
| 7174 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7175 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7176 | static int FAST_FUNC builtin_type(char **argv) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7177 | { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7178 | int ret = EXIT_SUCCESS; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7179 | |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7180 | while (*++argv) { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7181 | const char *type; |
Denys Vlasenko | 171932d | 2009-05-28 17:07:22 +0200 | [diff] [blame] | 7182 | char *path = NULL; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7183 | |
| 7184 | if (0) {} /* make conditional compile easier below */ |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7185 | /*else if (find_alias(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7186 | type = "an alias";*/ |
| 7187 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7188 | else if (find_function(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7189 | type = "a function"; |
| 7190 | #endif |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7191 | else if (find_builtin(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7192 | type = "a shell builtin"; |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7193 | else if ((path = find_in_path(*argv)) != NULL) |
| 7194 | type = path; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7195 | else { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7196 | bb_error_msg("type: %s: not found", *argv); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7197 | ret = EXIT_FAILURE; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7198 | continue; |
| 7199 | } |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7200 | |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7201 | printf("%s is %s\n", *argv, type); |
| 7202 | free(path); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7203 | } |
| 7204 | |
| 7205 | return ret; |
| 7206 | } |
| 7207 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7208 | #if ENABLE_HUSH_JOB |
| 7209 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7210 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7211 | { |
| 7212 | int i, jobnum; |
| 7213 | struct pipe *pi; |
| 7214 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7215 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7216 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7217 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7218 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 7219 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7220 | for (pi = G.job_list; pi; pi = pi->next) { |
| 7221 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7222 | goto found; |
| 7223 | } |
| 7224 | } |
| 7225 | bb_error_msg("%s: no current job", argv[0]); |
| 7226 | return EXIT_FAILURE; |
| 7227 | } |
| 7228 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 7229 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 7230 | return EXIT_FAILURE; |
| 7231 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7232 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7233 | if (pi->jobid == jobnum) { |
| 7234 | goto found; |
| 7235 | } |
| 7236 | } |
| 7237 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 7238 | return EXIT_FAILURE; |
| 7239 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 7240 | /* TODO: bash prints a string representation |
| 7241 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7242 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7243 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7244 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7245 | } |
| 7246 | |
| 7247 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7248 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 7249 | for (i = 0; i < pi->num_cmds; i++) { |
| 7250 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 7251 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7252 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7253 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7254 | |
| 7255 | i = kill(- pi->pgrp, SIGCONT); |
| 7256 | if (i < 0) { |
| 7257 | if (errno == ESRCH) { |
| 7258 | delete_finished_bg_job(pi); |
| 7259 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7260 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7261 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7262 | } |
| 7263 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7264 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7265 | remove_bg_job(pi); |
| 7266 | return checkjobs_and_fg_shell(pi); |
| 7267 | } |
| 7268 | return EXIT_SUCCESS; |
| 7269 | } |
| 7270 | #endif |
| 7271 | |
| 7272 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7273 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7274 | { |
| 7275 | const struct built_in_command *x; |
| 7276 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7277 | printf( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7278 | "Built-in commands:\n" |
| 7279 | "------------------\n"); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7280 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
| 7281 | if (x->descr) |
| 7282 | printf("%s\t%s\n", x->cmd, x->descr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7283 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7284 | bb_putchar('\n'); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7285 | return EXIT_SUCCESS; |
| 7286 | } |
| 7287 | #endif |
| 7288 | |
| 7289 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7290 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7291 | { |
| 7292 | struct pipe *job; |
| 7293 | const char *status_string; |
| 7294 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7295 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7296 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7297 | status_string = "Stopped"; |
| 7298 | else |
| 7299 | status_string = "Running"; |
| 7300 | |
| 7301 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 7302 | } |
| 7303 | return EXIT_SUCCESS; |
| 7304 | } |
| 7305 | #endif |
| 7306 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7307 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7308 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7309 | { |
| 7310 | void *p; |
| 7311 | unsigned long l; |
| 7312 | |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 7313 | #ifdef M_TRIM_THRESHOLD |
| 7314 | /* Optional. Reduces probability of false positives */ |
| 7315 | malloc_trim(0); |
| 7316 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7317 | /* Crude attempt to find where "free memory" starts, |
| 7318 | * sans fragmentation. */ |
| 7319 | p = malloc(240); |
| 7320 | l = (unsigned long)p; |
| 7321 | free(p); |
| 7322 | p = malloc(3400); |
| 7323 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 7324 | free(p); |
| 7325 | |
| 7326 | if (!G.memleak_value) |
| 7327 | G.memleak_value = l; |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 7328 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7329 | l -= G.memleak_value; |
| 7330 | if ((long)l < 0) |
| 7331 | l = 0; |
| 7332 | l /= 1024; |
| 7333 | if (l > 127) |
| 7334 | l = 127; |
| 7335 | |
| 7336 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 7337 | return l; |
| 7338 | } |
| 7339 | #endif |
| 7340 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7341 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7342 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7343 | puts(get_cwd(0)); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7344 | return EXIT_SUCCESS; |
| 7345 | } |
| 7346 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7347 | static int FAST_FUNC builtin_read(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7348 | { |
| 7349 | char *string; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 7350 | const char *name = "REPLY"; |
| 7351 | |
| 7352 | if (argv[1]) { |
| 7353 | name = argv[1]; |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 7354 | /* bash (3.2.33(1)) bug: "read 0abcd" will execute, |
| 7355 | * and _after_ that_ it will complain */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 7356 | if (!is_well_formed_var_name(name, '\0')) { |
| 7357 | /* Mimic bash message */ |
| 7358 | bb_error_msg("read: '%s': not a valid identifier", name); |
| 7359 | return 1; |
| 7360 | } |
| 7361 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7362 | |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 7363 | //TODO: bash unbackslashes input, splits words and puts them in argv[i] |
| 7364 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7365 | string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7366 | return set_local_var(string, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7367 | } |
| 7368 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7369 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 7370 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7371 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7372 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 7373 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7374 | * set -- [argument...] |
| 7375 | * set -o |
| 7376 | * set +o |
| 7377 | * Implementations shall support the options in both their hyphen and |
| 7378 | * plus-sign forms. These options can also be specified as options to sh. |
| 7379 | * Examples: |
| 7380 | * Write out all variables and their values: set |
| 7381 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 7382 | * Turn on the -x and -v options: set -xv |
| 7383 | * Unset all positional parameters: set -- |
| 7384 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 7385 | * Set the positional parameters to the expansion of x, even if x expands |
| 7386 | * with a leading '-' or '+': set -- $x |
| 7387 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7388 | * 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] | 7389 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7390 | static int FAST_FUNC builtin_set(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7391 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7392 | int n; |
| 7393 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7394 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7395 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7396 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7397 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7398 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7399 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7400 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7401 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7402 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7403 | do { |
| 7404 | if (!strcmp(arg, "--")) { |
| 7405 | ++argv; |
| 7406 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7407 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 7408 | if (arg[0] != '+' && arg[0] != '-') |
| 7409 | break; |
| 7410 | for (n = 1; arg[n]; ++n) |
| 7411 | if (set_mode(arg[0], arg[n])) |
| 7412 | goto error; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7413 | } while ((arg = *++argv) != NULL); |
| 7414 | /* Now argv[0] is 1st argument */ |
| 7415 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7416 | if (arg == NULL) |
| 7417 | return EXIT_SUCCESS; |
| 7418 | set_argv: |
| 7419 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7420 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 7421 | g_argv = G.global_argv; |
| 7422 | if (G.global_args_malloced) { |
| 7423 | pp = g_argv; |
| 7424 | while (*++pp) |
| 7425 | free(*pp); |
| 7426 | g_argv[1] = NULL; |
| 7427 | } else { |
| 7428 | G.global_args_malloced = 1; |
| 7429 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 7430 | pp[0] = g_argv[0]; /* retain $0 */ |
| 7431 | g_argv = pp; |
| 7432 | } |
| 7433 | /* This realloc's G.global_argv */ |
| 7434 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 7435 | |
| 7436 | n = 1; |
| 7437 | while (*++pp) |
| 7438 | n++; |
| 7439 | G.global_argc = n; |
| 7440 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7441 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7442 | |
| 7443 | /* Nothing known, so abort */ |
| 7444 | error: |
| 7445 | bb_error_msg("set: %s: invalid option", arg); |
| 7446 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7447 | } |
| 7448 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7449 | static int FAST_FUNC builtin_shift(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7450 | { |
| 7451 | int n = 1; |
| 7452 | if (argv[1]) { |
| 7453 | n = atoi(argv[1]); |
| 7454 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7455 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 7456 | if (G.global_args_malloced) { |
| 7457 | int m = 1; |
| 7458 | while (m <= n) |
| 7459 | free(G.global_argv[m++]); |
| 7460 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7461 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 7462 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 7463 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7464 | return EXIT_SUCCESS; |
| 7465 | } |
| 7466 | return EXIT_FAILURE; |
| 7467 | } |
| 7468 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7469 | static int FAST_FUNC builtin_source(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7470 | { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7471 | char *arg_path; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7472 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7473 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7474 | #if ENABLE_HUSH_FUNCTIONS |
| 7475 | smallint sv_flg; |
| 7476 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7477 | |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7478 | if (*++argv == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7479 | return EXIT_FAILURE; |
| 7480 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7481 | if (strchr(*argv, '/') == NULL && (arg_path = find_in_path(*argv)) != NULL) { |
| 7482 | input = fopen_for_read(arg_path); |
| 7483 | free(arg_path); |
| 7484 | } else |
| 7485 | input = fopen_or_warn(*argv, "r"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7486 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7487 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7488 | return EXIT_FAILURE; |
| 7489 | } |
| 7490 | close_on_exec_on(fileno(input)); |
| 7491 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7492 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7493 | sv_flg = G.flag_return_in_progress; |
| 7494 | /* "we are inside sourced file, ok to use return" */ |
| 7495 | G.flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7496 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7497 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7498 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7499 | parse_and_run_file(input); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7500 | fclose(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7501 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7502 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7503 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7504 | G.flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7505 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7506 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7507 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7508 | } |
| 7509 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7510 | static int FAST_FUNC builtin_umask(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7511 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7512 | int rc; |
| 7513 | mode_t mask; |
| 7514 | |
| 7515 | mask = umask(0); |
| 7516 | if (argv[1]) { |
| 7517 | mode_t old_mask = mask; |
| 7518 | |
| 7519 | mask ^= 0777; |
| 7520 | rc = bb_parse_mode(argv[1], &mask); |
| 7521 | mask ^= 0777; |
| 7522 | if (rc == 0) { |
| 7523 | mask = old_mask; |
| 7524 | /* bash messages: |
| 7525 | * bash: umask: 'q': invalid symbolic mode operator |
| 7526 | * bash: umask: 999: octal number out of range |
| 7527 | */ |
| 7528 | bb_error_msg("%s: '%s' invalid mode", argv[0], argv[1]); |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7529 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7530 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7531 | rc = 1; |
| 7532 | /* Mimic bash */ |
| 7533 | printf("%04o\n", (unsigned) mask); |
| 7534 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7535 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7536 | umask(mask); |
| 7537 | |
| 7538 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7539 | } |
| 7540 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7541 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7542 | static int FAST_FUNC builtin_unset(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7543 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7544 | int ret; |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7545 | unsigned opts; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7546 | |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7547 | /* "!": do not abort on errors */ |
| 7548 | /* "+": stop at 1st non-option */ |
| 7549 | opts = getopt32(argv, "!+vf"); |
| 7550 | if (opts == (unsigned)-1) |
| 7551 | return EXIT_FAILURE; |
| 7552 | if (opts == 3) { |
| 7553 | bb_error_msg("unset: -v and -f are exclusive"); |
| 7554 | return EXIT_FAILURE; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7555 | } |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7556 | argv += optind; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7557 | |
| 7558 | ret = EXIT_SUCCESS; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7559 | while (*argv) { |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7560 | if (!(opts & 2)) { /* not -f */ |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7561 | if (unset_local_var(*argv)) { |
| 7562 | /* unset <nonexistent_var> doesn't fail. |
| 7563 | * Error is when one tries to unset RO var. |
| 7564 | * Message was printed by unset_local_var. */ |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7565 | ret = EXIT_FAILURE; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7566 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7567 | } |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 7568 | #if ENABLE_HUSH_FUNCTIONS |
| 7569 | else { |
| 7570 | unset_func(*argv); |
| 7571 | } |
| 7572 | #endif |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7573 | argv++; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7574 | } |
| 7575 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7576 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7577 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7578 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7579 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7580 | { |
| 7581 | int ret = EXIT_SUCCESS; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7582 | int status, sig; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7583 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7584 | if (*++argv == NULL) { |
| 7585 | /* Don't care about wait results */ |
| 7586 | /* Note 1: must wait until there are no more children */ |
| 7587 | /* Note 2: must be interruptible */ |
| 7588 | /* Examples: |
| 7589 | * $ sleep 3 & sleep 6 & wait |
| 7590 | * [1] 30934 sleep 3 |
| 7591 | * [2] 30935 sleep 6 |
| 7592 | * [1] Done sleep 3 |
| 7593 | * [2] Done sleep 6 |
| 7594 | * $ sleep 3 & sleep 6 & wait |
| 7595 | * [1] 30936 sleep 3 |
| 7596 | * [2] 30937 sleep 6 |
| 7597 | * [1] Done sleep 3 |
| 7598 | * ^C <-- after ~4 sec from keyboard |
| 7599 | * $ |
| 7600 | */ |
| 7601 | sigaddset(&G.blocked_set, SIGCHLD); |
| 7602 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7603 | while (1) { |
| 7604 | checkjobs(NULL); |
| 7605 | if (errno == ECHILD) |
| 7606 | break; |
| 7607 | /* Wait for SIGCHLD or any other signal of interest */ |
| 7608 | /* sigtimedwait with infinite timeout: */ |
| 7609 | sig = sigwaitinfo(&G.blocked_set, NULL); |
| 7610 | if (sig > 0) { |
| 7611 | sig = check_and_run_traps(sig); |
| 7612 | if (sig && sig != SIGCHLD) { /* see note 2 */ |
| 7613 | ret = 128 + sig; |
| 7614 | break; |
| 7615 | } |
| 7616 | } |
| 7617 | } |
| 7618 | sigdelset(&G.blocked_set, SIGCHLD); |
| 7619 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7620 | return ret; |
| 7621 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7622 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7623 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7624 | while (*argv) { |
| 7625 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 7626 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7627 | /* mimic bash message */ |
| 7628 | 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] | 7629 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7630 | } |
| 7631 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7632 | if (WIFSIGNALED(status)) |
| 7633 | ret = 128 + WTERMSIG(status); |
| 7634 | else if (WIFEXITED(status)) |
| 7635 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7636 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7637 | ret = EXIT_FAILURE; |
| 7638 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7639 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7640 | ret = 127; |
| 7641 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7642 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7643 | } |
| 7644 | |
| 7645 | return ret; |
| 7646 | } |
| 7647 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7648 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 7649 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 7650 | { |
| 7651 | if (argv[1]) { |
| 7652 | def = bb_strtou(argv[1], NULL, 10); |
| 7653 | if (errno || def < def_min || argv[2]) { |
| 7654 | bb_error_msg("%s: bad arguments", argv[0]); |
| 7655 | def = UINT_MAX; |
| 7656 | } |
| 7657 | } |
| 7658 | return def; |
| 7659 | } |
| 7660 | #endif |
| 7661 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 7662 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7663 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7664 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7665 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7666 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 7667 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 7668 | return EXIT_SUCCESS; /* bash compat */ |
| 7669 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7670 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7671 | |
| 7672 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 7673 | if (depth == UINT_MAX) |
| 7674 | G.flag_break_continue = BC_BREAK; |
| 7675 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7676 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7677 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7678 | return EXIT_SUCCESS; |
| 7679 | } |
| 7680 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7681 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7682 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 7683 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 7684 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7685 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 7686 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7687 | |
| 7688 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7689 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7690 | { |
| 7691 | int rc; |
| 7692 | |
| 7693 | if (G.flag_return_in_progress != -1) { |
| 7694 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 7695 | return EXIT_FAILURE; /* bash compat */ |
| 7696 | } |
| 7697 | |
| 7698 | G.flag_return_in_progress = 1; |
| 7699 | |
| 7700 | /* bash: |
| 7701 | * out of range: wraps around at 256, does not error out |
| 7702 | * non-numeric param: |
| 7703 | * f() { false; return qwe; }; f; echo $? |
| 7704 | * bash: return: qwe: numeric argument required <== we do this |
| 7705 | * 255 <== we also do this |
| 7706 | */ |
| 7707 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 7708 | return rc; |
| 7709 | } |
| 7710 | #endif |