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 | 1967978 | 2009-08-12 14:53:44 +0200 | [diff] [blame] | 61 | * special variables (PWD etc) |
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 */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 81 | #include <glob.h> |
| 82 | /* #include <dmalloc.h> */ |
| 83 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 84 | # include <fnmatch.h> |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 85 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 86 | #include "math.h" |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 87 | #include "match.h" |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 88 | #ifndef PIPE_BUF |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 89 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 90 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 91 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 92 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 93 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 94 | #define LEAK_HUNTING 0 |
| 95 | #define BUILD_AS_NOMMU 0 |
| 96 | /* Enable/disable sanity checks. Ok to enable in production, |
| 97 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 98 | * Keeping 1 for now even in released versions. |
| 99 | */ |
| 100 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 101 | /* Slightly bigger (+200 bytes), but faster hush. |
| 102 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 103 | * which allows us to do fewer waitpid's. |
| 104 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 105 | * and therefore waitpid will return the same result as last time) |
| 106 | */ |
| 107 | #define ENABLE_HUSH_FAST 0 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 108 | |
| 109 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 110 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 111 | # undef BB_MMU |
| 112 | # undef USE_FOR_NOMMU |
| 113 | # undef USE_FOR_MMU |
| 114 | # define BB_MMU 0 |
| 115 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 116 | # define USE_FOR_MMU(...) |
| 117 | #endif |
| 118 | |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 119 | #if defined SINGLE_APPLET_MAIN |
| 120 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 121 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 122 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 123 | # undef IF_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 124 | # define IF_FEATURE_SH_STANDALONE(...) |
| 125 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denys Vlasenko | 673e945 | 2009-05-27 14:39:35 +0200 | [diff] [blame] | 126 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 127 | #endif |
| 128 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 129 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 130 | # undef ENABLE_FEATURE_EDITING |
| 131 | # define ENABLE_FEATURE_EDITING 0 |
| 132 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 133 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 134 | #endif |
| 135 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 136 | /* Do we support ANY keywords? */ |
| 137 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 138 | # define HAS_KEYWORDS 1 |
| 139 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 140 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 141 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 142 | # define HAS_KEYWORDS 0 |
| 143 | # define IF_HAS_KEYWORDS(...) |
| 144 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 145 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 146 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 147 | /* If you comment out one of these below, it will be #defined later |
| 148 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 149 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 150 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 151 | #define debug_printf_parse(...) do {} while (0) |
| 152 | #define debug_print_tree(a, b) do {} while (0) |
| 153 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 154 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 155 | #define debug_printf_jobs(...) do {} while (0) |
| 156 | #define debug_printf_expand(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 157 | #define debug_printf_glob(...) do {} while (0) |
| 158 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 159 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 160 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 161 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 162 | #define ERR_PTR ((void*)(long)1) |
| 163 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 164 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 165 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 166 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 167 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 168 | struct variable; |
| 169 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 170 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 171 | |
| 172 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 173 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 174 | */ |
| 175 | #if !BB_MMU |
| 176 | typedef struct nommu_save_t { |
| 177 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 178 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 179 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 180 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 181 | } nommu_save_t; |
| 182 | #endif |
| 183 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 184 | typedef enum reserved_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 185 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 186 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 187 | RES_IF , |
| 188 | RES_THEN , |
| 189 | RES_ELIF , |
| 190 | RES_ELSE , |
| 191 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 192 | #endif |
| 193 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 194 | RES_FOR , |
| 195 | RES_WHILE , |
| 196 | RES_UNTIL , |
| 197 | RES_DO , |
| 198 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 199 | #endif |
| 200 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 201 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 202 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 203 | #if ENABLE_HUSH_CASE |
| 204 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 205 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 206 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 207 | RES_MATCH , /* "word)" */ |
| 208 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 209 | RES_ESAC , |
| 210 | #endif |
| 211 | RES_XXXX , |
| 212 | RES_SNTX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 213 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 214 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 215 | typedef struct o_string { |
| 216 | char *data; |
| 217 | int length; /* position where data is appended */ |
| 218 | int maxlen; |
| 219 | /* Protect newly added chars against globbing |
| 220 | * (by prepending \ to *, ?, [, \) */ |
| 221 | smallint o_escape; |
| 222 | smallint o_glob; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 223 | /* At least some part of the string was inside '' or "", |
| 224 | * possibly empty one: word"", wo''rd etc. */ |
| 225 | smallint o_quoted; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 226 | smallint has_empty_slot; |
| 227 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 228 | } o_string; |
| 229 | enum { |
| 230 | MAYBE_ASSIGNMENT = 0, |
| 231 | DEFINITELY_ASSIGNMENT = 1, |
| 232 | NOT_ASSIGNMENT = 2, |
| 233 | WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */ |
| 234 | }; |
| 235 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 236 | #define NULL_O_STRING { NULL } |
| 237 | |
| 238 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
| 239 | * available? Where is it documented? */ |
| 240 | typedef struct in_str { |
| 241 | const char *p; |
| 242 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 243 | char eof_flag; /* meaningless if ->p == NULL */ |
| 244 | char peek_buf[2]; |
| 245 | #if ENABLE_HUSH_INTERACTIVE |
| 246 | smallint promptme; |
| 247 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 248 | #endif |
| 249 | FILE *file; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 250 | int (*get) (struct in_str *) FAST_FUNC; |
| 251 | int (*peek) (struct in_str *) FAST_FUNC; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 252 | } in_str; |
| 253 | #define i_getch(input) ((input)->get(input)) |
| 254 | #define i_peek(input) ((input)->peek(input)) |
| 255 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 256 | /* The descrip member of this structure is only used to make |
| 257 | * debugging output pretty */ |
| 258 | static const struct { |
| 259 | int mode; |
| 260 | signed char default_fd; |
| 261 | char descrip[3]; |
| 262 | } redir_table[] = { |
| 263 | { O_RDONLY, 0, "<" }, |
| 264 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 265 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 266 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 267 | { O_RDONLY, 0, "<<" }, |
| 268 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 269 | /* { O_RDONLY, 77, "<<" }, */ |
| 270 | }; |
| 271 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 272 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 273 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 274 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 275 | int rd_fd; /* fd to redirect */ |
| 276 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 277 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 278 | smallint rd_type; /* (enum redir_type) */ |
| 279 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 280 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 281 | * bit 0: do we need to trim leading tabs? |
| 282 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 283 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 284 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 285 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 286 | REDIRECT_INPUT = 0, |
| 287 | REDIRECT_OVERWRITE = 1, |
| 288 | REDIRECT_APPEND = 2, |
| 289 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 290 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 291 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 292 | |
| 293 | REDIRFD_CLOSE = -3, |
| 294 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 295 | REDIRFD_TO_FILE = -1, |
| 296 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 297 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 298 | HEREDOC_SKIPTABS = 1, |
| 299 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 300 | } redir_type; |
| 301 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 302 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 303 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 304 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 305 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 306 | smallint is_stopped; /* is the command currently running? */ |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 307 | smallint cmd_type; /* CMD_xxx */ |
| 308 | #define CMD_NORMAL 0 |
| 309 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 310 | |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 311 | /* used for "[[ EXPR ]]" */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 312 | #if ENABLE_HUSH_BASH_COMPAT |
| 313 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 314 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 315 | |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 316 | /* used for "export noglob=* glob* a=`echo a b`" */ |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 317 | //#define CMD_SINGLEWORD_NOGLOB_COND 3 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 318 | // It is hard to implement correctly, it adds significant amounts of tricky code, |
| 319 | // and all this is only useful for really obscure export statements |
| 320 | // almost nobody would use anyway. #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 321 | // guards the code which implements it, but I have doubts it works |
| 322 | // in all cases (especially with mixed globbed/non-globbed arguments) |
| 323 | |
| 324 | #if ENABLE_HUSH_FUNCTIONS |
| 325 | # define CMD_FUNCDEF 3 |
| 326 | #endif |
| 327 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 328 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 329 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 330 | #if !BB_MMU |
| 331 | char *group_as_string; |
| 332 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 333 | #if ENABLE_HUSH_FUNCTIONS |
| 334 | struct function *child_func; |
| 335 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 336 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 337 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 338 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 339 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 340 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 341 | * we put those fields back into cmd->xxx |
| 342 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 343 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 344 | * Without this trick, loop would execute a;b;b;b;... |
| 345 | * instead of correct sequence a;b;a;b;... |
| 346 | * When command is freed, it severs the link |
| 347 | * (sets ->child_func->parent_cmd to NULL). |
| 348 | */ |
| 349 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 350 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 351 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 352 | * and on execution these are substituted with their values. |
| 353 | * Substitution can make _several_ words out of one argv[n]! |
| 354 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 355 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 356 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 357 | struct redir_struct *redirects; /* I/O redirections */ |
| 358 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 359 | /* Is there anything in this command at all? */ |
| 360 | #define IS_NULL_CMD(cmd) \ |
| 361 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 362 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 363 | |
| 364 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 365 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 366 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 367 | int alive_cmds; /* number of commands running (not exited) */ |
| 368 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 369 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 370 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 371 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 372 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 373 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 374 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 375 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 376 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 377 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 378 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 379 | typedef enum pipe_style { |
| 380 | PIPE_SEQ = 1, |
| 381 | PIPE_AND = 2, |
| 382 | PIPE_OR = 3, |
| 383 | PIPE_BG = 4, |
| 384 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 385 | /* Is there anything in this pipe at all? */ |
| 386 | #define IS_NULL_PIPE(pi) \ |
| 387 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 388 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 389 | /* This holds pointers to the various results of parsing */ |
| 390 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 391 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 392 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 393 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 394 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 395 | /* last command in pipe (being constructed right now) */ |
| 396 | struct command *command; |
| 397 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 398 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 399 | #if !BB_MMU |
| 400 | o_string as_string; |
| 401 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 402 | #if HAS_KEYWORDS |
| 403 | smallint ctx_res_w; |
| 404 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 405 | #if ENABLE_HUSH_CASE |
| 406 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 407 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 408 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 409 | int old_flag; |
| 410 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 411 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 412 | * when we see "if" or "then", we malloc and copy current context, |
| 413 | * and make ->stack point to it. then we parse pipeN. |
| 414 | * when closing "then" / fi" / whatever is found, |
| 415 | * we move list_head into ->stack->command->group, |
| 416 | * copy ->stack into current context, and delete ->stack. |
| 417 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 418 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 419 | struct parse_context *stack; |
| 420 | #endif |
| 421 | }; |
| 422 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 423 | /* On program start, environ points to initial environment. |
| 424 | * putenv adds new pointers into it, unsetenv removes them. |
| 425 | * Neither of these (de)allocates the strings. |
| 426 | * setenv allocates new strings in malloc space and does putenv, |
| 427 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 428 | #define setenv(...) setenv_is_leaky_dont_use() |
| 429 | struct variable { |
| 430 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 431 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 432 | #if ENABLE_HUSH_LOCAL |
| 433 | unsigned func_nest_level; |
| 434 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 435 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 436 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 437 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 438 | }; |
| 439 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 440 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 441 | BC_BREAK = 1, |
| 442 | BC_CONTINUE = 2, |
| 443 | }; |
| 444 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 445 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 446 | struct function { |
| 447 | struct function *next; |
| 448 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 449 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 450 | struct pipe *body; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 451 | #if !BB_MMU |
| 452 | char *body_as_string; |
| 453 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 454 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 455 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 456 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 457 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 458 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 459 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 460 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 461 | /* interactive_fd != 0 means we are an interactive shell. |
| 462 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 463 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 464 | * job control still works, but terminal signals |
| 465 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 466 | * process groups can only be created with "cmd &". |
| 467 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 468 | * to give tty to the foreground process group, |
| 469 | * and will take it back when the group is stopped (^Z) |
| 470 | * or killed (^C). |
| 471 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 472 | #if ENABLE_HUSH_INTERACTIVE |
| 473 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 474 | * _AND_ if we decided to act interactively */ |
| 475 | int interactive_fd; |
| 476 | const char *PS1; |
| 477 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 478 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 479 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 480 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 481 | #endif |
| 482 | #if ENABLE_FEATURE_EDITING |
| 483 | line_input_t *line_input_state; |
| 484 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 485 | pid_t root_pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 486 | pid_t last_bg_pid; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 487 | #if ENABLE_HUSH_JOB |
| 488 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 489 | int last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 490 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 491 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 492 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 493 | #else |
| 494 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 495 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 496 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 497 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 498 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 499 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 500 | #if ENABLE_HUSH_FUNCTIONS |
| 501 | /* 0: outside of a function (or sourced file) |
| 502 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 503 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 504 | */ |
| 505 | smallint flag_return_in_progress; |
| 506 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 507 | smallint fake_mode; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 508 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 509 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 510 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 511 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 512 | smalluint global_args_malloced; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 513 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 514 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 515 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 516 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 517 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 518 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 519 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 520 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 521 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 522 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 523 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 524 | const char *cwd; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 525 | struct variable *top_var; /* = &G.shell_ver (set in main()) */ |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 526 | struct variable shell_ver; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 527 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 528 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 529 | # if ENABLE_HUSH_LOCAL |
| 530 | struct variable **shadowed_vars_pp; |
| 531 | unsigned func_nest_level; |
| 532 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 533 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 534 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 535 | #if ENABLE_HUSH_FAST |
| 536 | unsigned count_SIGCHLD; |
| 537 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 538 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 539 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 540 | /* which signals have non-DFL handler (even with no traps set)? */ |
| 541 | unsigned non_DFL_mask; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 542 | char **traps; /* char *traps[NSIG] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 543 | sigset_t blocked_set; |
| 544 | sigset_t inherited_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 545 | #if HUSH_DEBUG |
| 546 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 547 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 548 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 549 | char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2]; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 550 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 551 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 552 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 553 | * (too many defines). Also, I actually prefer to see when a variable |
| 554 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 555 | #define INIT_G() do { \ |
| 556 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 557 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 558 | |
| 559 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 560 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 561 | static int builtin_cd(char **argv) FAST_FUNC; |
| 562 | static int builtin_echo(char **argv) FAST_FUNC; |
| 563 | static int builtin_eval(char **argv) FAST_FUNC; |
| 564 | static int builtin_exec(char **argv) FAST_FUNC; |
| 565 | static int builtin_exit(char **argv) FAST_FUNC; |
| 566 | static int builtin_export(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 567 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 568 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 569 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 570 | #endif |
| 571 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 572 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 573 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 574 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 575 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 576 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 577 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 578 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 579 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 580 | static int builtin_pwd(char **argv) FAST_FUNC; |
| 581 | static int builtin_read(char **argv) FAST_FUNC; |
| 582 | static int builtin_set(char **argv) FAST_FUNC; |
| 583 | static int builtin_shift(char **argv) FAST_FUNC; |
| 584 | static int builtin_source(char **argv) FAST_FUNC; |
| 585 | static int builtin_test(char **argv) FAST_FUNC; |
| 586 | static int builtin_trap(char **argv) FAST_FUNC; |
| 587 | static int builtin_type(char **argv) FAST_FUNC; |
| 588 | static int builtin_true(char **argv) FAST_FUNC; |
| 589 | static int builtin_umask(char **argv) FAST_FUNC; |
| 590 | static int builtin_unset(char **argv) FAST_FUNC; |
| 591 | static int builtin_wait(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 592 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 593 | static int builtin_break(char **argv) FAST_FUNC; |
| 594 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 595 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 596 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 597 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 598 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 599 | |
| 600 | /* Table of built-in functions. They can be forked or not, depending on |
| 601 | * context: within pipes, they fork. As simple commands, they do not. |
| 602 | * When used in non-forking context, they can change global variables |
| 603 | * in the parent shell process. If forked, of course they cannot. |
| 604 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 605 | * still be set at the end. */ |
| 606 | struct built_in_command { |
| 607 | const char *cmd; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 608 | int (*function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 609 | #if ENABLE_HUSH_HELP |
| 610 | const char *descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 611 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 612 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 613 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 614 | #endif |
| 615 | }; |
| 616 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 617 | static const struct built_in_command bltins1[] = { |
| 618 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 619 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 620 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 621 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 622 | #endif |
| 623 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 624 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 625 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 626 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 627 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 628 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 629 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 630 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 631 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 632 | BLTIN("exit" , builtin_exit , "Exit"), |
| 633 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 634 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 635 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 636 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 637 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 638 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 639 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 640 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 641 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 642 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 643 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 644 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 645 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 646 | #if HUSH_DEBUG |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 647 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 648 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 649 | BLTIN("read" , builtin_read , "Input into variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 650 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 651 | BLTIN("return" , builtin_return , "Return from a function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 652 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 653 | BLTIN("set" , builtin_set , "Set/unset positional parameters"), |
| 654 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
| 655 | BLTIN("trap" , builtin_trap , "Trap signals"), |
| 656 | BLTIN("type" , builtin_type , "Write a description of command type"), |
| 657 | // BLTIN("ulimit" , builtin_ulimit , "Control resource limits"), |
| 658 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 659 | BLTIN("unset" , builtin_unset , "Unset variables"), |
| 660 | BLTIN("wait" , builtin_wait , "Wait for process"), |
| 661 | }; |
| 662 | /* For now, echo and test are unconditionally enabled. |
| 663 | * Maybe make it configurable? */ |
| 664 | static const struct built_in_command bltins2[] = { |
| 665 | BLTIN("[" , builtin_test , NULL), |
| 666 | BLTIN("echo" , builtin_echo , NULL), |
| 667 | BLTIN("pwd" , builtin_pwd , NULL), |
| 668 | BLTIN("test" , builtin_test , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 669 | }; |
| 670 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 671 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 672 | /* Debug printouts. |
| 673 | */ |
| 674 | #if HUSH_DEBUG |
| 675 | /* prevent disasters with G.debug_indent < 0 */ |
| 676 | # define indent() fprintf(stderr, "%*s", (G.debug_indent * 2) & 0xff, "") |
| 677 | # define debug_enter() (G.debug_indent++) |
| 678 | # define debug_leave() (G.debug_indent--) |
| 679 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 680 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 681 | # define debug_enter() ((void)0) |
| 682 | # define debug_leave() ((void)0) |
| 683 | #endif |
| 684 | |
| 685 | #ifndef debug_printf |
| 686 | # define debug_printf(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 687 | #endif |
| 688 | |
| 689 | #ifndef debug_printf_parse |
| 690 | # define debug_printf_parse(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 691 | #endif |
| 692 | |
| 693 | #ifndef debug_printf_exec |
| 694 | #define debug_printf_exec(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 695 | #endif |
| 696 | |
| 697 | #ifndef debug_printf_env |
| 698 | # define debug_printf_env(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 699 | #endif |
| 700 | |
| 701 | #ifndef debug_printf_jobs |
| 702 | # define debug_printf_jobs(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 703 | # define DEBUG_JOBS 1 |
| 704 | #else |
| 705 | # define DEBUG_JOBS 0 |
| 706 | #endif |
| 707 | |
| 708 | #ifndef debug_printf_expand |
| 709 | # define debug_printf_expand(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 710 | # define DEBUG_EXPAND 1 |
| 711 | #else |
| 712 | # define DEBUG_EXPAND 0 |
| 713 | #endif |
| 714 | |
| 715 | #ifndef debug_printf_glob |
| 716 | # define debug_printf_glob(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 717 | # define DEBUG_GLOB 1 |
| 718 | #else |
| 719 | # define DEBUG_GLOB 0 |
| 720 | #endif |
| 721 | |
| 722 | #ifndef debug_printf_list |
| 723 | # define debug_printf_list(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 724 | #endif |
| 725 | |
| 726 | #ifndef debug_printf_subst |
| 727 | # define debug_printf_subst(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 728 | #endif |
| 729 | |
| 730 | #ifndef debug_printf_clean |
| 731 | # define debug_printf_clean(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 732 | # define DEBUG_CLEAN 1 |
| 733 | #else |
| 734 | # define DEBUG_CLEAN 0 |
| 735 | #endif |
| 736 | |
| 737 | #if DEBUG_EXPAND |
| 738 | static void debug_print_strings(const char *prefix, char **vv) |
| 739 | { |
| 740 | indent(); |
| 741 | fprintf(stderr, "%s:\n", prefix); |
| 742 | while (*vv) |
| 743 | fprintf(stderr, " '%s'\n", *vv++); |
| 744 | } |
| 745 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 746 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 747 | #endif |
| 748 | |
| 749 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 750 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 751 | */ |
| 752 | #if LEAK_HUNTING |
| 753 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 754 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 755 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 756 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 757 | return ptr; |
| 758 | } |
| 759 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 760 | { |
| 761 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 762 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 763 | return ptr; |
| 764 | } |
| 765 | static char *xxstrdup(int lineno, const char *str) |
| 766 | { |
| 767 | char *ptr = xstrdup(str); |
| 768 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 769 | return ptr; |
| 770 | } |
| 771 | static void xxfree(void *ptr) |
| 772 | { |
| 773 | fdprintf(2, "free %p\n", ptr); |
| 774 | free(ptr); |
| 775 | } |
| 776 | #define xmalloc(s) xxmalloc(__LINE__, s) |
| 777 | #define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 778 | #define xstrdup(s) xxstrdup(__LINE__, s) |
| 779 | #define free(p) xxfree(p) |
| 780 | #endif |
| 781 | |
| 782 | |
| 783 | /* Syntax and runtime errors. They always abort scripts. |
| 784 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 785 | * and return to the prompt. |
| 786 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 787 | */ |
| 788 | #if HUSH_DEBUG < 2 |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 789 | # define die_if_script(lineno, fmt...) die_if_script(fmt) |
| 790 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 791 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 792 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 793 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 794 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 795 | #endif |
| 796 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 797 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 798 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 799 | va_list p; |
| 800 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 801 | #if HUSH_DEBUG >= 2 |
| 802 | bb_error_msg("hush.c:%u", lineno); |
| 803 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 804 | va_start(p, fmt); |
| 805 | bb_verror_msg(fmt, p, NULL); |
| 806 | va_end(p); |
| 807 | if (!G_interactive_fd) |
| 808 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 809 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 810 | |
| 811 | static void syntax_error(unsigned lineno, const char *msg) |
| 812 | { |
| 813 | if (msg) |
| 814 | die_if_script(lineno, "syntax error: %s", msg); |
| 815 | else |
| 816 | die_if_script(lineno, "syntax error", NULL); |
| 817 | } |
| 818 | |
| 819 | static void syntax_error_at(unsigned lineno, const char *msg) |
| 820 | { |
| 821 | die_if_script(lineno, "syntax error at '%s'", msg); |
| 822 | } |
| 823 | |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 824 | static void syntax_error_unterm_str(unsigned lineno, const char *s) |
| 825 | { |
| 826 | die_if_script(lineno, "syntax error: unterminated %s", s); |
| 827 | } |
| 828 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 829 | /* It so happens that all such cases are totally fatal |
| 830 | * even if shell is interactive: EOF while looking for closing |
| 831 | * delimiter. There is nowhere to read stuff from after that, |
| 832 | * it's EOF! The only choice is to terminate. |
| 833 | */ |
| 834 | static void syntax_error_unterm_ch(unsigned lineno, char ch) NORETURN; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 835 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 836 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 837 | char msg[2] = { ch, '\0' }; |
| 838 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 839 | xfunc_die(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 842 | static void syntax_error_unexpected_ch(unsigned lineno, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 843 | { |
| 844 | char msg[2]; |
| 845 | msg[0] = ch; |
| 846 | msg[1] = '\0'; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 847 | die_if_script(lineno, "syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 850 | #if HUSH_DEBUG < 2 |
| 851 | # undef die_if_script |
| 852 | # undef syntax_error |
| 853 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 854 | # undef syntax_error_unterm_ch |
| 855 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 856 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 857 | #else |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 858 | # define die_if_script(fmt...) die_if_script(__LINE__, fmt) |
| 859 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 860 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 861 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 862 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 863 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 864 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 865 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 866 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 867 | #if ENABLE_HUSH_INTERACTIVE |
| 868 | static void cmdedit_update_prompt(void); |
| 869 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 870 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 871 | #endif |
| 872 | |
| 873 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 874 | /* Utility functions |
| 875 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 876 | static int glob_needed(const char *s) |
| 877 | { |
| 878 | while (*s) { |
| 879 | if (*s == '\\') |
| 880 | s++; |
| 881 | if (*s == '*' || *s == '[' || *s == '?') |
| 882 | return 1; |
| 883 | s++; |
| 884 | } |
| 885 | return 0; |
| 886 | } |
| 887 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 888 | static int is_well_formed_var_name(const char *s, char terminator) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 889 | { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 890 | if (!s || !(isalpha(*s) || *s == '_')) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 891 | return 0; |
| 892 | s++; |
| 893 | while (isalnum(*s) || *s == '_') |
| 894 | s++; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 895 | return *s == terminator; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 898 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 899 | static char *unbackslash(char *src) |
| 900 | { |
| 901 | char *dst = src; |
| 902 | while (1) { |
| 903 | if (*src == '\\') |
| 904 | src++; |
| 905 | if ((*dst++ = *src++) == '\0') |
| 906 | break; |
| 907 | } |
| 908 | return dst; |
| 909 | } |
| 910 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 911 | 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] | 912 | { |
| 913 | int i; |
| 914 | unsigned count1; |
| 915 | unsigned count2; |
| 916 | char **v; |
| 917 | |
| 918 | v = strings; |
| 919 | count1 = 0; |
| 920 | if (v) { |
| 921 | while (*v) { |
| 922 | count1++; |
| 923 | v++; |
| 924 | } |
| 925 | } |
| 926 | count2 = 0; |
| 927 | v = add; |
| 928 | while (*v) { |
| 929 | count2++; |
| 930 | v++; |
| 931 | } |
| 932 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 933 | v[count1 + count2] = NULL; |
| 934 | i = count2; |
| 935 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 936 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 937 | return v; |
| 938 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 939 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 940 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 941 | { |
| 942 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 943 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 944 | return ptr; |
| 945 | } |
| 946 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 947 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 948 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 949 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 950 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 951 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 952 | { |
| 953 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 954 | v[0] = add; |
| 955 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 956 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 957 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 958 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 959 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 960 | { |
| 961 | char **ptr = add_string_to_strings(strings, add); |
| 962 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 963 | return ptr; |
| 964 | } |
| 965 | #define add_string_to_strings(strings, add) \ |
| 966 | xx_add_string_to_strings(__LINE__, strings, add) |
| 967 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 968 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 969 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 970 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 971 | char **v; |
| 972 | |
| 973 | if (!strings) |
| 974 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 975 | v = strings; |
| 976 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 977 | free(*v); |
| 978 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 979 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 980 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 983 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 984 | /* Helpers for setting new $n and restoring them back |
| 985 | */ |
| 986 | typedef struct save_arg_t { |
| 987 | char *sv_argv0; |
| 988 | char **sv_g_argv; |
| 989 | int sv_g_argc; |
| 990 | smallint sv_g_malloced; |
| 991 | } save_arg_t; |
| 992 | |
| 993 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 994 | { |
| 995 | int n; |
| 996 | |
| 997 | sv->sv_argv0 = argv[0]; |
| 998 | sv->sv_g_argv = G.global_argv; |
| 999 | sv->sv_g_argc = G.global_argc; |
| 1000 | sv->sv_g_malloced = G.global_args_malloced; |
| 1001 | |
| 1002 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1003 | G.global_argv = argv; |
| 1004 | G.global_args_malloced = 0; |
| 1005 | |
| 1006 | n = 1; |
| 1007 | while (*++argv) |
| 1008 | n++; |
| 1009 | G.global_argc = n; |
| 1010 | } |
| 1011 | |
| 1012 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1013 | { |
| 1014 | char **pp; |
| 1015 | |
| 1016 | if (G.global_args_malloced) { |
| 1017 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 1018 | pp = G.global_argv; |
| 1019 | while (*++pp) /* note: does not free $0 */ |
| 1020 | free(*pp); |
| 1021 | free(G.global_argv); |
| 1022 | } |
| 1023 | argv[0] = sv->sv_argv0; |
| 1024 | G.global_argv = sv->sv_g_argv; |
| 1025 | G.global_argc = sv->sv_g_argc; |
| 1026 | G.global_args_malloced = sv->sv_g_malloced; |
| 1027 | } |
| 1028 | |
| 1029 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1030 | /* Basic theory of signal handling in shell |
| 1031 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1032 | * This does not describe what hush does, rather, it is current understanding |
| 1033 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1034 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1035 | * |
| 1036 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1037 | * is finished or backgrounded. It is the same in interactive and |
| 1038 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1039 | * a user trap handler is installed or a shell special one is in effect. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1040 | * ^C or ^Z from keyboard seem to execute "at once" because it usually |
| 1041 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1042 | * pipe. |
| 1043 | * |
| 1044 | * Wait builtin in interruptible by signals for which user trap is set |
| 1045 | * or by SIGINT in interactive shell. |
| 1046 | * |
| 1047 | * Trap handlers will execute even within trap handlers. (right?) |
| 1048 | * |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1049 | * User trap handlers are forgotten when subshell ("(cmd)") is entered. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1050 | * |
| 1051 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1052 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1053 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1054 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1055 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1056 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1057 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1058 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1059 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1060 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1061 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1062 | * |
| 1063 | * SIGQUIT: ignore |
| 1064 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1065 | * SIGHUP (interactive): |
| 1066 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1067 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1068 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1069 | * that all pipe members are stopped. Try this in bash: |
| 1070 | * while :; do :; done - ^Z does not background it |
| 1071 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1072 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1073 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1074 | * to interactive shell while shell is waiting for a pipe, |
| 1075 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1076 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1077 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1078 | * Example 2: this does not wait and does not execute ls: |
| 1079 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1080 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1081 | * "sleep 5; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1082 | * |
| 1083 | * (What happens to signals which are IGN on shell start?) |
| 1084 | * (What happens with signal mask on shell start?) |
| 1085 | * |
| 1086 | * Implementation in hush |
| 1087 | * ====================== |
| 1088 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1089 | * We block all signals which we don't want to take action immediately, |
| 1090 | * i.e. we block all signals which need to have special handling as described |
| 1091 | * above, and all signals which have traps set. |
| 1092 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1093 | * and act on them. |
| 1094 | * |
| 1095 | * unsigned non_DFL_mask: a mask of such "special" signals |
| 1096 | * sigset_t blocked_set: current blocked signal set |
| 1097 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1098 | * "trap - SIGxxx": |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1099 | * 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] | 1100 | * "trap 'cmd' SIGxxx": |
| 1101 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1102 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1103 | * unblock signals with special interactive handling |
| 1104 | * (child shell is not interactive), |
| 1105 | * unset all traps (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1106 | * after [v]fork, if we plan to exec: |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1107 | * POSIX says pending signal mask is cleared in child - no need to clear it. |
| 1108 | * 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] | 1109 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1110 | * 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] | 1111 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1112 | * and to restore tty pgrp on signal-induced exit. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1113 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1114 | enum { |
| 1115 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1116 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1117 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1118 | | (1 << SIGHUP) |
| 1119 | , |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1120 | SPECIAL_JOB_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1121 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1122 | | (1 << SIGTTIN) |
| 1123 | | (1 << SIGTTOU) |
| 1124 | | (1 << SIGTSTP) |
| 1125 | #endif |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1126 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1127 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1128 | #if ENABLE_HUSH_FAST |
| 1129 | static void SIGCHLD_handler(int sig UNUSED_PARAM) |
| 1130 | { |
| 1131 | G.count_SIGCHLD++; |
| 1132 | //bb_error_msg("[%d] SIGCHLD_handler: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1133 | } |
| 1134 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1135 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1136 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1137 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1138 | /* After [v]fork, in child: do not restore tty pgrp on xfunc death */ |
| 1139 | #define disable_restore_tty_pgrp_on_exit() (die_sleep = 0) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1140 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1141 | #define enable_restore_tty_pgrp_on_exit() (die_sleep = -1) |
| 1142 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1143 | /* Restores tty foreground process group, and exits. |
| 1144 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1145 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1146 | * or called directly with -EXITCODE. |
| 1147 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1148 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1149 | static void sigexit(int sig) |
| 1150 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1151 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 1152 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1153 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1154 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1155 | * tty pgrp then, only top-level shell process does that */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1156 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 1157 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1158 | |
| 1159 | /* Not a signal, just exit */ |
| 1160 | if (sig <= 0) |
| 1161 | _exit(- sig); |
| 1162 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1163 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1164 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1165 | #else |
| 1166 | |
| 1167 | #define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1168 | #define enable_restore_tty_pgrp_on_exit() ((void)0) |
| 1169 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1170 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1171 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1172 | /* Restores tty foreground process group, and exits. */ |
| 1173 | static void hush_exit(int exitcode) NORETURN; |
| 1174 | static void hush_exit(int exitcode) |
| 1175 | { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1176 | if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) { |
| 1177 | /* Prevent recursion: |
| 1178 | * trap "echo Hi; exit" EXIT; exit |
| 1179 | */ |
| 1180 | char *argv[] = { NULL, G.traps[0], NULL }; |
| 1181 | G.traps[0] = NULL; |
| 1182 | G.exiting = 1; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1183 | builtin_eval(argv); |
| 1184 | free(argv[1]); |
| 1185 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1186 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1187 | #if ENABLE_HUSH_JOB |
| 1188 | fflush(NULL); /* flush all streams */ |
| 1189 | sigexit(- (exitcode & 0xff)); |
| 1190 | #else |
| 1191 | exit(exitcode); |
| 1192 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1195 | static int check_and_run_traps(int sig) |
| 1196 | { |
| 1197 | static const struct timespec zero_timespec = { 0, 0 }; |
| 1198 | smalluint save_rcode; |
| 1199 | int last_sig = 0; |
| 1200 | |
| 1201 | if (sig) |
| 1202 | goto jump_in; |
| 1203 | while (1) { |
| 1204 | sig = sigtimedwait(&G.blocked_set, NULL, &zero_timespec); |
| 1205 | if (sig <= 0) |
| 1206 | break; |
| 1207 | jump_in: |
| 1208 | last_sig = sig; |
| 1209 | if (G.traps && G.traps[sig]) { |
| 1210 | if (G.traps[sig][0]) { |
| 1211 | /* We have user-defined handler */ |
| 1212 | char *argv[] = { NULL, xstrdup(G.traps[sig]), NULL }; |
| 1213 | save_rcode = G.last_exitcode; |
| 1214 | builtin_eval(argv); |
| 1215 | free(argv[1]); |
| 1216 | G.last_exitcode = save_rcode; |
| 1217 | } /* else: "" trap, ignoring signal */ |
| 1218 | continue; |
| 1219 | } |
| 1220 | /* not a trap: special action */ |
| 1221 | switch (sig) { |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1222 | #if ENABLE_HUSH_FAST |
| 1223 | case SIGCHLD: |
| 1224 | G.count_SIGCHLD++; |
| 1225 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1226 | break; |
| 1227 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1228 | case SIGINT: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1229 | /* Builtin was ^C'ed, make it look prettier: */ |
| 1230 | bb_putchar('\n'); |
| 1231 | G.flag_SIGINT = 1; |
| 1232 | break; |
| 1233 | #if ENABLE_HUSH_JOB |
| 1234 | case SIGHUP: { |
| 1235 | struct pipe *job; |
| 1236 | /* bash is observed to signal whole process groups, |
| 1237 | * not individual processes */ |
| 1238 | for (job = G.job_list; job; job = job->next) { |
| 1239 | if (job->pgrp <= 0) |
| 1240 | continue; |
| 1241 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 1242 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 1243 | kill(- job->pgrp, SIGCONT); |
| 1244 | } |
| 1245 | sigexit(SIGHUP); |
| 1246 | } |
| 1247 | #endif |
| 1248 | default: /* ignored: */ |
| 1249 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
| 1250 | break; |
| 1251 | } |
| 1252 | } |
| 1253 | return last_sig; |
| 1254 | } |
| 1255 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1256 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1257 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1258 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1259 | if (force || G.cwd == NULL) { |
| 1260 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1261 | * we must not try to free(bb_msg_unknown) */ |
| 1262 | if (G.cwd == bb_msg_unknown) |
| 1263 | G.cwd = NULL; |
| 1264 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1265 | if (!G.cwd) |
| 1266 | G.cwd = bb_msg_unknown; |
| 1267 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1268 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1271 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1272 | /* |
| 1273 | * Shell and environment variable support |
| 1274 | */ |
| 1275 | static struct variable **get_ptr_to_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1276 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1277 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1278 | struct variable *cur; |
| 1279 | int len; |
| 1280 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1281 | len = strlen(name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1282 | pp = &G.top_var; |
| 1283 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1284 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1285 | return pp; |
| 1286 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1287 | } |
| 1288 | return NULL; |
| 1289 | } |
| 1290 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1291 | static struct variable *get_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1292 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1293 | struct variable **pp = get_ptr_to_local_var(name); |
| 1294 | if (pp) |
| 1295 | return *pp; |
| 1296 | return NULL; |
| 1297 | } |
| 1298 | |
| 1299 | static const char *get_local_var_value(const char *name) |
| 1300 | { |
| 1301 | struct variable **pp = get_ptr_to_local_var(name); |
| 1302 | if (pp) |
| 1303 | return strchr((*pp)->varstr, '=') + 1; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1304 | return NULL; |
| 1305 | } |
| 1306 | |
| 1307 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1308 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1309 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1310 | * 0: do not change export flag |
| 1311 | * (if creating new variable, flag will be 0) |
| 1312 | * 1: set export flag and putenv the variable |
| 1313 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1314 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1315 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1316 | #if !BB_MMU && ENABLE_HUSH_LOCAL |
| 1317 | /* all params are used */ |
| 1318 | #elif BB_MMU && ENABLE_HUSH_LOCAL |
| 1319 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1320 | set_local_var(str, flg_export, local_lvl) |
| 1321 | #elif BB_MMU && !ENABLE_HUSH_LOCAL |
| 1322 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1323 | set_local_var(str, flg_export) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1324 | #elif !BB_MMU && !ENABLE_HUSH_LOCAL |
| 1325 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1326 | set_local_var(str, flg_export, flg_read_only) |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1327 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1328 | 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] | 1329 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1330 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1331 | struct variable *cur; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1332 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1333 | int name_len; |
| 1334 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1335 | eq_sign = strchr(str, '='); |
| 1336 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1337 | free(str); |
| 1338 | return -1; |
| 1339 | } |
| 1340 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1341 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1342 | var_pp = &G.top_var; |
| 1343 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1344 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1345 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1346 | continue; |
| 1347 | } |
| 1348 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1349 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1350 | #if !BB_MMU |
| 1351 | if (!flg_read_only) |
| 1352 | #endif |
| 1353 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1354 | free(str); |
| 1355 | return -1; |
| 1356 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1357 | if (flg_export == -1) { // "&& cur->flg_export" ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1358 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1359 | *eq_sign = '\0'; |
| 1360 | unsetenv(str); |
| 1361 | *eq_sign = '='; |
| 1362 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1363 | #if ENABLE_HUSH_LOCAL |
| 1364 | if (cur->func_nest_level < local_lvl) { |
| 1365 | /* New variable is declared as local, |
| 1366 | * and existing one is global, or local |
| 1367 | * from enclosing function. |
| 1368 | * Remove and save old one: */ |
| 1369 | *var_pp = cur->next; |
| 1370 | cur->next = *G.shadowed_vars_pp; |
| 1371 | *G.shadowed_vars_pp = cur; |
| 1372 | /* bash 3.2.33(1) and exported vars: |
| 1373 | * # export z=z |
| 1374 | * # f() { local z=a; env | grep ^z; } |
| 1375 | * # f |
| 1376 | * z=a |
| 1377 | * # env | grep ^z |
| 1378 | * z=z |
| 1379 | */ |
| 1380 | if (cur->flg_export) |
| 1381 | flg_export = 1; |
| 1382 | break; |
| 1383 | } |
| 1384 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1385 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1386 | free_and_exp: |
| 1387 | free(str); |
| 1388 | goto exp; |
| 1389 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1390 | if (cur->max_len != 0) { |
| 1391 | if (cur->max_len >= strlen(str)) { |
| 1392 | /* This one is from startup env, reuse space */ |
| 1393 | strcpy(cur->varstr, str); |
| 1394 | goto free_and_exp; |
| 1395 | } |
| 1396 | } else { |
| 1397 | /* max_len == 0 signifies "malloced" var, which we can |
| 1398 | * (and has to) free */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1399 | free(cur->varstr); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1400 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1401 | cur->max_len = 0; |
| 1402 | goto set_str_and_exp; |
| 1403 | } |
| 1404 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1405 | /* Not found - create new variable struct */ |
| 1406 | cur = xzalloc(sizeof(*cur)); |
| 1407 | #if ENABLE_HUSH_LOCAL |
| 1408 | cur->func_nest_level = local_lvl; |
| 1409 | #endif |
| 1410 | cur->next = *var_pp; |
| 1411 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1412 | |
| 1413 | set_str_and_exp: |
| 1414 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1415 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1416 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1417 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1418 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1419 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1420 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1421 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1422 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1423 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1424 | if (flg_export == -1) { |
| 1425 | cur->flg_export = 0; |
| 1426 | /* unsetenv was already done */ |
| 1427 | } else { |
| 1428 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1429 | return putenv(cur->varstr); |
| 1430 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1431 | } |
| 1432 | return 0; |
| 1433 | } |
| 1434 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1435 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1436 | { |
| 1437 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1438 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1439 | |
| 1440 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1441 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1442 | var_pp = &G.top_var; |
| 1443 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1444 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1445 | if (cur->flg_read_only) { |
| 1446 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1447 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1448 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1449 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1450 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1451 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1452 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1453 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1454 | if (!cur->max_len) |
| 1455 | free(cur->varstr); |
| 1456 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1457 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1458 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1459 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1460 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1461 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1464 | static int unset_local_var(const char *name) |
| 1465 | { |
| 1466 | return unset_local_var_len(name, strlen(name)); |
| 1467 | } |
| 1468 | |
| 1469 | static void unset_vars(char **strings) |
| 1470 | { |
| 1471 | char **v; |
| 1472 | |
| 1473 | if (!strings) |
| 1474 | return; |
| 1475 | v = strings; |
| 1476 | while (*v) { |
| 1477 | const char *eq = strchrnul(*v, '='); |
| 1478 | unset_local_var_len(*v, (int)(eq - *v)); |
| 1479 | v++; |
| 1480 | } |
| 1481 | free(strings); |
| 1482 | } |
| 1483 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1484 | #if ENABLE_SH_MATH_SUPPORT |
| 1485 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1486 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 1487 | static char *endofname(const char *name) |
| 1488 | { |
| 1489 | char *p; |
| 1490 | |
| 1491 | p = (char *) name; |
| 1492 | if (!is_name(*p)) |
| 1493 | return p; |
| 1494 | while (*++p) { |
| 1495 | if (!is_in_name(*p)) |
| 1496 | break; |
| 1497 | } |
| 1498 | return p; |
| 1499 | } |
| 1500 | |
| 1501 | static void arith_set_local_var(const char *name, const char *val, int flags) |
| 1502 | { |
| 1503 | /* arith code doesnt malloc space, so do it for it */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1504 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1505 | set_local_var(var, flags, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1506 | } |
| 1507 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1508 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1509 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1510 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1511 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 1512 | */ |
| 1513 | static void add_vars(struct variable *var) |
| 1514 | { |
| 1515 | struct variable *next; |
| 1516 | |
| 1517 | while (var) { |
| 1518 | next = var->next; |
| 1519 | var->next = G.top_var; |
| 1520 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1521 | if (var->flg_export) { |
| 1522 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1523 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1524 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1525 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1526 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1527 | var = next; |
| 1528 | } |
| 1529 | } |
| 1530 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1531 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1532 | { |
| 1533 | char **s; |
| 1534 | struct variable *old = NULL; |
| 1535 | |
| 1536 | if (!strings) |
| 1537 | return old; |
| 1538 | s = strings; |
| 1539 | while (*s) { |
| 1540 | struct variable *var_p; |
| 1541 | struct variable **var_pp; |
| 1542 | char *eq; |
| 1543 | |
| 1544 | eq = strchr(*s, '='); |
| 1545 | if (eq) { |
| 1546 | *eq = '\0'; |
| 1547 | var_pp = get_ptr_to_local_var(*s); |
| 1548 | *eq = '='; |
| 1549 | if (var_pp) { |
| 1550 | /* Remove variable from global linked list */ |
| 1551 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1552 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1553 | *var_pp = var_p->next; |
| 1554 | /* Add it to returned list */ |
| 1555 | var_p->next = old; |
| 1556 | old = var_p; |
| 1557 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1558 | set_local_var(*s, /*exp:*/ 1, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1559 | } |
| 1560 | s++; |
| 1561 | } |
| 1562 | return old; |
| 1563 | } |
| 1564 | |
| 1565 | |
| 1566 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1567 | * in_str support |
| 1568 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1569 | static int FAST_FUNC static_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1570 | { |
| 1571 | int ch = *i->p++; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1572 | if (ch != '\0') |
| 1573 | return ch; |
| 1574 | i->p--; |
| 1575 | return EOF; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1578 | static int FAST_FUNC static_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1579 | { |
| 1580 | return *i->p; |
| 1581 | } |
| 1582 | |
| 1583 | #if ENABLE_HUSH_INTERACTIVE |
| 1584 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1585 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1586 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1587 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1588 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1589 | if (G.PS1 == NULL) |
| 1590 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1591 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1592 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1593 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1594 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1595 | if (G.PS2 == NULL) |
| 1596 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1597 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1598 | |
| 1599 | static const char* setup_prompt_string(int promptmode) |
| 1600 | { |
| 1601 | const char *prompt_str; |
| 1602 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1603 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1604 | /* Set up the prompt */ |
| 1605 | if (promptmode == 0) { /* PS1 */ |
| 1606 | free((char*)G.PS1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1607 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1608 | prompt_str = G.PS1; |
| 1609 | } else |
| 1610 | prompt_str = G.PS2; |
| 1611 | } else |
| 1612 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1613 | debug_printf("result '%s'\n", prompt_str); |
| 1614 | return prompt_str; |
| 1615 | } |
| 1616 | |
| 1617 | static void get_user_input(struct in_str *i) |
| 1618 | { |
| 1619 | int r; |
| 1620 | const char *prompt_str; |
| 1621 | |
| 1622 | prompt_str = setup_prompt_string(i->promptmode); |
| 1623 | #if ENABLE_FEATURE_EDITING |
| 1624 | /* Enable command line editing only while a command line |
| 1625 | * is actually being read */ |
| 1626 | do { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1627 | G.flag_SIGINT = 0; |
| 1628 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 1629 | * only after <Enter>. (^C will work) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1630 | 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] | 1631 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1632 | check_and_run_traps(0); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1633 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1634 | i->eof_flag = (r < 0); |
| 1635 | if (i->eof_flag) { /* EOF/error detected */ |
| 1636 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1637 | G.user_input_buf[1] = '\0'; |
| 1638 | } |
| 1639 | #else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1640 | do { |
| 1641 | G.flag_SIGINT = 0; |
| 1642 | fputs(prompt_str, stdout); |
| 1643 | fflush(stdout); |
| 1644 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1645 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1646 | //do we need check_and_run_traps(0)? (maybe only if stdin) |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1647 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1648 | i->eof_flag = (r == EOF); |
| 1649 | #endif |
| 1650 | i->p = G.user_input_buf; |
| 1651 | } |
| 1652 | |
| 1653 | #endif /* INTERACTIVE */ |
| 1654 | |
| 1655 | /* This is the magic location that prints prompts |
| 1656 | * and gets data back from the user */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1657 | static int FAST_FUNC file_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1658 | { |
| 1659 | int ch; |
| 1660 | |
| 1661 | /* If there is data waiting, eat it up */ |
| 1662 | if (i->p && *i->p) { |
| 1663 | #if ENABLE_HUSH_INTERACTIVE |
| 1664 | take_cached: |
| 1665 | #endif |
| 1666 | ch = *i->p++; |
| 1667 | if (i->eof_flag && !*i->p) |
| 1668 | ch = EOF; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1669 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1670 | } else { |
| 1671 | /* need to double check i->file because we might be doing something |
| 1672 | * more complicated by now, like sourcing or substituting. */ |
| 1673 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 1674 | if (G_interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1675 | do { |
| 1676 | get_user_input(i); |
| 1677 | } while (!*i->p); /* need non-empty line */ |
| 1678 | i->promptmode = 1; /* PS2 */ |
| 1679 | i->promptme = 0; |
| 1680 | goto take_cached; |
| 1681 | } |
| 1682 | #endif |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1683 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1684 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1685 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1686 | #if ENABLE_HUSH_INTERACTIVE |
| 1687 | if (ch == '\n') |
| 1688 | i->promptme = 1; |
| 1689 | #endif |
| 1690 | return ch; |
| 1691 | } |
| 1692 | |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1693 | /* All callers guarantee this routine will never |
| 1694 | * be used right after a newline, so prompting is not needed. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1695 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1696 | static int FAST_FUNC file_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1697 | { |
| 1698 | int ch; |
| 1699 | if (i->p && *i->p) { |
| 1700 | if (i->eof_flag && !i->p[1]) |
| 1701 | return EOF; |
| 1702 | return *i->p; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1703 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1704 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1705 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1706 | i->eof_flag = (ch == EOF); |
| 1707 | i->peek_buf[0] = ch; |
| 1708 | i->peek_buf[1] = '\0'; |
| 1709 | i->p = i->peek_buf; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1710 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1711 | return ch; |
| 1712 | } |
| 1713 | |
| 1714 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1715 | { |
| 1716 | i->peek = file_peek; |
| 1717 | i->get = file_get; |
| 1718 | #if ENABLE_HUSH_INTERACTIVE |
| 1719 | i->promptme = 1; |
| 1720 | i->promptmode = 0; /* PS1 */ |
| 1721 | #endif |
| 1722 | i->file = f; |
| 1723 | i->p = NULL; |
| 1724 | } |
| 1725 | |
| 1726 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1727 | { |
| 1728 | i->peek = static_peek; |
| 1729 | i->get = static_get; |
| 1730 | #if ENABLE_HUSH_INTERACTIVE |
| 1731 | i->promptme = 1; |
| 1732 | i->promptmode = 0; /* PS1 */ |
| 1733 | #endif |
| 1734 | i->p = s; |
| 1735 | i->eof_flag = 0; |
| 1736 | } |
| 1737 | |
| 1738 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1739 | /* |
| 1740 | * o_string support |
| 1741 | */ |
| 1742 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1743 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1744 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1745 | { |
| 1746 | o->length = 0; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 1747 | o->o_quoted = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1748 | if (o->data) |
| 1749 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1752 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1753 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1754 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1755 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1758 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 1759 | { |
| 1760 | free(o->data); |
| 1761 | } |
| 1762 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1763 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1764 | { |
| 1765 | if (o->length + len > o->maxlen) { |
| 1766 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 1767 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 1768 | } |
| 1769 | } |
| 1770 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1771 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1772 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1773 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 1774 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1775 | o->data[o->length] = ch; |
| 1776 | o->length++; |
| 1777 | o->data[o->length] = '\0'; |
| 1778 | } |
| 1779 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1780 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1781 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1782 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1783 | memcpy(&o->data[o->length], str, len); |
| 1784 | o->length += len; |
| 1785 | o->data[o->length] = '\0'; |
| 1786 | } |
| 1787 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1788 | #if !BB_MMU |
| 1789 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1790 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1791 | o_addblock(o, str, strlen(str)); |
| 1792 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1793 | static void nommu_addchr(o_string *o, int ch) |
| 1794 | { |
| 1795 | if (o) |
| 1796 | o_addchr(o, ch); |
| 1797 | } |
| 1798 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1799 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1800 | #endif |
| 1801 | |
| 1802 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 1803 | { |
| 1804 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1807 | 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] | 1808 | { |
| 1809 | while (len) { |
| 1810 | o_addchr(o, *str); |
| 1811 | if (*str++ == '\\' |
| 1812 | && (*str != '*' && *str != '?' && *str != '[') |
| 1813 | ) { |
| 1814 | o_addchr(o, '\\'); |
| 1815 | } |
| 1816 | len--; |
| 1817 | } |
| 1818 | } |
| 1819 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1820 | /* My analysis of quoting semantics tells me that state information |
| 1821 | * is associated with a destination, not a source. |
| 1822 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1823 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1824 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1825 | int sz = 1; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1826 | char *found = strchr("*?[\\", ch); |
| 1827 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1828 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1829 | o_grow_by(o, sz); |
| 1830 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1831 | o->data[o->length] = '\\'; |
| 1832 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1833 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1834 | o->data[o->length] = ch; |
| 1835 | o->length++; |
| 1836 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1839 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1840 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1841 | int sz = 1; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1842 | if (o->o_escape && strchr("*?[\\", ch)) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1843 | sz++; |
| 1844 | o->data[o->length] = '\\'; |
| 1845 | o->length++; |
| 1846 | } |
| 1847 | o_grow_by(o, sz); |
| 1848 | o->data[o->length] = ch; |
| 1849 | o->length++; |
| 1850 | o->data[o->length] = '\0'; |
| 1851 | } |
| 1852 | |
| 1853 | static void o_addQstr(o_string *o, const char *str, int len) |
| 1854 | { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1855 | if (!o->o_escape) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1856 | o_addblock(o, str, len); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1857 | return; |
| 1858 | } |
| 1859 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1860 | char ch; |
| 1861 | int sz; |
| 1862 | int ordinary_cnt = strcspn(str, "*?[\\"); |
| 1863 | if (ordinary_cnt > len) /* paranoia */ |
| 1864 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1865 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1866 | if (ordinary_cnt == len) |
| 1867 | return; |
| 1868 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1869 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1870 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1871 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1872 | sz = 1; |
| 1873 | if (ch) { /* it is necessarily one of "*?[\\" */ |
| 1874 | sz++; |
| 1875 | o->data[o->length] = '\\'; |
| 1876 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1877 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1878 | o_grow_by(o, sz); |
| 1879 | o->data[o->length] = ch; |
| 1880 | o->length++; |
| 1881 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1882 | } |
| 1883 | } |
| 1884 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1885 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1886 | * 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] | 1887 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1888 | * list[i] contains an INDEX (int!) into this string data. |
| 1889 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1890 | * but list[i]'s need not be modified. |
| 1891 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1892 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1893 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1894 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1895 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1896 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1897 | { |
| 1898 | char **list = (char**)o->data; |
| 1899 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1900 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1901 | |
| 1902 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1903 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1904 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1905 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1906 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1907 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1908 | o->data + (int)list[i] + string_start, |
| 1909 | o->data + (int)list[i] + string_start); |
| 1910 | i++; |
| 1911 | } |
| 1912 | if (n) { |
| 1913 | const char *p = o->data + (int)list[n - 1] + string_start; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1914 | indent(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1915 | 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] | 1916 | } |
| 1917 | } |
| 1918 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1919 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1920 | #endif |
| 1921 | |
| 1922 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1923 | * in list[n] so that it points past last stored byte so far. |
| 1924 | * It returns n+1. */ |
| 1925 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1926 | { |
| 1927 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1928 | int string_start; |
| 1929 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1930 | |
| 1931 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1932 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1933 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1934 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1935 | 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] | 1936 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1937 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1938 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 1939 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1940 | memmove(list + n + 0x10, list + n, string_len); |
| 1941 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1942 | } else { |
| 1943 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 1944 | n, string_len, string_start); |
| 1945 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1946 | } else { |
| 1947 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1948 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1949 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1950 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 1951 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1952 | o->has_empty_slot = 0; |
| 1953 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1954 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1955 | return n + 1; |
| 1956 | } |
| 1957 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1958 | /* "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] | 1959 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1960 | { |
| 1961 | char **list = (char**)o->data; |
| 1962 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1963 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1964 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1967 | /* o_glob performs globbing on last list[], saving each result |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1968 | * as a new list[]. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1969 | static int o_glob(o_string *o, int n) |
| 1970 | { |
| 1971 | glob_t globdata; |
| 1972 | int gr; |
| 1973 | char *pattern; |
| 1974 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1975 | 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] | 1976 | if (!o->data) |
| 1977 | return o_save_ptr_helper(o, n); |
| 1978 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1979 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1980 | if (!glob_needed(pattern)) { |
| 1981 | literal: |
| 1982 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1983 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1984 | return o_save_ptr_helper(o, n); |
| 1985 | } |
| 1986 | |
| 1987 | memset(&globdata, 0, sizeof(globdata)); |
| 1988 | gr = glob(pattern, 0, NULL, &globdata); |
| 1989 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 1990 | if (gr == GLOB_NOSPACE) |
| 1991 | bb_error_msg_and_die("out of memory during glob"); |
| 1992 | if (gr == GLOB_NOMATCH) { |
| 1993 | globfree(&globdata); |
| 1994 | goto literal; |
| 1995 | } |
| 1996 | if (gr != 0) { /* GLOB_ABORTED ? */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 1997 | /* TODO: testcase for bad glob pattern behavior */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1998 | bb_error_msg("glob(3) error %d on '%s'", gr, pattern); |
| 1999 | } |
| 2000 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2001 | char **argv = globdata.gl_pathv; |
| 2002 | o->length = pattern - o->data; /* "forget" pattern */ |
| 2003 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2004 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2005 | n = o_save_ptr_helper(o, n); |
| 2006 | argv++; |
| 2007 | if (!*argv) |
| 2008 | break; |
| 2009 | } |
| 2010 | } |
| 2011 | globfree(&globdata); |
| 2012 | if (DEBUG_GLOB) |
| 2013 | debug_print_list("o_glob returning", o, n); |
| 2014 | return n; |
| 2015 | } |
| 2016 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2017 | /* If o->o_glob == 1, glob the string so far remembered. |
| 2018 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2019 | static int o_save_ptr(o_string *o, int n) |
| 2020 | { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2021 | if (o->o_glob) { /* if globbing is requested */ |
| 2022 | /* If o->has_empty_slot, list[n] was already globbed |
| 2023 | * (if it was requested back then when it was filled) |
| 2024 | * so don't do that again! */ |
| 2025 | if (!o->has_empty_slot) |
| 2026 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 2027 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2028 | return o_save_ptr_helper(o, n); |
| 2029 | } |
| 2030 | |
| 2031 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2032 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2033 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2034 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2035 | int string_start; |
| 2036 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2037 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 2038 | if (DEBUG_EXPAND) |
| 2039 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2040 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2041 | list = (char**)o->data; |
| 2042 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2043 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2044 | while (n) { |
| 2045 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 2046 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2047 | } |
| 2048 | return list; |
| 2049 | } |
| 2050 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2051 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2052 | /* Expansion can recurse */ |
| 2053 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2054 | static int process_command_subs(o_string *dest, const char *s); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2055 | #endif |
| 2056 | static char *expand_string_to_string(const char *str); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2057 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2058 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2059 | parse_stream_dquoted(dest, input, dquote_end) |
| 2060 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2061 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2062 | o_string *dest, |
| 2063 | struct in_str *input, |
| 2064 | int dquote_end); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2065 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2066 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 2067 | * all variable references within and returns a pointer to |
| 2068 | * a list of expanded strings, possibly with larger number |
| 2069 | * of strings. (Think VAR="a b"; echo $VAR). |
| 2070 | * This new list is allocated as a single malloc block. |
| 2071 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 2072 | * followed by strings themself. |
| 2073 | * Caller can deallocate entire list by single free(list). */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2074 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2075 | /* Store given string, finalizing the word and starting new one whenever |
| 2076 | * we encounter IFS char(s). This is used for expanding variable values. |
| 2077 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 2078 | 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] | 2079 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2080 | while (1) { |
| 2081 | int word_len = strcspn(str, G.ifs); |
| 2082 | if (word_len) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2083 | if (output->o_escape || !output->o_glob) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2084 | o_addQstr(output, str, word_len); |
| 2085 | else /* protect backslashes against globbing up :) */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2086 | o_addblock_duplicate_backslash(output, str, word_len); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2087 | str += word_len; |
| 2088 | } |
| 2089 | if (!*str) /* EOL - do not finalize word */ |
| 2090 | break; |
| 2091 | o_addchr(output, '\0'); |
| 2092 | debug_print_list("expand_on_ifs", output, n); |
| 2093 | n = o_save_ptr(output, n); |
| 2094 | str += strspn(str, G.ifs); /* skip ifs chars */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2095 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2096 | debug_print_list("expand_on_ifs[1]", output, n); |
| 2097 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2100 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 2101 | * they are in double quotes, with the exception that they are not :). |
| 2102 | * Just the rules are similar: "expand only $var and `cmd`" |
| 2103 | * |
| 2104 | * Returns malloced string. |
| 2105 | * As an optimization, we return NULL if expansion is not needed. |
| 2106 | */ |
| 2107 | static char *expand_pseudo_dquoted(const char *str) |
| 2108 | { |
| 2109 | char *exp_str; |
| 2110 | struct in_str input; |
| 2111 | o_string dest = NULL_O_STRING; |
| 2112 | |
| 2113 | if (strchr(str, '$') == NULL |
| 2114 | #if ENABLE_HUSH_TICK |
| 2115 | && strchr(str, '`') == NULL |
| 2116 | #endif |
| 2117 | ) { |
| 2118 | return NULL; |
| 2119 | } |
| 2120 | |
| 2121 | /* We need to expand. Example: |
| 2122 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 2123 | */ |
| 2124 | setup_string_in_str(&input, str); |
| 2125 | parse_stream_dquoted(NULL, &dest, &input, EOF); |
| 2126 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
| 2127 | exp_str = expand_string_to_string(dest.data); |
| 2128 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 2129 | o_free_unsafe(&dest); |
| 2130 | return exp_str; |
| 2131 | } |
| 2132 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2133 | /* Expand all variable references in given string, adding words to list[] |
| 2134 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 2135 | * to be filled). This routine is extremely tricky: has to deal with |
| 2136 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 2137 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
| 2138 | 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] | 2139 | { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 2140 | /* or_mask is either 0 (normal case) or 0x80 - |
| 2141 | * expansion of right-hand side of assignment == 1-element expand. |
| 2142 | * It will also do no globbing, and thus we must not backslash-quote! |
| 2143 | */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2144 | char ored_ch; |
| 2145 | char *p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2146 | |
| 2147 | ored_ch = 0; |
| 2148 | |
| 2149 | debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg); |
| 2150 | debug_print_list("expand_vars_to_list", output, n); |
| 2151 | n = o_save_ptr(output, n); |
| 2152 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 2153 | |
| 2154 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2155 | char first_ch; |
| 2156 | int i; |
| 2157 | char *dyn_val = NULL; |
| 2158 | const char *val = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2159 | #if ENABLE_HUSH_TICK |
| 2160 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2161 | #endif |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2162 | #if ENABLE_SH_MATH_SUPPORT |
| 2163 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 2164 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2165 | o_addblock(output, arg, p - arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2166 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 2167 | arg = ++p; |
| 2168 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 2169 | |
| 2170 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
| 2171 | /* "$@" is special. Even if quoted, it can still |
| 2172 | * expand to nothing (not even an empty string) */ |
| 2173 | if ((first_ch & 0x7f) != '@') |
| 2174 | ored_ch |= first_ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2175 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2176 | switch (first_ch & 0x7f) { |
| 2177 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 2178 | case '$': /* pid */ |
| 2179 | val = utoa(G.root_pid); |
| 2180 | break; |
| 2181 | case '!': /* bg pid */ |
| 2182 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
| 2183 | break; |
| 2184 | case '?': /* exitcode */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 2185 | val = utoa(G.last_exitcode); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2186 | break; |
| 2187 | case '#': /* argc */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2188 | if (arg[1] != SPECIAL_VAR_SYMBOL) |
| 2189 | /* actually, it's a ${#var} */ |
| 2190 | goto case_default; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2191 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 2192 | break; |
| 2193 | case '*': |
| 2194 | case '@': |
| 2195 | i = 1; |
| 2196 | if (!G.global_argv[i]) |
| 2197 | break; |
| 2198 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
| 2199 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2200 | smallint sv = output->o_escape; |
| 2201 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2202 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2203 | while (G.global_argv[i]) { |
| 2204 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 2205 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 2206 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 2207 | /* this argv[] is not empty and not last: |
| 2208 | * put terminating NUL, start new word */ |
| 2209 | o_addchr(output, '\0'); |
| 2210 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 2211 | n = o_save_ptr(output, n); |
| 2212 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 2213 | } |
| 2214 | } |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2215 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2216 | } else |
| 2217 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
| 2218 | * and in this case should treat it like '$*' - see 'else...' below */ |
| 2219 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
| 2220 | while (1) { |
| 2221 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2222 | if (++i >= G.global_argc) |
| 2223 | break; |
| 2224 | o_addchr(output, '\0'); |
| 2225 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 2226 | n = o_save_ptr(output, n); |
| 2227 | } |
| 2228 | } else { /* quoted $*: add as one word */ |
| 2229 | while (1) { |
| 2230 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2231 | if (!G.global_argv[++i]) |
| 2232 | break; |
| 2233 | if (G.ifs[0]) |
| 2234 | o_addchr(output, G.ifs[0]); |
| 2235 | } |
| 2236 | } |
| 2237 | break; |
| 2238 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 2239 | /* "Empty variable", used to make "" etc to not disappear */ |
| 2240 | arg++; |
| 2241 | ored_ch = 0x80; |
| 2242 | break; |
| 2243 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2244 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2245 | *p = '\0'; |
| 2246 | arg++; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2247 | /* Can't just stuff it into output o_string, |
| 2248 | * expanded result may need to be globbed |
| 2249 | * and $IFS-splitted */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2250 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2251 | process_command_subs(&subst_result, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2252 | debug_printf_subst("SUBST RES '%s'\n", subst_result.data); |
| 2253 | val = subst_result.data; |
| 2254 | goto store_val; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2255 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2256 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2257 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2258 | arith_eval_hooks_t hooks; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2259 | arith_t res; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2260 | int errcode; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2261 | char *exp_str; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2262 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2263 | arg++; /* skip '+' */ |
| 2264 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2265 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2266 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2267 | exp_str = expand_pseudo_dquoted(arg); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 2268 | hooks.lookupvar = get_local_var_value; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2269 | hooks.setvar = arith_set_local_var; |
| 2270 | hooks.endofname = endofname; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2271 | res = arith(exp_str ? exp_str : arg, &errcode, &hooks); |
| 2272 | free(exp_str); |
| 2273 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2274 | if (errcode < 0) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2275 | const char *msg = "error in arithmetic"; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2276 | switch (errcode) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2277 | case -3: |
| 2278 | msg = "exponent less than 0"; |
| 2279 | break; |
| 2280 | case -2: |
| 2281 | msg = "divide by 0"; |
| 2282 | break; |
| 2283 | case -5: |
| 2284 | msg = "expression recursion loop detected"; |
| 2285 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2286 | } |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2287 | die_if_script(msg); |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2288 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2289 | debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2290 | sprintf(arith_buf, arith_t_fmt, res); |
| 2291 | val = arith_buf; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2292 | break; |
| 2293 | } |
| 2294 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2295 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2296 | case_default: { |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2297 | bool exp_len = false; |
| 2298 | bool exp_null = false; |
| 2299 | char *var = arg; |
| 2300 | char exp_save = exp_save; /* for compiler */ |
| 2301 | char exp_op = exp_op; /* for compiler */ |
| 2302 | char *exp_word = exp_word; /* for compiler */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2303 | size_t exp_off = 0; |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2304 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2305 | *p = '\0'; |
| 2306 | arg[0] = first_ch & 0x7f; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2307 | |
| 2308 | /* prepare for expansions */ |
| 2309 | if (var[0] == '#') { |
| 2310 | /* handle length expansion ${#var} */ |
| 2311 | exp_len = true; |
| 2312 | ++var; |
| 2313 | } else { |
| 2314 | /* maybe handle parameter expansion */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2315 | exp_off = strcspn(var, ":-=+?%#"); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2316 | if (!var[exp_off]) |
| 2317 | exp_off = 0; |
| 2318 | if (exp_off) { |
| 2319 | exp_save = var[exp_off]; |
| 2320 | exp_null = exp_save == ':'; |
| 2321 | exp_word = var + exp_off; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2322 | if (exp_null) |
| 2323 | ++exp_word; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2324 | exp_op = *exp_word++; |
| 2325 | var[exp_off] = '\0'; |
| 2326 | } |
| 2327 | } |
| 2328 | |
| 2329 | /* lookup the variable in question */ |
| 2330 | if (isdigit(var[0])) { |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 2331 | /* handle_dollar() should have vetted var for us */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2332 | i = xatoi_u(var); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2333 | if (i < G.global_argc) |
| 2334 | val = G.global_argv[i]; |
| 2335 | /* else val remains NULL: $N with too big N */ |
| 2336 | } else |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 2337 | val = get_local_var_value(var); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2338 | |
| 2339 | /* handle any expansions */ |
| 2340 | if (exp_len) { |
| 2341 | debug_printf_expand("expand: length of '%s' = ", val); |
| 2342 | val = utoa(val ? strlen(val) : 0); |
| 2343 | debug_printf_expand("%s\n", val); |
| 2344 | } else if (exp_off) { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2345 | if (exp_op == '%' || exp_op == '#') { |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2346 | if (val) { |
| 2347 | /* we need to do a pattern match */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2348 | bool match_at_left; |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2349 | char *loc; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2350 | scan_t scan = pick_scan(exp_op, *exp_word, &match_at_left); |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2351 | if (exp_op == *exp_word) /* ## or %% */ |
| 2352 | ++exp_word; |
| 2353 | val = dyn_val = xstrdup(val); |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2354 | loc = scan(dyn_val, exp_word, match_at_left); |
| 2355 | if (match_at_left) /* # or ## */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2356 | val = loc; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2357 | else if (loc) /* % or %% and match was found */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2358 | *loc = '\0'; |
| 2359 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2360 | } else { |
| 2361 | /* we need to do an expansion */ |
| 2362 | int exp_test = (!val || (exp_null && !val[0])); |
| 2363 | if (exp_op == '+') |
| 2364 | exp_test = !exp_test; |
| 2365 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 2366 | exp_null ? "true" : "false", exp_test); |
| 2367 | if (exp_test) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2368 | if (exp_op == '?') { |
| 2369 | //TODO: how interactive bash aborts expansion mid-command? |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2370 | /* ${var?[error_msg_if_unset]} */ |
| 2371 | /* ${var:?[error_msg_if_unset_or_null]} */ |
| 2372 | /* mimic bash message */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2373 | die_if_script("%s: %s", |
| 2374 | var, |
| 2375 | exp_word[0] ? exp_word : "parameter null or not set" |
| 2376 | ); |
| 2377 | } else { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2378 | val = exp_word; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2379 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2380 | |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2381 | if (exp_op == '=') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2382 | /* ${var=[word]} or ${var:=[word]} */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2383 | if (isdigit(var[0]) || var[0] == '#') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2384 | /* mimic bash message */ |
| 2385 | die_if_script("$%s: cannot assign in this way", var); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2386 | val = NULL; |
| 2387 | } else { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2388 | char *new_var = xasprintf("%s=%s", var, val); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2389 | set_local_var(new_var, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2390 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2391 | } |
| 2392 | } |
| 2393 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2394 | |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2395 | var[exp_off] = exp_save; |
| 2396 | } |
| 2397 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2398 | arg[0] = first_ch; |
| 2399 | #if ENABLE_HUSH_TICK |
| 2400 | store_val: |
| 2401 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2402 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2403 | 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] | 2404 | if (val) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2405 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2406 | smallint sv = output->o_escape; |
| 2407 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2408 | n = expand_on_ifs(output, n, val); |
| 2409 | val = NULL; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2410 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2411 | } |
| 2412 | } else { /* quoted $VAR, val will be appended below */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2413 | 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] | 2414 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2415 | } /* default: */ |
| 2416 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2417 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2418 | if (val) { |
| 2419 | o_addQstr(output, val, strlen(val)); |
| 2420 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2421 | free(dyn_val); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2422 | /* Do the check to avoid writing to a const string */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2423 | if (*p != SPECIAL_VAR_SYMBOL) |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2424 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2425 | |
| 2426 | #if ENABLE_HUSH_TICK |
| 2427 | o_free(&subst_result); |
| 2428 | #endif |
| 2429 | arg = ++p; |
| 2430 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 2431 | |
| 2432 | if (arg[0]) { |
| 2433 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 2434 | /* this part is literal, and it was already pre-quoted |
| 2435 | * if needed (much earlier), do not use o_addQstr here! */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2436 | o_addstr_with_NUL(output, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2437 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 2438 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 2439 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 2440 | ) { |
| 2441 | n--; |
| 2442 | /* allow to reuse list[n] later without re-growth */ |
| 2443 | output->has_empty_slot = 1; |
| 2444 | } else { |
| 2445 | o_addchr(output, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2446 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2447 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2450 | static char **expand_variables(char **argv, int or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2451 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2452 | int n; |
| 2453 | char **list; |
| 2454 | char **v; |
| 2455 | o_string output = NULL_O_STRING; |
| 2456 | |
| 2457 | if (or_mask & 0x100) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2458 | output.o_escape = 1; /* protect against globbing for "$var" */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2459 | /* (unquoted $var will temporarily switch it off) */ |
| 2460 | output.o_glob = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2461 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2462 | |
| 2463 | n = 0; |
| 2464 | v = argv; |
| 2465 | while (*v) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2466 | n = expand_vars_to_list(&output, n, *v, (unsigned char)or_mask); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2467 | v++; |
| 2468 | } |
| 2469 | debug_print_list("expand_variables", &output, n); |
| 2470 | |
| 2471 | /* output.data (malloced in one block) gets returned in "list" */ |
| 2472 | list = o_finalize_list(&output, n); |
| 2473 | debug_print_strings("expand_variables[1]", list); |
| 2474 | return list; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2477 | static char **expand_strvec_to_strvec(char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2478 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2479 | return expand_variables(argv, 0x100); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 2482 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2483 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 2484 | { |
| 2485 | return expand_variables(argv, 0x80); |
| 2486 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 2487 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2488 | |
| 2489 | #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 2490 | static char **expand_strvec_to_strvec_singleword_noglob_cond(char **argv) |
| 2491 | { |
| 2492 | int n; |
| 2493 | char **list; |
| 2494 | char **v; |
| 2495 | o_string output = NULL_O_STRING; |
| 2496 | |
| 2497 | n = 0; |
| 2498 | v = argv; |
| 2499 | while (*v) { |
| 2500 | int is_var = is_well_formed_var_name(*v, '='); |
| 2501 | /* is_var * 0x80: singleword expansion for vars */ |
| 2502 | n = expand_vars_to_list(&output, n, *v, is_var * 0x80); |
| 2503 | |
| 2504 | /* Subtle! expand_vars_to_list did not glob last word yet. |
| 2505 | * It does this only when fed with further data. |
| 2506 | * Therefore we set globbing flags AFTER it, not before: |
| 2507 | */ |
| 2508 | |
| 2509 | /* if it is not recognizably abc=...; then: */ |
| 2510 | output.o_escape = !is_var; /* protect against globbing for "$var" */ |
| 2511 | /* (unquoted $var will temporarily switch it off) */ |
| 2512 | output.o_glob = !is_var; /* and indeed do globbing */ |
| 2513 | v++; |
| 2514 | } |
| 2515 | debug_print_list("expand_cond", &output, n); |
| 2516 | |
| 2517 | /* output.data (malloced in one block) gets returned in "list" */ |
| 2518 | list = o_finalize_list(&output, n); |
| 2519 | debug_print_strings("expand_cond[1]", list); |
| 2520 | return list; |
| 2521 | } |
| 2522 | #endif |
| 2523 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2524 | /* Used for expansion of right hand of assignments */ |
| 2525 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 2526 | * "v=/bin/c*" */ |
| 2527 | static char *expand_string_to_string(const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2528 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2529 | char *argv[2], **list; |
| 2530 | |
| 2531 | argv[0] = (char*)str; |
| 2532 | argv[1] = NULL; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2533 | list = expand_variables(argv, 0x80); /* 0x80: singleword expansion */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2534 | if (HUSH_DEBUG) |
| 2535 | if (!list[0] || list[1]) |
| 2536 | bb_error_msg_and_die("BUG in varexp2"); |
| 2537 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 2538 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 2539 | unbackslash((char*)list); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2540 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 2541 | return (char*)list; |
| 2542 | } |
| 2543 | |
| 2544 | /* Used for "eval" builtin */ |
| 2545 | static char* expand_strvec_to_string(char **argv) |
| 2546 | { |
| 2547 | char **list; |
| 2548 | |
| 2549 | list = expand_variables(argv, 0x80); |
| 2550 | /* Convert all NULs to spaces */ |
| 2551 | if (list[0]) { |
| 2552 | int n = 1; |
| 2553 | while (list[n]) { |
| 2554 | if (HUSH_DEBUG) |
| 2555 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 2556 | bb_error_msg_and_die("BUG in varexp3"); |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 2557 | /* bash uses ' ' regardless of $IFS contents */ |
| 2558 | list[n][-1] = ' '; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2559 | n++; |
| 2560 | } |
| 2561 | } |
| 2562 | overlapping_strcpy((char*)list, list[0]); |
| 2563 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 2564 | return (char*)list; |
| 2565 | } |
| 2566 | |
| 2567 | static char **expand_assignments(char **argv, int count) |
| 2568 | { |
| 2569 | int i; |
| 2570 | char **p = NULL; |
| 2571 | /* Expand assignments into one string each */ |
| 2572 | for (i = 0; i < count; i++) { |
| 2573 | p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
| 2574 | } |
| 2575 | return p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2578 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2579 | #if BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2580 | /* never called */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2581 | void re_execute_shell(char ***to_free, const char *s, |
| 2582 | char *g_argv0, char **g_argv, |
| 2583 | char **builtin_argv) NORETURN; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2584 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2585 | static void reset_traps_to_defaults(void) |
| 2586 | { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2587 | /* This function is always called in a child shell |
| 2588 | * after fork (not vfork, NOMMU doesn't use this function). |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2589 | * Child shells are not interactive. |
| 2590 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 2591 | * Testcase: (while :; do :; done) + ^Z should background. |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2592 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2593 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2594 | unsigned sig; |
| 2595 | unsigned mask; |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2596 | |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2597 | if (!G.traps && !(G.non_DFL_mask & SPECIAL_INTERACTIVE_SIGS)) |
| 2598 | return; |
| 2599 | |
| 2600 | /* Stupid. It can be done with *single* &= op, but we can't use |
| 2601 | * the fact that G.blocked_set is implemented as a bitmask... */ |
| 2602 | mask = (SPECIAL_INTERACTIVE_SIGS >> 1); |
| 2603 | sig = 1; |
| 2604 | while (1) { |
| 2605 | if (mask & 1) |
| 2606 | sigdelset(&G.blocked_set, sig); |
| 2607 | mask >>= 1; |
| 2608 | if (!mask) |
| 2609 | break; |
| 2610 | sig++; |
| 2611 | } |
| 2612 | |
| 2613 | G.non_DFL_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
| 2614 | mask = G.non_DFL_mask; |
| 2615 | if (G.traps) for (sig = 0; sig < NSIG; sig++, mask >>= 1) { |
| 2616 | if (!G.traps[sig]) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2617 | continue; |
| 2618 | free(G.traps[sig]); |
| 2619 | G.traps[sig] = NULL; |
| 2620 | /* There is no signal for 0 (EXIT) */ |
| 2621 | if (sig == 0) |
| 2622 | continue; |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2623 | /* There was a trap handler, we are removing it. |
| 2624 | * But if sig still has non-DFL handling, |
| 2625 | * we should not unblock it. */ |
| 2626 | if (mask & 1) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2627 | continue; |
| 2628 | sigdelset(&G.blocked_set, sig); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2629 | } |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2630 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
| 2633 | #else /* !BB_MMU */ |
| 2634 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2635 | static void re_execute_shell(char ***to_free, const char *s, |
| 2636 | char *g_argv0, char **g_argv, |
| 2637 | char **builtin_argv) NORETURN; |
| 2638 | static void re_execute_shell(char ***to_free, const char *s, |
| 2639 | char *g_argv0, char **g_argv, |
| 2640 | char **builtin_argv) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2641 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2642 | char param_buf[sizeof("-$%x:%x:%x:%x:%x") + sizeof(unsigned) * 2]; |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2643 | char *heredoc_argv[4]; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2644 | struct variable *cur; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2645 | #if ENABLE_HUSH_FUNCTIONS |
| 2646 | struct function *funcp; |
| 2647 | #endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2648 | char **argv, **pp; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2649 | unsigned cnt; |
| 2650 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2651 | if (!g_argv0) { /* heredoc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2652 | argv = heredoc_argv; |
| 2653 | argv[0] = (char *) G.argv0_for_re_execing; |
| 2654 | argv[1] = (char *) "-<"; |
| 2655 | argv[2] = (char *) s; |
| 2656 | argv[3] = NULL; |
Denis Vlasenko | f50caac | 2009-04-09 01:40:15 +0000 | [diff] [blame] | 2657 | pp = &argv[3]; /* used as pointer to empty environment */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2658 | goto do_exec; |
| 2659 | } |
| 2660 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2661 | cnt = 0; |
| 2662 | pp = builtin_argv; |
| 2663 | if (pp) while (*pp++) |
| 2664 | cnt++; |
| 2665 | |
| 2666 | sprintf(param_buf, "-$%x:%x:%x:%x" IF_HUSH_LOOPS(":%x") |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2667 | , (unsigned) G.root_pid |
| 2668 | , (unsigned) G.last_bg_pid |
| 2669 | , (unsigned) G.last_exitcode |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2670 | , cnt |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 2671 | IF_HUSH_LOOPS(, G.depth_of_loop) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2672 | ); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2673 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<depth> <vars...> <funcs...> |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2674 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2675 | */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2676 | cnt += 6; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2677 | for (cur = G.top_var; cur; cur = cur->next) { |
| 2678 | if (!cur->flg_export || cur->flg_read_only) |
| 2679 | cnt += 2; |
| 2680 | } |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2681 | #if ENABLE_HUSH_FUNCTIONS |
| 2682 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 2683 | cnt += 3; |
| 2684 | #endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2685 | pp = g_argv; |
| 2686 | while (*pp++) |
| 2687 | cnt++; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2688 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2689 | *pp++ = (char *) G.argv0_for_re_execing; |
| 2690 | *pp++ = param_buf; |
| 2691 | for (cur = G.top_var; cur; cur = cur->next) { |
| 2692 | if (cur->varstr == hush_version_str) |
| 2693 | continue; |
| 2694 | if (cur->flg_read_only) { |
| 2695 | *pp++ = (char *) "-R"; |
| 2696 | *pp++ = cur->varstr; |
| 2697 | } else if (!cur->flg_export) { |
| 2698 | *pp++ = (char *) "-V"; |
| 2699 | *pp++ = cur->varstr; |
| 2700 | } |
| 2701 | } |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2702 | #if ENABLE_HUSH_FUNCTIONS |
| 2703 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 2704 | *pp++ = (char *) "-F"; |
| 2705 | *pp++ = funcp->name; |
| 2706 | *pp++ = funcp->body_as_string; |
| 2707 | } |
| 2708 | #endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2709 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 2710 | * |
| 2711 | * However, POSIX says that subshells reset signals with traps |
| 2712 | * to SIG_DFL. |
| 2713 | * I tested bash-3.2 and it not only does that with true subshells |
| 2714 | * of the form ( list ), but with any forked children shells. |
| 2715 | * I set trap "echo W" WINCH; and then tried: |
| 2716 | * |
| 2717 | * { echo 1; sleep 20; echo 2; } & |
| 2718 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 2719 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 2720 | * |
| 2721 | * In all these cases sending SIGWINCH to the child shell |
| 2722 | * did not run the trap. If I add trap "echo V" WINCH; |
| 2723 | * _inside_ group (just before echo 1), it works. |
| 2724 | * |
| 2725 | * I conclude it means we don't need to pass active traps here. |
| 2726 | * exec syscall below resets them to SIG_DFL for us. |
| 2727 | */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2728 | *pp++ = (char *) "-c"; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2729 | *pp++ = (char *) s; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2730 | if (builtin_argv) { |
| 2731 | while (*++builtin_argv) |
| 2732 | *pp++ = *builtin_argv; |
| 2733 | *pp++ = (char *) ""; |
| 2734 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2735 | *pp++ = g_argv0; |
| 2736 | while (*g_argv) |
| 2737 | *pp++ = *g_argv++; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2738 | /* *pp = NULL; - is already there */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2739 | pp = environ; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2740 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2741 | do_exec: |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2742 | debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s); |
| 2743 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2744 | execve(bb_busybox_exec_path, argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2745 | /* Fallback. Useful for init=/bin/hush usage etc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2746 | if (argv[0][0] == '/') |
| 2747 | execve(argv[0], argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2748 | xfunc_error_retval = 127; |
| 2749 | bb_error_msg_and_die("can't re-execute the shell"); |
| 2750 | } |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2751 | #endif /* !BB_MMU */ |
| 2752 | |
| 2753 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2754 | static void setup_heredoc(struct redir_struct *redir) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2755 | { |
| 2756 | struct fd_pair pair; |
| 2757 | pid_t pid; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2758 | int len, written; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2759 | /* the _body_ of heredoc (misleading field name) */ |
| 2760 | const char *heredoc = redir->rd_filename; |
| 2761 | char *expanded; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2762 | #if !BB_MMU |
| 2763 | char **to_free; |
| 2764 | #endif |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2765 | |
| 2766 | expanded = NULL; |
| 2767 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
| 2768 | expanded = expand_pseudo_dquoted(heredoc); |
| 2769 | if (expanded) |
| 2770 | heredoc = expanded; |
| 2771 | } |
| 2772 | len = strlen(heredoc); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2773 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2774 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2775 | xpiped_pair(pair); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2776 | xmove_fd(pair.rd, redir->rd_fd); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2777 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2778 | /* Try writing without forking. Newer kernels have |
| 2779 | * dynamically growing pipes. Must use non-blocking write! */ |
| 2780 | ndelay_on(pair.wr); |
| 2781 | while (1) { |
| 2782 | written = write(pair.wr, heredoc, len); |
| 2783 | if (written <= 0) |
| 2784 | break; |
| 2785 | len -= written; |
| 2786 | if (len == 0) { |
| 2787 | close(pair.wr); |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 2788 | free(expanded); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2789 | return; |
| 2790 | } |
| 2791 | heredoc += written; |
| 2792 | } |
| 2793 | ndelay_off(pair.wr); |
| 2794 | |
| 2795 | /* Okay, pipe buffer was not big enough */ |
| 2796 | /* Note: we must not create a stray child (bastard? :) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2797 | * for the unsuspecting parent process. Child creates a grandchild |
| 2798 | * and exits before parent execs the process which consumes heredoc |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2799 | * (that exec happens after we return from this function) */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 2800 | #if !BB_MMU |
| 2801 | to_free = NULL; |
| 2802 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2803 | pid = vfork(); |
| 2804 | if (pid < 0) |
| 2805 | bb_perror_msg_and_die("vfork"); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2806 | if (pid == 0) { |
| 2807 | /* child */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 2808 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2809 | pid = BB_MMU ? fork() : vfork(); |
| 2810 | if (pid < 0) |
| 2811 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
| 2812 | if (pid != 0) |
| 2813 | _exit(0); |
| 2814 | /* grandchild */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2815 | close(redir->rd_fd); /* read side of the pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2816 | #if BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2817 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2818 | _exit(0); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2819 | #else |
| 2820 | /* Delegate blocking writes to another process */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2821 | xmove_fd(pair.wr, STDOUT_FILENO); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2822 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2823 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2824 | } |
| 2825 | /* parent */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 2826 | #if ENABLE_HUSH_FAST |
| 2827 | G.count_SIGCHLD++; |
| 2828 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 2829 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2830 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2831 | #if !BB_MMU |
| 2832 | free(to_free); |
| 2833 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2834 | close(pair.wr); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2835 | free(expanded); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2836 | wait(NULL); /* wait till child has died */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2839 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 2840 | * and stderr if they are redirected. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2841 | static int setup_redirects(struct command *prog, int squirrel[]) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2842 | { |
| 2843 | int openfd, mode; |
| 2844 | struct redir_struct *redir; |
| 2845 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2846 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2847 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2848 | /* rd_fd<<HERE case */ |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 2849 | if (squirrel && redir->rd_fd < 3 |
| 2850 | && squirrel[redir->rd_fd] < 0 |
| 2851 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2852 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2853 | } |
| 2854 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 2855 | * of the heredoc */ |
| 2856 | debug_printf_parse("set heredoc '%s'\n", |
| 2857 | redir->rd_filename); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2858 | setup_heredoc(redir); |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2859 | continue; |
| 2860 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2861 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2862 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
| 2863 | /* rd_fd<*>file case (<*> is <,>,>>,<>) */ |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2864 | char *p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2865 | if (redir->rd_filename == NULL) { |
| 2866 | /* Something went wrong in the parse. |
| 2867 | * Pretend it didn't happen */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2868 | bb_error_msg("bug in redirect parse"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2869 | continue; |
| 2870 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2871 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2872 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2873 | openfd = open_or_warn(p, mode); |
| 2874 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2875 | if (openfd < 0) { |
| 2876 | /* this could get lost if stderr has been redirected, but |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2877 | * bash and ash both lose it as well (though zsh doesn't!) */ |
| 2878 | //what the above comment tries to say? |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2879 | return 1; |
| 2880 | } |
| 2881 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2882 | /* rd_fd<*>rd_dup or rd_fd<*>- cases */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2883 | openfd = redir->rd_dup; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2884 | } |
| 2885 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2886 | if (openfd != redir->rd_fd) { |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 2887 | if (squirrel && redir->rd_fd < 3 |
| 2888 | && squirrel[redir->rd_fd] < 0 |
| 2889 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2890 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2891 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2892 | if (openfd == REDIRFD_CLOSE) { |
| 2893 | /* "n>-" means "close me" */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2894 | close(redir->rd_fd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2895 | } else { |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2896 | xdup2(openfd, redir->rd_fd); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2897 | if (redir->rd_dup == REDIRFD_TO_FILE) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2898 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2899 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2900 | } |
| 2901 | } |
| 2902 | return 0; |
| 2903 | } |
| 2904 | |
| 2905 | static void restore_redirects(int squirrel[]) |
| 2906 | { |
| 2907 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2908 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2909 | fd = squirrel[i]; |
| 2910 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2911 | /* We simply die on error */ |
| 2912 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2913 | } |
| 2914 | } |
| 2915 | } |
| 2916 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2917 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2918 | static void free_pipe_list(struct pipe *head); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2919 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2920 | /* Return code is the exit status of the pipe */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2921 | static void free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2922 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2923 | char **p; |
| 2924 | struct command *command; |
| 2925 | struct redir_struct *r, *rnext; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2926 | int a, i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2927 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2928 | if (pi->stopped_cmds > 0) /* why? */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2929 | return; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2930 | debug_printf_clean("run pipe: (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2931 | for (i = 0; i < pi->num_cmds; i++) { |
| 2932 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2933 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2934 | if (command->argv) { |
| 2935 | for (a = 0, p = command->argv; *p; a++, p++) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2936 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2937 | } |
| 2938 | free_strings(command->argv); |
| 2939 | command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2940 | } |
| 2941 | /* not "else if": on syntax error, we may have both! */ |
| 2942 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2943 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 2944 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2945 | free_pipe_list(command->group); |
| 2946 | debug_printf_clean(" end group\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2947 | command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2948 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 2949 | /* else is crucial here. |
| 2950 | * If group != NULL, child_func is meaningless */ |
| 2951 | #if ENABLE_HUSH_FUNCTIONS |
| 2952 | else if (command->child_func) { |
| 2953 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 2954 | command->child_func->parent_cmd = NULL; |
| 2955 | } |
| 2956 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2957 | #if !BB_MMU |
| 2958 | free(command->group_as_string); |
| 2959 | command->group_as_string = NULL; |
| 2960 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2961 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2962 | debug_printf_clean(" redirect %d%s", |
| 2963 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2964 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 2965 | if (r->rd_filename) { |
| 2966 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 2967 | free(r->rd_filename); |
| 2968 | r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2969 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2970 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2971 | rnext = r->next; |
| 2972 | free(r); |
| 2973 | } |
| 2974 | command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2975 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2976 | free(pi->cmds); /* children are an array, they get freed all at once */ |
| 2977 | pi->cmds = NULL; |
| 2978 | #if ENABLE_HUSH_JOB |
| 2979 | free(pi->cmdtext); |
| 2980 | pi->cmdtext = NULL; |
| 2981 | #endif |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2982 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2983 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2984 | static void free_pipe_list(struct pipe *head) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2985 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2986 | struct pipe *pi, *next; |
| 2987 | |
| 2988 | for (pi = head; pi; pi = next) { |
| 2989 | #if HAS_KEYWORDS |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2990 | debug_printf_clean(" pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2991 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2992 | free_pipe(pi); |
| 2993 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2994 | next = pi->next; |
| 2995 | /*pi->next = NULL;*/ |
| 2996 | free(pi); |
| 2997 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3001 | static int run_list(struct pipe *pi); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3002 | #if BB_MMU |
| 3003 | #define parse_stream(pstring, input, end_trigger) \ |
| 3004 | parse_stream(input, end_trigger) |
| 3005 | #endif |
| 3006 | static struct pipe *parse_stream(char **pstring, |
| 3007 | struct in_str *input, |
| 3008 | int end_trigger); |
| 3009 | static void parse_and_run_string(const char *s); |
| 3010 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3011 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 3012 | static char *find_in_path(const char *arg) |
| 3013 | { |
| 3014 | char *ret = NULL; |
| 3015 | const char *PATH = get_local_var_value("PATH"); |
| 3016 | |
| 3017 | if (!PATH) |
| 3018 | return NULL; |
| 3019 | |
| 3020 | while (1) { |
| 3021 | const char *end = strchrnul(PATH, ':'); |
| 3022 | int sz = end - PATH; /* must be int! */ |
| 3023 | |
| 3024 | free(ret); |
| 3025 | if (sz != 0) { |
| 3026 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 3027 | } else { |
| 3028 | /* We have xxx::yyyy in $PATH, |
| 3029 | * it means "use current dir" */ |
| 3030 | ret = xstrdup(arg); |
| 3031 | } |
| 3032 | if (access(ret, F_OK) == 0) |
| 3033 | break; |
| 3034 | |
| 3035 | if (*end == '\0') { |
| 3036 | free(ret); |
| 3037 | return NULL; |
| 3038 | } |
| 3039 | PATH = end + 1; |
| 3040 | } |
| 3041 | |
| 3042 | return ret; |
| 3043 | } |
| 3044 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3045 | static const struct built_in_command* find_builtin_helper(const char *name, |
| 3046 | const struct built_in_command *x, |
| 3047 | const struct built_in_command *end) |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3048 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3049 | while (x != end) { |
| 3050 | if (strcmp(name, x->cmd) != 0) { |
| 3051 | x++; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3052 | continue; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3053 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3054 | debug_printf_exec("found builtin '%s'\n", name); |
| 3055 | return x; |
| 3056 | } |
| 3057 | return NULL; |
| 3058 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3059 | static const struct built_in_command* find_builtin1(const char *name) |
| 3060 | { |
| 3061 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 3062 | } |
| 3063 | static const struct built_in_command* find_builtin(const char *name) |
| 3064 | { |
| 3065 | const struct built_in_command *x = find_builtin1(name); |
| 3066 | if (x) |
| 3067 | return x; |
| 3068 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 3069 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3070 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3071 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3072 | static const struct function *find_function(const char *name) |
| 3073 | { |
| 3074 | const struct function *funcp = G.top_func; |
| 3075 | while (funcp) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3076 | if (strcmp(name, funcp->name) == 0) { |
| 3077 | break; |
| 3078 | } |
| 3079 | funcp = funcp->next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3080 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3081 | if (funcp) |
| 3082 | debug_printf_exec("found function '%s'\n", name); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3083 | return funcp; |
| 3084 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3085 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3086 | /* Note: takes ownership on name ptr */ |
| 3087 | static struct function *new_function(char *name) |
| 3088 | { |
| 3089 | struct function *funcp; |
| 3090 | struct function **funcpp = &G.top_func; |
| 3091 | |
| 3092 | while ((funcp = *funcpp) != NULL) { |
| 3093 | struct command *cmd; |
| 3094 | |
| 3095 | if (strcmp(funcp->name, name) != 0) { |
| 3096 | funcpp = &funcp->next; |
| 3097 | continue; |
| 3098 | } |
| 3099 | |
| 3100 | cmd = funcp->parent_cmd; |
| 3101 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 3102 | if (!cmd) { |
| 3103 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 3104 | free(funcp->name); |
| 3105 | /* Note: if !funcp->body, do not free body_as_string! |
| 3106 | * This is a special case of "-F name body" function: |
| 3107 | * body_as_string was not malloced! */ |
| 3108 | if (funcp->body) { |
| 3109 | free_pipe_list(funcp->body); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3110 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3111 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3112 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3113 | } |
| 3114 | } else { |
| 3115 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 3116 | cmd->argv[0] = funcp->name; |
| 3117 | cmd->group = funcp->body; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3118 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3119 | cmd->group_as_string = funcp->body_as_string; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3120 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3121 | } |
| 3122 | goto skip; |
| 3123 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3124 | debug_printf_exec("remembering new function '%s'\n", name); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3125 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 3126 | /*funcp->next = NULL;*/ |
| 3127 | skip: |
| 3128 | funcp->name = name; |
| 3129 | return funcp; |
| 3130 | } |
| 3131 | |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3132 | static void unset_func(const char *name) |
| 3133 | { |
| 3134 | struct function *funcp; |
| 3135 | struct function **funcpp = &G.top_func; |
| 3136 | |
| 3137 | while ((funcp = *funcpp) != NULL) { |
| 3138 | if (strcmp(funcp->name, name) == 0) { |
| 3139 | *funcpp = funcp->next; |
root | 6245202 | 2009-05-04 12:00:19 +0200 | [diff] [blame] | 3140 | /* funcp is unlinked now, deleting it. |
| 3141 | * Note: if !funcp->body, the function was created by |
| 3142 | * "-F name body", do not free ->body_as_string |
| 3143 | * and ->name as they were not malloced. */ |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3144 | if (funcp->body) { |
| 3145 | free_pipe_list(funcp->body); |
root | 6245202 | 2009-05-04 12:00:19 +0200 | [diff] [blame] | 3146 | free(funcp->name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3147 | # if !BB_MMU |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3148 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3149 | # endif |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3150 | } |
| 3151 | free(funcp); |
| 3152 | break; |
| 3153 | } |
Denis Vlasenko | 7301067 | 2009-04-18 11:25:18 +0000 | [diff] [blame] | 3154 | funcpp = &funcp->next; |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3155 | } |
| 3156 | } |
| 3157 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3158 | # if BB_MMU |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3159 | #define exec_function(to_free, funcp, argv) \ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3160 | exec_function(funcp, argv) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3161 | # endif |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3162 | static void exec_function(char ***to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3163 | const struct function *funcp, |
| 3164 | char **argv) NORETURN; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3165 | static void exec_function(char ***to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3166 | const struct function *funcp, |
| 3167 | char **argv) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3168 | { |
| 3169 | # if BB_MMU |
| 3170 | int n = 1; |
| 3171 | |
| 3172 | argv[0] = G.global_argv[0]; |
| 3173 | G.global_argv = argv; |
| 3174 | while (*++argv) |
| 3175 | n++; |
| 3176 | G.global_argc = n; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3177 | /* On MMU, funcp->body is always non-NULL */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3178 | n = run_list(funcp->body); |
| 3179 | fflush(NULL); |
| 3180 | _exit(n); |
| 3181 | # else |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3182 | re_execute_shell(to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3183 | funcp->body_as_string, |
| 3184 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3185 | argv + 1, |
| 3186 | NULL); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3187 | # endif |
| 3188 | } |
| 3189 | |
| 3190 | static int run_function(const struct function *funcp, char **argv) |
| 3191 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3192 | int rc; |
| 3193 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 3194 | smallint sv_flg; |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3195 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3196 | save_and_replace_G_args(&sv, argv); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3197 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3198 | /* "we are in function, ok to use return" */ |
| 3199 | sv_flg = G.flag_return_in_progress; |
| 3200 | G.flag_return_in_progress = -1; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3201 | #if ENABLE_HUSH_LOCAL |
| 3202 | G.func_nest_level++; |
| 3203 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3204 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3205 | /* On MMU, funcp->body is always non-NULL */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3206 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3207 | if (!funcp->body) { |
| 3208 | /* Function defined by -F */ |
| 3209 | parse_and_run_string(funcp->body_as_string); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3210 | rc = G.last_exitcode; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3211 | } else |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3212 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3213 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3214 | rc = run_list(funcp->body); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3215 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3216 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3217 | #if ENABLE_HUSH_LOCAL |
| 3218 | { |
| 3219 | struct variable *var; |
| 3220 | struct variable **var_pp; |
| 3221 | |
| 3222 | var_pp = &G.top_var; |
| 3223 | while ((var = *var_pp) != NULL) { |
| 3224 | if (var->func_nest_level < G.func_nest_level) { |
| 3225 | var_pp = &var->next; |
| 3226 | continue; |
| 3227 | } |
| 3228 | /* Unexport */ |
| 3229 | if (var->flg_export) |
| 3230 | bb_unsetenv(var->varstr); |
| 3231 | /* Remove from global list */ |
| 3232 | *var_pp = var->next; |
| 3233 | /* Free */ |
| 3234 | if (!var->max_len) |
| 3235 | free(var->varstr); |
| 3236 | free(var); |
| 3237 | } |
| 3238 | G.func_nest_level--; |
| 3239 | } |
| 3240 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3241 | G.flag_return_in_progress = sv_flg; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3242 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3243 | restore_G_args(&sv, argv); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3244 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3245 | return rc; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3246 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3247 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3248 | |
| 3249 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3250 | # if BB_MMU |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3251 | #define exec_builtin(to_free, x, argv) \ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3252 | exec_builtin(x, argv) |
| 3253 | # else |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3254 | #define exec_builtin(to_free, x, argv) \ |
| 3255 | exec_builtin(to_free, argv) |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3256 | # endif |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3257 | static void exec_builtin(char ***to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3258 | const struct built_in_command *x, |
| 3259 | char **argv) NORETURN; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3260 | static void exec_builtin(char ***to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3261 | const struct built_in_command *x, |
| 3262 | char **argv) |
| 3263 | { |
| 3264 | # if BB_MMU |
| 3265 | int rcode = x->function(argv); |
| 3266 | fflush(NULL); |
| 3267 | _exit(rcode); |
| 3268 | # else |
| 3269 | /* On NOMMU, we must never block! |
| 3270 | * Example: { sleep 99 | read line; } & echo Ok |
| 3271 | */ |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3272 | re_execute_shell(to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3273 | argv[0], |
| 3274 | G.global_argv[0], |
| 3275 | G.global_argv + 1, |
| 3276 | argv); |
| 3277 | # endif |
| 3278 | } |
| 3279 | |
| 3280 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 3281 | #if BB_MMU |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3282 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 3283 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 3284 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 3285 | pseudo_exec(command, argv_expanded) |
| 3286 | #endif |
| 3287 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3288 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3289 | * Never returns. |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3290 | * Don't exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3291 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3292 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3293 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 3294 | char **argv, int assignment_cnt, |
| 3295 | char **argv_expanded) NORETURN; |
| 3296 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 3297 | char **argv, int assignment_cnt, |
| 3298 | char **argv_expanded) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3299 | { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3300 | char **new_env; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3301 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3302 | /* Case when we are here: ... | var=val | ... */ |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3303 | if (!argv[assignment_cnt]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3304 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3305 | |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 3306 | new_env = expand_assignments(argv, assignment_cnt); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3307 | #if BB_MMU |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3308 | set_vars_and_save_old(new_env); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3309 | free(new_env); /* optional */ |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3310 | /* we can also destroy set_vars_and_save_old's return value, |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3311 | * to save memory */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3312 | #else |
| 3313 | nommu_save->new_env = new_env; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3314 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3315 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3316 | if (argv_expanded) { |
| 3317 | argv = argv_expanded; |
| 3318 | } else { |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 3319 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3320 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3321 | nommu_save->argv = argv; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3322 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3323 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 3324 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3325 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 3326 | if (strchr(argv[0], '/') != NULL) |
| 3327 | goto skip; |
| 3328 | #endif |
| 3329 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3330 | /* Check if the command matches any of the builtins. |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3331 | * Depending on context, this might be redundant. But it's |
| 3332 | * easier to waste a few CPU cycles than it is to figure out |
| 3333 | * if this is one of those cases. |
| 3334 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3335 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3336 | /* On NOMMU, it is more expensive to re-execute shell |
| 3337 | * just in order to run echo or test builtin. |
| 3338 | * It's better to skip it here and run corresponding |
| 3339 | * non-builtin later. */ |
| 3340 | const struct built_in_command *x; |
| 3341 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3342 | if (x) { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3343 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3344 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3345 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3346 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3347 | /* Check if the command matches any functions */ |
| 3348 | { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3349 | const struct function *funcp = find_function(argv[0]); |
| 3350 | if (funcp) { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3351 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3352 | } |
| 3353 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3354 | #endif |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3355 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 3356 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3357 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3358 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3359 | int a = find_applet_by_name(argv[0]); |
| 3360 | if (a >= 0) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3361 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3362 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3363 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3364 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3365 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3366 | # endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3367 | /* Re-exec ourselves */ |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3368 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3369 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 3370 | execv(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3371 | /* If they called chroot or otherwise made the binary no longer |
| 3372 | * executable, fall through */ |
| 3373 | } |
| 3374 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 3375 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3376 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3377 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3378 | skip: |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3379 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3380 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3381 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3382 | execvp(argv[0], argv); |
Denis Vlasenko | f9d4fc3 | 2009-04-21 20:40:51 +0000 | [diff] [blame] | 3383 | bb_perror_msg("can't execute '%s'", argv[0]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 3384 | _exit(EXIT_FAILURE); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3385 | } |
| 3386 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3387 | /* Called after [v]fork() in run_pipe |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3388 | */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3389 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3390 | struct command *command, |
| 3391 | char **argv_expanded) NORETURN; |
| 3392 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3393 | struct command *command, |
| 3394 | char **argv_expanded) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3395 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3396 | if (command->argv) { |
| 3397 | pseudo_exec_argv(nommu_save, command->argv, |
| 3398 | command->assignment_cnt, argv_expanded); |
| 3399 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3400 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3401 | if (command->group) { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3402 | /* Cases when we are here: |
| 3403 | * ( list ) |
| 3404 | * { list } & |
| 3405 | * ... | ( list ) | ... |
| 3406 | * ... | { list } | ... |
| 3407 | */ |
| 3408 | #if BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 3409 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3410 | debug_printf_exec("pseudo_exec: run_list\n"); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3411 | reset_traps_to_defaults(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3412 | rcode = run_list(command->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 3413 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3414 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3415 | _exit(rcode); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3416 | #else |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3417 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 3418 | command->group_as_string, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3419 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3420 | G.global_argv + 1, |
| 3421 | NULL); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3422 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3423 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3424 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3425 | /* Case when we are here: ... | >file */ |
| 3426 | debug_printf_exec("pseudo_exec'ed null command\n"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3427 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3430 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3431 | static const char *get_cmdtext(struct pipe *pi) |
| 3432 | { |
| 3433 | char **argv; |
| 3434 | char *p; |
| 3435 | int len; |
| 3436 | |
| 3437 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 3438 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3439 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3440 | if (pi->cmdtext) |
| 3441 | return pi->cmdtext; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3442 | argv = pi->cmds[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3443 | if (!argv || !argv[0]) { |
| 3444 | pi->cmdtext = xzalloc(1); |
| 3445 | return pi->cmdtext; |
| 3446 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3447 | |
| 3448 | len = 0; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3449 | do { |
| 3450 | len += strlen(*argv) + 1; |
| 3451 | } while (*++argv); |
| 3452 | p = xmalloc(len); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3453 | pi->cmdtext = p; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3454 | argv = pi->cmds[0].argv; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3455 | do { |
| 3456 | len = strlen(*argv); |
| 3457 | memcpy(p, *argv, len); |
| 3458 | p += len; |
| 3459 | *p++ = ' '; |
| 3460 | } while (*++argv); |
| 3461 | p[-1] = '\0'; |
| 3462 | return pi->cmdtext; |
| 3463 | } |
| 3464 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3465 | static void insert_bg_job(struct pipe *pi) |
| 3466 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3467 | struct pipe *job, **jobp; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3468 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3469 | |
| 3470 | /* Linear search for the ID of the job to use */ |
| 3471 | pi->jobid = 1; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3472 | for (job = G.job_list; job; job = job->next) |
| 3473 | if (job->jobid >= pi->jobid) |
| 3474 | pi->jobid = job->jobid + 1; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3475 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3476 | /* Add job to the list of running jobs */ |
| 3477 | jobp = &G.job_list; |
| 3478 | while ((job = *jobp) != NULL) |
| 3479 | jobp = &job->next; |
| 3480 | job = *jobp = xmalloc(sizeof(*job)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3481 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3482 | *job = *pi; /* physical copy */ |
| 3483 | job->next = NULL; |
| 3484 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 3485 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3486 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3487 | job->cmds[i].pid = pi->cmds[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3488 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3489 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3490 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3491 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3492 | if (G_interactive_fd) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3493 | printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext); |
| 3494 | /* Last command's pid goes to $! */ |
| 3495 | G.last_bg_pid = job->cmds[job->num_cmds - 1].pid; |
| 3496 | G.last_jobid = job->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3497 | } |
| 3498 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3499 | static void remove_bg_job(struct pipe *pi) |
| 3500 | { |
| 3501 | struct pipe *prev_pipe; |
| 3502 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3503 | if (pi == G.job_list) { |
| 3504 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3505 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3506 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3507 | while (prev_pipe->next != pi) |
| 3508 | prev_pipe = prev_pipe->next; |
| 3509 | prev_pipe->next = pi->next; |
| 3510 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3511 | if (G.job_list) |
| 3512 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3513 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3514 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3515 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3516 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3517 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3518 | static void delete_finished_bg_job(struct pipe *pi) |
| 3519 | { |
| 3520 | remove_bg_job(pi); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3521 | pi->stopped_cmds = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3522 | free_pipe(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3523 | free(pi); |
| 3524 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3525 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3526 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3527 | /* Check to see if any processes have exited -- if they |
| 3528 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3529 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3530 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3531 | int attributes; |
| 3532 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3533 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3534 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3535 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3536 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3537 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3538 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3539 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 3540 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3541 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3542 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3543 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3544 | |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3545 | errno = 0; |
| 3546 | #if ENABLE_HUSH_FAST |
| 3547 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 3548 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 3549 | //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] | 3550 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 3551 | /* Avoid doing waitpid syscall if possible */ |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3552 | if (!G.we_have_children) { |
| 3553 | errno = ECHILD; |
| 3554 | return -1; |
| 3555 | } |
| 3556 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 3557 | /* We have children, but they did not exit |
| 3558 | * or stop yet (we saw no SIGCHLD) */ |
| 3559 | return 0; |
| 3560 | } |
| 3561 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 3562 | } |
| 3563 | #endif |
| 3564 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3565 | /* Do we do this right? |
| 3566 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3567 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3568 | * [3]+ Stopped sleep 20 | false |
| 3569 | * bash-3.00# echo $? |
| 3570 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3571 | * [hush 1.14.0: yes we do it right] |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3572 | */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3573 | wait_more: |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3574 | while (1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3575 | int i; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3576 | int dead; |
| 3577 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3578 | #if ENABLE_HUSH_FAST |
| 3579 | i = G.count_SIGCHLD; |
| 3580 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3581 | childpid = waitpid(-1, &status, attributes); |
| 3582 | if (childpid <= 0) { |
| 3583 | if (childpid && errno != ECHILD) |
| 3584 | bb_perror_msg("waitpid"); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3585 | #if ENABLE_HUSH_FAST |
| 3586 | else { /* Until next SIGCHLD, waitpid's are useless */ |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3587 | G.we_have_children = (childpid == 0); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3588 | G.handled_SIGCHLD = i; |
| 3589 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 3590 | } |
| 3591 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3592 | break; |
| 3593 | } |
| 3594 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 3595 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3596 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3597 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3598 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3599 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 3600 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3601 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3602 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 3603 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3604 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3605 | childpid, WEXITSTATUS(status)); |
| 3606 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3607 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3608 | if (fg_pipe) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3609 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 3610 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 3611 | if (fg_pipe->cmds[i].pid != childpid) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3612 | continue; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3613 | if (dead) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3614 | fg_pipe->cmds[i].pid = 0; |
| 3615 | fg_pipe->alive_cmds--; |
| 3616 | if (i == fg_pipe->num_cmds - 1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3617 | /* last process gives overall exitstatus */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3618 | /* Note: is WIFSIGNALED, WEXITSTATUS = sig + 128 */ |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3619 | rcode = WEXITSTATUS(status); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3620 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3621 | /* bash prints killing signal's name for *last* |
| 3622 | * process in pipe (prints just newline for SIGINT). |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3623 | * Mimic this. Example: "sleep 5" + ^\ |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3624 | */ |
| 3625 | if (WIFSIGNALED(status)) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3626 | int sig = WTERMSIG(status); |
| 3627 | printf("%s\n", sig == SIGINT ? "" : get_signame(sig)); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3628 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3629 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3630 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3631 | fg_pipe->cmds[i].is_stopped = 1; |
| 3632 | fg_pipe->stopped_cmds++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3633 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3634 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 3635 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 3636 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3637 | /* All processes in fg pipe have exited or stopped */ |
| 3638 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 3639 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 3640 | * and "killall -STOP cat" */ |
| 3641 | if (G_interactive_fd) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3642 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3643 | if (fg_pipe->alive_cmds) |
| 3644 | insert_bg_job(fg_pipe); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3645 | #endif |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3646 | return rcode; |
| 3647 | } |
Denys Vlasenko | 6f22624 | 2009-06-03 14:37:30 +0200 | [diff] [blame] | 3648 | if (!fg_pipe->alive_cmds) |
| 3649 | return rcode; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3650 | } |
| 3651 | /* There are still running processes in the fg pipe */ |
| 3652 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3653 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3654 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3657 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3658 | /* We asked to wait for bg or orphaned children */ |
| 3659 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3660 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3661 | for (i = 0; i < pi->num_cmds; i++) { |
| 3662 | if (pi->cmds[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3663 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3664 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3665 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3666 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 3667 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3668 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 3669 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3670 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3671 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3672 | /* child exited */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3673 | pi->cmds[i].pid = 0; |
| 3674 | pi->alive_cmds--; |
| 3675 | if (!pi->alive_cmds) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3676 | if (G_interactive_fd) |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 3677 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 3678 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3679 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3680 | } |
| 3681 | } else { |
| 3682 | /* child stopped */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3683 | pi->cmds[i].is_stopped = 1; |
| 3684 | pi->stopped_cmds++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3685 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3686 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3687 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3688 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3689 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3692 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3693 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 3694 | { |
| 3695 | pid_t p; |
| 3696 | int rcode = checkjobs(fg_pipe); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 3697 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3698 | /* Job finished, move the shell to the foreground */ |
| 3699 | p = getpgrp(); /* our process group id */ |
| 3700 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 3701 | tcsetpgrp(G_interactive_fd, p); |
| 3702 | } |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3703 | return rcode; |
| 3704 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3705 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3706 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3707 | /* Start all the jobs, but don't wait for anything to finish. |
| 3708 | * See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3709 | * |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3710 | * 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] | 3711 | * to finish to determine the exit status of the pipe. If the pipe |
| 3712 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3713 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3714 | * return value. |
| 3715 | * |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3716 | * Returns -1 only if started some children. IOW: we have to |
| 3717 | * mask out retvals of builtins etc with 0xff! |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3718 | * |
| 3719 | * The only case when we do not need to [v]fork is when the pipe |
| 3720 | * is single, non-backgrounded, non-subshell command. Examples: |
| 3721 | * cmd ; ... { list } ; ... |
| 3722 | * cmd && ... { list } && ... |
| 3723 | * cmd || ... { list } || ... |
| 3724 | * If it is, then we can run cmd as a builtin, NOFORK [do we do this?], |
| 3725 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3726 | * 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] | 3727 | * |
| 3728 | * Cases when we must fork: |
| 3729 | * non-single: cmd | cmd |
| 3730 | * backgrounded: cmd & { list } & |
| 3731 | * subshell: ( list ) [&] |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3732 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3733 | static int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3734 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3735 | static const char *const null_ptr = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3736 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3737 | int nextin; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3738 | struct command *command; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3739 | char **argv_expanded; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3740 | char **argv; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3741 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3742 | /* it is not always needed, but we aim to smaller code */ |
| 3743 | int squirrel[] = { -1, -1, -1 }; |
| 3744 | int rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3745 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3746 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3747 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 3748 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 3749 | IF_HUSH_JOB(pi->pgrp = -1;) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3750 | pi->stopped_cmds = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3751 | command = &(pi->cmds[0]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3752 | argv_expanded = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3753 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3754 | if (pi->num_cmds != 1 |
| 3755 | || pi->followup == PIPE_BG |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3756 | || command->cmd_type == CMD_SUBSHELL |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3757 | ) { |
| 3758 | goto must_fork; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3759 | } |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3760 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3761 | pi->alive_cmds = 1; |
| 3762 | |
| 3763 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 3764 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 3765 | |
| 3766 | if (command->group) { |
| 3767 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3768 | if (command->cmd_type == CMD_FUNCDEF) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3769 | /* "executing" func () { list } */ |
| 3770 | struct function *funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3771 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3772 | funcp = new_function(command->argv[0]); |
| 3773 | /* funcp->name is already set to argv[0] */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3774 | funcp->body = command->group; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3775 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3776 | funcp->body_as_string = command->group_as_string; |
| 3777 | command->group_as_string = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3778 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3779 | command->group = NULL; |
| 3780 | command->argv[0] = NULL; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3781 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 3782 | funcp->parent_cmd = command; |
| 3783 | command->child_func = funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3784 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3785 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 3786 | debug_leave(); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3787 | return EXIT_SUCCESS; |
| 3788 | } |
| 3789 | #endif |
| 3790 | /* { list } */ |
| 3791 | debug_printf("non-subshell group\n"); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3792 | rcode = 1; /* exitcode if redir failed */ |
| 3793 | if (setup_redirects(command, squirrel) == 0) { |
| 3794 | debug_printf_exec(": run_list\n"); |
| 3795 | rcode = run_list(command->group) & 0xff; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3796 | } |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 3797 | restore_redirects(squirrel); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3798 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3799 | debug_leave(); |
| 3800 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3801 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3802 | } |
| 3803 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3804 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 3805 | { |
| 3806 | const struct built_in_command *x; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3807 | #if ENABLE_HUSH_FUNCTIONS |
| 3808 | const struct function *funcp; |
| 3809 | #else |
| 3810 | enum { funcp = 0 }; |
| 3811 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3812 | char **new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3813 | struct variable *old_vars = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3814 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3815 | if (argv[command->assignment_cnt] == NULL) { |
| 3816 | /* Assignments, but no command */ |
| 3817 | /* Ensure redirects take effect. Try "a=t >file" */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3818 | rcode = setup_redirects(command, squirrel); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3819 | restore_redirects(squirrel); |
| 3820 | /* Set shell variables */ |
| 3821 | while (*argv) { |
| 3822 | p = expand_string_to_string(*argv); |
| 3823 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 3824 | *argv, p); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3825 | set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3826 | argv++; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3827 | } |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3828 | /* Do we need to flag set_local_var() errors? |
| 3829 | * "assignment to readonly var" and "putenv error" |
| 3830 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3831 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3832 | debug_leave(); |
| 3833 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3834 | return rcode; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3835 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3836 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3837 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3838 | if (0) {} |
| 3839 | #if ENABLE_HUSH_BASH_COMPAT |
| 3840 | else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3841 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
| 3842 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3843 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3844 | #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 3845 | else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB_COND) { |
| 3846 | argv_expanded = expand_strvec_to_strvec_singleword_noglob_cond(argv + command->assignment_cnt); |
| 3847 | |
| 3848 | } |
| 3849 | #endif |
| 3850 | else { |
| 3851 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
| 3852 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3853 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3854 | x = find_builtin(argv_expanded[0]); |
| 3855 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3856 | funcp = NULL; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3857 | if (!x) |
| 3858 | funcp = find_function(argv_expanded[0]); |
| 3859 | #endif |
| 3860 | if (x || funcp) { |
| 3861 | if (!funcp) { |
| 3862 | if (x->function == builtin_exec && argv_expanded[1] == NULL) { |
| 3863 | debug_printf("exec with redirects only\n"); |
| 3864 | rcode = setup_redirects(command, NULL); |
| 3865 | goto clean_up_and_ret1; |
| 3866 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3867 | } |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3868 | /* setup_redirects acts on file descriptors, not FILEs. |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3869 | * This is perfect for work that comes after exec(). |
| 3870 | * Is it really safe for inline use? Experimentally, |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3871 | * things seem to work. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3872 | rcode = setup_redirects(command, squirrel); |
| 3873 | if (rcode == 0) { |
| 3874 | new_env = expand_assignments(argv, command->assignment_cnt); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3875 | old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3876 | if (!funcp) { |
| 3877 | debug_printf_exec(": builtin '%s' '%s'...\n", |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3878 | x->cmd, argv_expanded[1]); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3879 | rcode = x->function(argv_expanded) & 0xff; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3880 | fflush(NULL); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3881 | } |
| 3882 | #if ENABLE_HUSH_FUNCTIONS |
| 3883 | else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3884 | # if ENABLE_HUSH_LOCAL |
| 3885 | struct variable **sv; |
| 3886 | sv = G.shadowed_vars_pp; |
| 3887 | G.shadowed_vars_pp = &old_vars; |
| 3888 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3889 | debug_printf_exec(": function '%s' '%s'...\n", |
| 3890 | funcp->name, argv_expanded[1]); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3891 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3892 | # if ENABLE_HUSH_LOCAL |
| 3893 | G.shadowed_vars_pp = sv; |
| 3894 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3895 | } |
| 3896 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3897 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3898 | #if ENABLE_FEATURE_SH_STANDALONE |
| 3899 | clean_up_and_ret: |
| 3900 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3901 | restore_redirects(squirrel); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3902 | unset_vars(new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3903 | add_vars(old_vars); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3904 | clean_up_and_ret1: |
| 3905 | free(argv_expanded); |
| 3906 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3907 | debug_leave(); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3908 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 3909 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3910 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3911 | |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3912 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3913 | i = find_applet_by_name(argv_expanded[0]); |
| 3914 | if (i >= 0 && APPLET_IS_NOFORK(i)) { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3915 | rcode = setup_redirects(command, squirrel); |
| 3916 | if (rcode == 0) { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3917 | new_env = expand_assignments(argv, command->assignment_cnt); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3918 | old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3919 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3920 | argv_expanded[0], argv_expanded[1]); |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 3921 | rcode = run_nofork_applet(i, argv_expanded); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3922 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3923 | goto clean_up_and_ret; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3924 | } |
| 3925 | #endif |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3926 | /* It is neither builtin nor applet. We must fork. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3927 | } |
| 3928 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3929 | must_fork: |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3930 | /* NB: argv_expanded may already be created, and that |
| 3931 | * might include `cmd` runs! Do not rerun it! We *must* |
| 3932 | * use argv_expanded if it's non-NULL */ |
| 3933 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3934 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3935 | pi->alive_cmds = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3936 | nextin = 0; |
| 3937 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3938 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3939 | struct fd_pair pipefds; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3940 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3941 | volatile nommu_save_t nommu_save; |
| 3942 | nommu_save.new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3943 | nommu_save.old_vars = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3944 | nommu_save.argv = NULL; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3945 | nommu_save.argv_from_re_execing = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3946 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3947 | command = &(pi->cmds[i]); |
| 3948 | if (command->argv) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3949 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 3950 | command->argv[0], command->argv[1]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3951 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3952 | debug_printf_exec(": pipe member with no argv\n"); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3953 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3954 | |
| 3955 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3956 | pipefds.rd = 0; |
| 3957 | pipefds.wr = 1; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3958 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3959 | xpiped_pair(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3960 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3961 | command->pid = BB_MMU ? fork() : vfork(); |
| 3962 | if (!command->pid) { /* child */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3963 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3964 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3965 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3966 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3967 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3968 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3969 | pid_t pgrp; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3970 | pgrp = pi->pgrp; |
| 3971 | if (pgrp < 0) /* true for 1st process only */ |
| 3972 | pgrp = getpid(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3973 | if (setpgid(0, pgrp) == 0 |
| 3974 | && pi->followup != PIPE_BG |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 3975 | && G_saved_tty_pgrp /* we have ctty */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3976 | ) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3977 | /* We do it in *every* child, not just first, |
| 3978 | * to avoid races */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3979 | tcsetpgrp(G_interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3980 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3981 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3982 | #endif |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3983 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 3984 | /* 1st cmd in backgrounded pipe |
| 3985 | * should have its stdin /dev/null'ed */ |
| 3986 | close(0); |
| 3987 | if (open(bb_dev_null, O_RDONLY)) |
| 3988 | xopen("/", O_RDONLY); |
| 3989 | } else { |
| 3990 | xmove_fd(nextin, 0); |
| 3991 | } |
| 3992 | xmove_fd(pipefds.wr, 1); |
| 3993 | if (pipefds.rd > 1) |
| 3994 | close(pipefds.rd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3995 | /* Like bash, explicit redirects override pipes, |
| 3996 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3997 | if (setup_redirects(command, NULL)) |
| 3998 | _exit(1); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3999 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4000 | /* Restore default handlers just prior to exec */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 4001 | /*signal(SIGCHLD, SIG_DFL); - so far we don't have any handlers */ |
| 4002 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4003 | /* Stores to nommu_save list of env vars putenv'ed |
| 4004 | * (NOMMU, on MMU we don't need that) */ |
| 4005 | /* cast away volatility... */ |
| 4006 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4007 | /* pseudo_exec() does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4008 | } |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 4009 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 4010 | /* parent or error */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 4011 | #if ENABLE_HUSH_FAST |
| 4012 | G.count_SIGCHLD++; |
| 4013 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 4014 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4015 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4016 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4017 | /* Clean up after vforked child */ |
| 4018 | free(nommu_save.argv); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4019 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 4020 | unset_vars(nommu_save.new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4021 | add_vars(nommu_save.old_vars); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4022 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4023 | free(argv_expanded); |
| 4024 | argv_expanded = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4025 | if (command->pid < 0) { /* [v]fork failed */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4026 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 4027 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4028 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4029 | pi->alive_cmds++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4030 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4031 | /* Second and next children need to know pid of first one */ |
| 4032 | if (pi->pgrp < 0) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4033 | pi->pgrp = command->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4034 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4035 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4036 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4037 | if (i) |
| 4038 | close(nextin); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4039 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4040 | close(pipefds.wr); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4041 | /* Pass read (output) pipe end to next iteration */ |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4042 | nextin = pipefds.rd; |
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 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4045 | if (!pi->alive_cmds) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4046 | debug_leave(); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4047 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 4048 | return 1; |
| 4049 | } |
| 4050 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4051 | debug_leave(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4052 | 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] | 4053 | return -1; |
| 4054 | } |
| 4055 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 4056 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4057 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 4058 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4059 | static const char *const PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4060 | [PIPE_SEQ] = "SEQ", |
| 4061 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4062 | [PIPE_OR ] = "OR" , |
| 4063 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4064 | }; |
| 4065 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4066 | [RES_NONE ] = "NONE" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4067 | #if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4068 | [RES_IF ] = "IF" , |
| 4069 | [RES_THEN ] = "THEN" , |
| 4070 | [RES_ELIF ] = "ELIF" , |
| 4071 | [RES_ELSE ] = "ELSE" , |
| 4072 | [RES_FI ] = "FI" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4073 | #endif |
| 4074 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4075 | [RES_FOR ] = "FOR" , |
| 4076 | [RES_WHILE] = "WHILE", |
| 4077 | [RES_UNTIL] = "UNTIL", |
| 4078 | [RES_DO ] = "DO" , |
| 4079 | [RES_DONE ] = "DONE" , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 4080 | #endif |
| 4081 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4082 | [RES_IN ] = "IN" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4083 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4084 | #if ENABLE_HUSH_CASE |
| 4085 | [RES_CASE ] = "CASE" , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4086 | [RES_CASE_IN ] = "CASE_IN" , |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4087 | [RES_MATCH] = "MATCH", |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4088 | [RES_CASE_BODY] = "CASE_BODY", |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4089 | [RES_ESAC ] = "ESAC" , |
| 4090 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4091 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4092 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4093 | }; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4094 | static const char *const CMDTYPE[] = { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4095 | "{}", |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 4096 | "()", |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4097 | "[noglob]", |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4098 | #if ENABLE_HUSH_FUNCTIONS |
| 4099 | "func()", |
| 4100 | #endif |
| 4101 | }; |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4102 | |
| 4103 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4104 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4105 | pin = 0; |
| 4106 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4107 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 4108 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4109 | prn = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4110 | while (prn < pi->num_cmds) { |
| 4111 | struct command *command = &pi->cmds[prn]; |
| 4112 | char **argv = command->argv; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4113 | |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4114 | fprintf(stderr, "%*s cmd %d assignment_cnt:%d", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4115 | lvl*2, "", prn, |
| 4116 | command->assignment_cnt); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4117 | if (command->group) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4118 | fprintf(stderr, " group %s: (argv=%p)\n", |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4119 | CMDTYPE[command->cmd_type], |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4120 | argv); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4121 | debug_print_tree(command->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4122 | prn++; |
| 4123 | continue; |
| 4124 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4125 | if (argv) while (*argv) { |
| 4126 | fprintf(stderr, " '%s'", *argv); |
| 4127 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 4128 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4129 | fprintf(stderr, "\n"); |
| 4130 | prn++; |
| 4131 | } |
| 4132 | pi = pi->next; |
| 4133 | pin++; |
| 4134 | } |
| 4135 | } |
| 4136 | #endif |
| 4137 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 4138 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 4139 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4140 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4141 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4142 | #if ENABLE_HUSH_CASE |
| 4143 | char *case_word = NULL; |
| 4144 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4145 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4146 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4147 | char **for_lcur = NULL; |
| 4148 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4149 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4150 | smallint last_followup; |
| 4151 | smalluint rcode; |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 4152 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4153 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4154 | #else |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4155 | enum { cond_code = 0 }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4156 | #endif |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4157 | #if HAS_KEYWORDS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4158 | smallint rword; /* enum reserved_style */ |
| 4159 | smallint last_rword; /* ditto */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4160 | #endif |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4161 | |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4162 | 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] | 4163 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4164 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4165 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4166 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4167 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 4168 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4169 | continue; |
| 4170 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4171 | if (cpipe->next == NULL) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4172 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4173 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4174 | 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] | 4175 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4176 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4177 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4178 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4179 | continue; |
| 4180 | /* 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] | 4181 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 4182 | || 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] | 4183 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4184 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4185 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4186 | 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] | 4187 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4188 | } |
| 4189 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4190 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4191 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4192 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 4193 | * in order to return, no direct "return" statements please. |
| 4194 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4195 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4196 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4197 | G.run_list_level++; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4198 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4199 | |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4200 | #if HAS_KEYWORDS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4201 | rword = RES_NONE; |
| 4202 | last_rword = RES_XXXX; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4203 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4204 | last_followup = PIPE_SEQ; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4205 | rcode = G.last_exitcode; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4206 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4207 | /* Go through list of pipes, (maybe) executing them. */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4208 | 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] | 4209 | if (G.flag_SIGINT) |
| 4210 | break; |
| 4211 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4212 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4213 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 4214 | rword, cond_code, last_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4215 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 4216 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4217 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 4218 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4219 | /* start of a loop: remember where loop starts */ |
| 4220 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4221 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4222 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4223 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4224 | /* Still in the same "if...", "then..." or "do..." branch? */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4225 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4226 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 4227 | || (rcode != 0 && last_followup == PIPE_AND) |
| 4228 | ) { |
| 4229 | /* It is "<true> || CMD" or "<false> && CMD" |
| 4230 | * and we should not execute CMD */ |
| 4231 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 4232 | last_followup = pi->followup; |
| 4233 | continue; |
| 4234 | } |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4235 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4236 | last_followup = pi->followup; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4237 | IF_HAS_KEYWORDS(last_rword = rword;) |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4238 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4239 | if (cond_code) { |
| 4240 | if (rword == RES_THEN) { |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4241 | /* if false; then ... fi has exitcode 0! */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4242 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4243 | /* "if <false> THEN cmd": skip cmd */ |
| 4244 | continue; |
| 4245 | } |
| 4246 | } else { |
| 4247 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 4248 | /* "if <true> then ... ELSE/ELIF cmd": |
| 4249 | * skip cmd and all following ones */ |
| 4250 | break; |
| 4251 | } |
| 4252 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4253 | #endif |
| 4254 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4255 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4256 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 4257 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4258 | |
| 4259 | static const char encoded_dollar_at[] ALIGN1 = { |
| 4260 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 4261 | }; /* encoded representation of "$@" */ |
| 4262 | static const char *const encoded_dollar_at_argv[] = { |
| 4263 | encoded_dollar_at, NULL |
| 4264 | }; /* argv list with one element: "$@" */ |
| 4265 | char **vals; |
| 4266 | |
| 4267 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4268 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4269 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4270 | if (!pi->next->cmds[0].argv) { |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4271 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4272 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4273 | break; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4274 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4275 | vals = pi->next->cmds[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4276 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4277 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4278 | debug_print_strings("for_list made from", vals); |
| 4279 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4280 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4281 | debug_print_strings("for_list", for_list); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4282 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4283 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 4284 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4285 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4286 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4287 | for_lcur = NULL; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4288 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4289 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4290 | /* Insert next value from for_lcur */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4291 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4292 | 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] | 4293 | continue; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4294 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4295 | if (rword == RES_IN) { |
| 4296 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 4297 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4298 | if (rword == RES_DONE) { |
| 4299 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4300 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4301 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4302 | #if ENABLE_HUSH_CASE |
| 4303 | if (rword == RES_CASE) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4304 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4305 | continue; |
| 4306 | } |
| 4307 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4308 | char **argv; |
| 4309 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4310 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 4311 | break; |
| 4312 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4313 | argv = pi->cmds->argv; |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4314 | while (*argv) { |
| 4315 | char *pattern = expand_string_to_string(*argv); |
| 4316 | /* TODO: which FNM_xxx flags to use? */ |
| 4317 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 4318 | free(pattern); |
| 4319 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 4320 | free(case_word); /* make future "word)" stop */ |
| 4321 | case_word = NULL; |
| 4322 | break; |
| 4323 | } |
| 4324 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4325 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4326 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4327 | } |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4328 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4329 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4330 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4331 | } |
| 4332 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4333 | /* Just pressing <enter> in shell should check for jobs. |
| 4334 | * OTOH, in non-interactive shell this is useless |
| 4335 | * and only leads to extra job checks */ |
| 4336 | if (pi->num_cmds == 0) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4337 | if (G_interactive_fd) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4338 | goto check_jobs_and_continue; |
| 4339 | continue; |
| 4340 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4341 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4342 | /* After analyzing all keywords and conditions, we decided |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4343 | * to execute this pipe. NB: have to do checkjobs(NULL) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4344 | * after run_pipe to collect any background children, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4345 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4346 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4347 | { |
| 4348 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4349 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4350 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4351 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4352 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 4353 | if (r != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4354 | /* We ran a builtin, function, or group. |
| 4355 | * rcode is already known |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4356 | * and we don't need to wait for anything. */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4357 | G.last_exitcode = rcode; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4358 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4359 | check_and_run_traps(0); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4360 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4361 | /* Was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4362 | if (G.flag_break_continue) { |
| 4363 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4364 | /* We might fall into outer *loop*, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4365 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4366 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4367 | G.depth_break_continue--; |
| 4368 | if (G.depth_break_continue == 0) |
| 4369 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4370 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 4371 | } /* 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] | 4372 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4373 | goto check_jobs_and_break; |
| 4374 | /* "continue": simulate end of loop */ |
| 4375 | rword = RES_DONE; |
| 4376 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4377 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4378 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4379 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 673e945 | 2009-05-27 14:39:35 +0200 | [diff] [blame] | 4380 | if (G.flag_return_in_progress == 1) { |
| 4381 | /* same as "goto check_jobs_and_break" */ |
| 4382 | checkjobs(NULL); |
| 4383 | break; |
| 4384 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4385 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4386 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4387 | /* What does bash do with attempts to background builtins? */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4388 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4389 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 4390 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4391 | check_and_run_traps(0); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4392 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4393 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4394 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4395 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4396 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4397 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4398 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4399 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4400 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4401 | /* Waits for completion, then fg's main shell */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4402 | rcode = checkjobs_and_fg_shell(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4403 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4404 | check_and_run_traps(0); |
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 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4407 | { /* This one just waits for completion */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4408 | rcode = checkjobs(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4409 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4410 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4411 | } |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4412 | G.last_exitcode = rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4413 | } |
| 4414 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4415 | |
| 4416 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4417 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4418 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4419 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4420 | #endif |
| 4421 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4422 | /* Beware of "while false; true; do ..."! */ |
| 4423 | if (pi->next && pi->next->res_word == RES_DO) { |
| 4424 | if (rword == RES_WHILE) { |
| 4425 | if (rcode) { |
| 4426 | /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4427 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4428 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
| 4429 | goto check_jobs_and_break; |
| 4430 | } |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 4431 | } |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4432 | if (rword == RES_UNTIL) { |
| 4433 | if (!rcode) { |
| 4434 | debug_printf_exec(": until expr is true: breaking\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4435 | check_jobs_and_break: |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4436 | checkjobs(NULL); |
| 4437 | break; |
| 4438 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4439 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4440 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4441 | #endif |
Mike Frysinger | 8ec1c9d | 2009-03-29 00:45:26 +0000 | [diff] [blame] | 4442 | |
| 4443 | check_jobs_and_continue: |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 4444 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4445 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4446 | |
| 4447 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4448 | G.run_list_level--; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4449 | #endif |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4450 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4451 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4452 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4453 | free(for_list); |
| 4454 | #endif |
| 4455 | #if ENABLE_HUSH_CASE |
| 4456 | free(case_word); |
| 4457 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4458 | debug_leave(); |
| 4459 | 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] | 4460 | return rcode; |
| 4461 | } |
| 4462 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4463 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4464 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4465 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4466 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4467 | debug_printf_exec("run_and_free_list entered\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4468 | if (!G.fake_mode) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4469 | 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] | 4470 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4471 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4472 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4473 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4474 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4475 | free_pipe_list(pi); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 4476 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4477 | return rcode; |
| 4478 | } |
| 4479 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4480 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 4481 | static struct pipe *new_pipe(void) |
| 4482 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4483 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4484 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4485 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4486 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4487 | return pi; |
| 4488 | } |
| 4489 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4490 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 4491 | * if ctx->command is NULL. |
| 4492 | * No errors possible here. |
| 4493 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4494 | static int done_command(struct parse_context *ctx) |
| 4495 | { |
| 4496 | /* The command is really already in the pipe structure, so |
| 4497 | * advance the pipe counter and make a new, null command. */ |
| 4498 | struct pipe *pi = ctx->pipe; |
| 4499 | struct command *command = ctx->command; |
| 4500 | |
| 4501 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4502 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4503 | 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] | 4504 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4505 | } |
| 4506 | pi->num_cmds++; |
| 4507 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4508 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4509 | } else { |
| 4510 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 4511 | } |
| 4512 | |
| 4513 | /* Only real trickiness here is that the uncommitted |
| 4514 | * command structure is not counted in pi->num_cmds. */ |
| 4515 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4516 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 4517 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4518 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4519 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 4520 | } |
| 4521 | |
| 4522 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 4523 | { |
| 4524 | int not_null; |
| 4525 | |
| 4526 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 4527 | /* Close previous command */ |
| 4528 | not_null = done_command(ctx); |
| 4529 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4530 | #if HAS_KEYWORDS |
| 4531 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 4532 | ctx->ctx_inverted = 0; |
| 4533 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 4534 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4535 | |
| 4536 | /* Without this check, even just <enter> on command line generates |
| 4537 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4538 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4539 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4540 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4541 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4542 | #endif |
| 4543 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4544 | || ctx->ctx_res_w == RES_DONE |
| 4545 | || ctx->ctx_res_w == RES_FOR |
| 4546 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4547 | #endif |
| 4548 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4549 | || ctx->ctx_res_w == RES_ESAC |
| 4550 | #endif |
| 4551 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4552 | struct pipe *new_p; |
| 4553 | debug_printf_parse("done_pipe: adding new pipe: " |
| 4554 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 4555 | not_null, ctx->ctx_res_w); |
| 4556 | new_p = new_pipe(); |
| 4557 | ctx->pipe->next = new_p; |
| 4558 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4559 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4560 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4561 | * This is used to control execution. |
| 4562 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 4563 | * cases where variable or value happens to match a keyword): |
| 4564 | */ |
| 4565 | #if ENABLE_HUSH_LOOPS |
| 4566 | if (ctx->ctx_res_w == RES_FOR |
| 4567 | || ctx->ctx_res_w == RES_IN) |
| 4568 | ctx->ctx_res_w = RES_NONE; |
| 4569 | #endif |
| 4570 | #if ENABLE_HUSH_CASE |
| 4571 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4572 | ctx->ctx_res_w = RES_CASE_BODY; |
| 4573 | if (ctx->ctx_res_w == RES_CASE) |
| 4574 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4575 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4576 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4577 | /* Create the memory for command, roughly: |
| 4578 | * ctx->pipe->cmds = new struct command; |
| 4579 | * ctx->command = &ctx->pipe->cmds[0]; |
| 4580 | */ |
| 4581 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4582 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4583 | } |
| 4584 | debug_printf_parse("done_pipe return\n"); |
| 4585 | } |
| 4586 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4587 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4588 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4589 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4590 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4591 | /* Create the memory for command, roughly: |
| 4592 | * ctx->pipe->cmds = new struct command; |
| 4593 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4594 | */ |
| 4595 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4598 | /* If a reserved word is found and processed, parse context is modified |
| 4599 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4600 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4601 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4602 | struct reserved_combo { |
| 4603 | char literal[6]; |
| 4604 | unsigned char res; |
| 4605 | unsigned char assignment_flag; |
| 4606 | int flag; |
| 4607 | }; |
| 4608 | enum { |
| 4609 | FLAG_END = (1 << RES_NONE ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4610 | #if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4611 | FLAG_IF = (1 << RES_IF ), |
| 4612 | FLAG_THEN = (1 << RES_THEN ), |
| 4613 | FLAG_ELIF = (1 << RES_ELIF ), |
| 4614 | FLAG_ELSE = (1 << RES_ELSE ), |
| 4615 | FLAG_FI = (1 << RES_FI ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4616 | #endif |
| 4617 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4618 | FLAG_FOR = (1 << RES_FOR ), |
| 4619 | FLAG_WHILE = (1 << RES_WHILE), |
| 4620 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 4621 | FLAG_DO = (1 << RES_DO ), |
| 4622 | FLAG_DONE = (1 << RES_DONE ), |
| 4623 | FLAG_IN = (1 << RES_IN ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4624 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4625 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4626 | FLAG_MATCH = (1 << RES_MATCH), |
| 4627 | FLAG_ESAC = (1 << RES_ESAC ), |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4628 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4629 | FLAG_START = (1 << RES_XXXX ), |
| 4630 | }; |
| 4631 | |
| 4632 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 4633 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4634 | /* Mostly a list of accepted follow-up reserved words. |
| 4635 | * FLAG_END means we are done with the sequence, and are ready |
| 4636 | * to turn the compound list into a command. |
| 4637 | * FLAG_START means the word must start a new compound list. |
| 4638 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4639 | static const struct reserved_combo reserved_list[] = { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4640 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4641 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 4642 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 4643 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 4644 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 4645 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 4646 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4647 | #endif |
| 4648 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4649 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 4650 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 4651 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 4652 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 4653 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 4654 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4655 | #endif |
| 4656 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4657 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 4658 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4659 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4660 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4661 | const struct reserved_combo *r; |
| 4662 | |
| 4663 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 4664 | if (strcmp(word->data, r->literal) == 0) |
| 4665 | return r; |
| 4666 | } |
| 4667 | return NULL; |
| 4668 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4669 | /* Return 0: not a keyword, 1: keyword |
| 4670 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4671 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 4672 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4673 | #if ENABLE_HUSH_CASE |
| 4674 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4675 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4676 | }; |
| 4677 | #endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4678 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4679 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4680 | if (word->o_quoted) |
| 4681 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4682 | r = match_reserved_word(word); |
| 4683 | if (!r) |
| 4684 | return 0; |
| 4685 | |
| 4686 | 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] | 4687 | #if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4688 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 4689 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4690 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4691 | } else |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4692 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4693 | if (r->flag == 0) { /* '!' */ |
| 4694 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4695 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4696 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4697 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4698 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4699 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4700 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4701 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4702 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4703 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4704 | old = xmalloc(sizeof(*old)); |
| 4705 | debug_printf_parse("push stack %p\n", old); |
| 4706 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4707 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4708 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4709 | } 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] | 4710 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4711 | ctx->ctx_res_w = RES_SNTX; |
| 4712 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4713 | } else { |
| 4714 | /* "{...} fi" is ok. "{...} if" is not |
| 4715 | * Example: |
| 4716 | * if { echo foo; } then { echo bar; } fi */ |
| 4717 | if (ctx->command->group) |
| 4718 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4719 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4720 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4721 | ctx->ctx_res_w = r->res; |
| 4722 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4723 | word->o_assignment = r->assignment_flag; |
| 4724 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4725 | if (ctx->old_flag & FLAG_END) { |
| 4726 | struct parse_context *old; |
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 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4729 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4730 | old = ctx->stack; |
| 4731 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4732 | old->command->cmd_type = CMD_NORMAL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4733 | #if !BB_MMU |
| 4734 | o_addstr(&old->as_string, ctx->as_string.data); |
| 4735 | o_free_unsafe(&ctx->as_string); |
| 4736 | old->command->group_as_string = xstrdup(old->as_string.data); |
| 4737 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 4738 | old->command->group_as_string); |
| 4739 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4740 | *ctx = *old; /* physical copy */ |
| 4741 | free(old); |
| 4742 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4743 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4744 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4745 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4746 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4747 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4748 | * Normal return is 0. Syntax errors return 1. |
| 4749 | * Note: on return, word is reset, but not o_free'd! |
| 4750 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4751 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4752 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4753 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4754 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4755 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4756 | if (word->length == 0 && word->o_quoted == 0) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4757 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 4758 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4759 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4760 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4761 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4762 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 4763 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4764 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4765 | * "2.7 Redirection |
| 4766 | * ...the word that follows the redirection operator |
| 4767 | * shall be subjected to tilde expansion, parameter expansion, |
| 4768 | * command substitution, arithmetic expansion, and quote |
| 4769 | * removal. Pathname expansion shall not be performed |
| 4770 | * on the word by a non-interactive shell; an interactive |
| 4771 | * shell may perform it, but shall do so only when |
| 4772 | * the expansion would result in one word." |
| 4773 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4774 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4775 | /* Cater for >\file case: |
| 4776 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 4777 | * Same with heredocs: |
| 4778 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 4779 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4780 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 4781 | unbackslash(ctx->pending_redirect->rd_filename); |
| 4782 | /* Is it <<"HEREDOC"? */ |
| 4783 | if (word->o_quoted) { |
| 4784 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 4785 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4786 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4787 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4788 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4789 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4790 | /* If this word wasn't an assignment, next ones definitely |
| 4791 | * can't be assignments. Even if they look like ones. */ |
| 4792 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 4793 | && word->o_assignment != WORD_IS_KEYWORD |
| 4794 | ) { |
| 4795 | word->o_assignment = NOT_ASSIGNMENT; |
| 4796 | } else { |
| 4797 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
| 4798 | command->assignment_cnt++; |
| 4799 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 4800 | } |
| 4801 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4802 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4803 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 4804 | if (ctx->ctx_dsemicolon |
| 4805 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 4806 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 4807 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4808 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 4809 | ctx->ctx_dsemicolon = 0; |
| 4810 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4811 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4812 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4813 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4814 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 4815 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4816 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4817 | # if ENABLE_HUSH_CASE |
| 4818 | && ctx->ctx_res_w != RES_CASE |
| 4819 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4820 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4821 | debug_printf_parse("checking '%s' for reserved-ness\n", word->data); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4822 | if (reserved_word(word, ctx)) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4823 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4824 | debug_printf_parse("done_word return %d\n", |
| 4825 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4826 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4827 | } |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4828 | # ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 4829 | if (strcmp(word->data, "export") == 0 |
| 4830 | # if ENABLE_HUSH_LOCAL |
| 4831 | || strcmp(word->data, "local") == 0 |
| 4832 | # endif |
| 4833 | ) { |
| 4834 | command->cmd_type = CMD_SINGLEWORD_NOGLOB_COND; |
| 4835 | } else |
| 4836 | # endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4837 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4838 | if (strcmp(word->data, "[[") == 0) { |
| 4839 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 4840 | } |
| 4841 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4842 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4843 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4844 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4845 | if (command->group) { |
| 4846 | /* "{ echo foo; } echo bar" - bad */ |
| 4847 | syntax_error_at(word->data); |
| 4848 | debug_printf_parse("done_word return 1: syntax error, " |
| 4849 | "groups and arglists don't mix\n"); |
| 4850 | return 1; |
| 4851 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4852 | 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] | 4853 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 4854 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4855 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4856 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4857 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 4858 | char *p = word->data; |
| 4859 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 4860 | && (p[1] & 0x7f) == '@' |
| 4861 | && p[2] == SPECIAL_VAR_SYMBOL |
| 4862 | ) { |
| 4863 | p += 3; |
| 4864 | } |
| 4865 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4866 | /* saw no "$@", or not only "$@" but some |
| 4867 | * real text is there too */ |
| 4868 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 4869 | * e.g. "", $empty"" etc to not disappear */ |
| 4870 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 4871 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 4872 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 4873 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4874 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4875 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4876 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4877 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4878 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4879 | if (ctx->ctx_res_w == RES_FOR) { |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4880 | if (word->o_quoted |
| 4881 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 4882 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4883 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4884 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4885 | return 1; |
| 4886 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4887 | /* Force FOR to have just one word (variable name) */ |
| 4888 | /* NB: basically, this makes hush see "for v in ..." |
| 4889 | * syntax as if it is "for v; in ...". FOR and IN become |
| 4890 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4891 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4892 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4893 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4894 | #if ENABLE_HUSH_CASE |
| 4895 | /* Force CASE to have just one word */ |
| 4896 | if (ctx->ctx_res_w == RES_CASE) { |
| 4897 | done_pipe(ctx, PIPE_SEQ); |
| 4898 | } |
| 4899 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4900 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4901 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4902 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4903 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4904 | return 0; |
| 4905 | } |
| 4906 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4907 | |
| 4908 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 4909 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4910 | * Return: |
| 4911 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 4912 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 4913 | * REDIRFD_TO_FILE if no & was seen, |
| 4914 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4915 | */ |
| 4916 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4917 | #define parse_redir_right_fd(as_string, input) \ |
| 4918 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4919 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4920 | 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] | 4921 | { |
| 4922 | int ch, d, ok; |
| 4923 | |
| 4924 | ch = i_peek(input); |
| 4925 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4926 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4927 | |
| 4928 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4929 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4930 | ch = i_peek(input); |
| 4931 | if (ch == '-') { |
| 4932 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4933 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4934 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4935 | } |
| 4936 | d = 0; |
| 4937 | ok = 0; |
| 4938 | while (ch != EOF && isdigit(ch)) { |
| 4939 | d = d*10 + (ch-'0'); |
| 4940 | ok = 1; |
| 4941 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4942 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4943 | ch = i_peek(input); |
| 4944 | } |
| 4945 | if (ok) return d; |
| 4946 | |
| 4947 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 4948 | |
| 4949 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4950 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4951 | } |
| 4952 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4953 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4954 | */ |
| 4955 | static int parse_redirect(struct parse_context *ctx, |
| 4956 | int fd, |
| 4957 | redir_type style, |
| 4958 | struct in_str *input) |
| 4959 | { |
| 4960 | struct command *command = ctx->command; |
| 4961 | struct redir_struct *redir; |
| 4962 | struct redir_struct **redirp; |
| 4963 | int dup_num; |
| 4964 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4965 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4966 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4967 | /* Check for a '>&1' type redirect */ |
| 4968 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 4969 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 4970 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4971 | } else { |
| 4972 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4973 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4974 | if (dup_num) { /* <<-... */ |
| 4975 | ch = i_getch(input); |
| 4976 | nommu_addchr(&ctx->as_string, ch); |
| 4977 | ch = i_peek(input); |
| 4978 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4979 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4980 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4981 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4982 | int ch = i_peek(input); |
| 4983 | if (ch == '|') { |
| 4984 | /* >|FILE redirect ("clobbering" >). |
| 4985 | * Since we do not support "set -o noclobber" yet, |
| 4986 | * >| and > are the same for now. Just eat |. |
| 4987 | */ |
| 4988 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4989 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4990 | } |
| 4991 | } |
| 4992 | |
| 4993 | /* Create a new redir_struct and append it to the linked list */ |
| 4994 | redirp = &command->redirects; |
| 4995 | while ((redir = *redirp) != NULL) { |
| 4996 | redirp = &(redir->next); |
| 4997 | } |
| 4998 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 4999 | /* redir->next = NULL; */ |
| 5000 | /* redir->rd_filename = NULL; */ |
| 5001 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5002 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5003 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5004 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 5005 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5006 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5007 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5008 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5009 | /* Erik had a check here that the file descriptor in question |
| 5010 | * is legit; I postpone that to "run time" |
| 5011 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5012 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 5013 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5014 | } else { |
| 5015 | /* Set ctx->pending_redirect, so we know what to do at the |
| 5016 | * end of the next parsed word. */ |
| 5017 | ctx->pending_redirect = redir; |
| 5018 | } |
| 5019 | return 0; |
| 5020 | } |
| 5021 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5022 | /* If a redirect is immediately preceded by a number, that number is |
| 5023 | * supposed to tell which file descriptor to redirect. This routine |
| 5024 | * looks for such preceding numbers. In an ideal world this routine |
| 5025 | * needs to handle all the following classes of redirects... |
| 5026 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 5027 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 5028 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 5029 | * 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] | 5030 | * |
| 5031 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 5032 | * "2.7 Redirection |
| 5033 | * ... If n is quoted, the number shall not be recognized as part of |
| 5034 | * the redirection expression. For example: |
| 5035 | * echo \2>a |
| 5036 | * writes the character 2 into file a" |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5037 | * We are getting it right by setting ->o_quoted on any \<char> |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5038 | * |
| 5039 | * A -1 return means no valid number was found, |
| 5040 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5041 | */ |
| 5042 | static int redirect_opt_num(o_string *o) |
| 5043 | { |
| 5044 | int num; |
| 5045 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5046 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5047 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5048 | num = bb_strtou(o->data, NULL, 10); |
| 5049 | if (errno || num < 0) |
| 5050 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5051 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5052 | return num; |
| 5053 | } |
| 5054 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5055 | #if BB_MMU |
| 5056 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 5057 | fetch_till_str(input, word, skip_tabs) |
| 5058 | #endif |
| 5059 | static char *fetch_till_str(o_string *as_string, |
| 5060 | struct in_str *input, |
| 5061 | const char *word, |
| 5062 | int skip_tabs) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5063 | { |
| 5064 | o_string heredoc = NULL_O_STRING; |
| 5065 | int past_EOL = 0; |
| 5066 | int ch; |
| 5067 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5068 | goto jump_in; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5069 | while (1) { |
| 5070 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5071 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5072 | if (ch == '\n') { |
| 5073 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 5074 | heredoc.data[past_EOL] = '\0'; |
| 5075 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 5076 | return heredoc.data; |
| 5077 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5078 | do { |
| 5079 | o_addchr(&heredoc, ch); |
| 5080 | past_EOL = heredoc.length; |
| 5081 | jump_in: |
| 5082 | do { |
| 5083 | ch = i_getch(input); |
| 5084 | nommu_addchr(as_string, ch); |
| 5085 | } while (skip_tabs && ch == '\t'); |
| 5086 | } while (ch == '\n'); |
| 5087 | } |
| 5088 | if (ch == EOF) { |
| 5089 | o_free_unsafe(&heredoc); |
| 5090 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5091 | } |
| 5092 | o_addchr(&heredoc, ch); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5093 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5094 | } |
| 5095 | } |
| 5096 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5097 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 5098 | * and load them all. There should be exactly heredoc_cnt of them. |
| 5099 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5100 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 5101 | { |
| 5102 | struct pipe *pi = ctx->list_head; |
| 5103 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5104 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5105 | int i; |
| 5106 | struct command *cmd = pi->cmds; |
| 5107 | |
| 5108 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 5109 | pi->num_cmds, |
| 5110 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 5111 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5112 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5113 | |
| 5114 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 5115 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5116 | while (redir) { |
| 5117 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5118 | char *p; |
| 5119 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5120 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5121 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5122 | p = fetch_till_str(&ctx->as_string, input, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5123 | redir->rd_filename, redir->rd_dup & HEREDOC_SKIPTABS); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5124 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5125 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5126 | return 1; |
| 5127 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5128 | free(redir->rd_filename); |
| 5129 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5130 | heredoc_cnt--; |
| 5131 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5132 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5133 | } |
| 5134 | cmd++; |
| 5135 | } |
| 5136 | pi = pi->next; |
| 5137 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5138 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5139 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5140 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5141 | bb_error_msg_and_die("heredoc BUG 2"); |
| 5142 | #endif |
| 5143 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5144 | } |
| 5145 | |
| 5146 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5147 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5148 | static FILE *generate_stream_from_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5149 | { |
| 5150 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5151 | int pid, channel[2]; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5152 | #if !BB_MMU |
| 5153 | char **to_free; |
| 5154 | #endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 5155 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 5156 | xpipe(channel); |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 5157 | pid = BB_MMU ? fork() : vfork(); |
| 5158 | if (pid < 0) |
| 5159 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5160 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5161 | if (pid == 0) { /* child */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5162 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 5163 | /* Process substitution is not considered to be usual |
| 5164 | * 'command execution'. |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 5165 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 5166 | */ |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 5167 | bb_signals(0 |
| 5168 | + (1 << SIGTSTP) |
| 5169 | + (1 << SIGTTIN) |
| 5170 | + (1 << SIGTTOU) |
| 5171 | , SIG_IGN); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5172 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 5173 | xmove_fd(channel[1], 1); |
| 5174 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 5175 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5176 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5177 | reset_traps_to_defaults(); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5178 | parse_and_run_string(s); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5179 | _exit(G.last_exitcode); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5180 | #else |
| 5181 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5182 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 5183 | * huge=`cat BIG` # was blocking here forever |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5184 | * echo OK |
| 5185 | */ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5186 | re_execute_shell(&to_free, |
| 5187 | s, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5188 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 5189 | G.global_argv + 1, |
| 5190 | NULL); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5191 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5192 | } |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5193 | |
| 5194 | /* parent */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 5195 | #if ENABLE_HUSH_FAST |
| 5196 | G.count_SIGCHLD++; |
| 5197 | //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); |
| 5198 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5199 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5200 | #if !BB_MMU |
| 5201 | free(to_free); |
| 5202 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5203 | close(channel[1]); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 5204 | pf = fdopen(channel[0], "r"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5205 | return pf; |
| 5206 | } |
| 5207 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 5208 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5209 | static int process_command_subs(o_string *dest, const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5210 | { |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5211 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5212 | struct in_str pipe_str; |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5213 | int ch, eol_cnt; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5214 | |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5215 | pf = generate_stream_from_string(s); |
| 5216 | if (pf == NULL) |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5217 | return 1; |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5218 | close_on_exec_on(fileno(pf)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5219 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5220 | /* Now send results of command back into original context */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5221 | setup_file_in_str(&pipe_str, pf); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5222 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5223 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5224 | if (ch == '\n') { |
| 5225 | eol_cnt++; |
| 5226 | continue; |
| 5227 | } |
| 5228 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5229 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5230 | eol_cnt--; |
| 5231 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 5232 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5233 | } |
| 5234 | |
| 5235 | debug_printf("done reading from pipe, pclose()ing\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5236 | /* Note: we got EOF, and we just close the read end of the pipe. |
| 5237 | * 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] | 5238 | * Try these: |
| 5239 | * echo `echo Hi; exec 1>&-; sleep 2` - bash waits 2 sec |
| 5240 | * `false`; echo $? - bash outputs "1" |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5241 | */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5242 | fclose(pf); |
| 5243 | debug_printf("closed FILE from child. return 0\n"); |
| 5244 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5245 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5246 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5247 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5248 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5249 | struct in_str *input, int ch) |
| 5250 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5251 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5252 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5253 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5254 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5255 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5256 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5257 | |
| 5258 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5259 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5260 | if (ch == '(' && !dest->o_quoted) { |
| 5261 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5262 | if (done_word(dest, ctx)) |
| 5263 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5264 | if (!command->argv) |
| 5265 | goto skip; /* (... */ |
| 5266 | if (command->argv[1]) { /* word word ... (... */ |
| 5267 | syntax_error_unexpected_ch('('); |
| 5268 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5269 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5270 | /* it is "word(..." or "word (..." */ |
| 5271 | do |
| 5272 | ch = i_getch(input); |
| 5273 | while (ch == ' ' || ch == '\t'); |
| 5274 | if (ch != ')') { |
| 5275 | syntax_error_unexpected_ch(ch); |
| 5276 | return 1; |
| 5277 | } |
| 5278 | nommu_addchr(&ctx->as_string, ch); |
| 5279 | do |
| 5280 | ch = i_getch(input); |
| 5281 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 5282 | if (ch != '{') { |
| 5283 | syntax_error_unexpected_ch(ch); |
| 5284 | return 1; |
| 5285 | } |
| 5286 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5287 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5288 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5289 | } |
| 5290 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5291 | if (command->argv /* word [word]{... */ |
| 5292 | || dest->length /* word{... */ |
| 5293 | || dest->o_quoted /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5294 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5295 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5296 | debug_printf_parse("parse_group return 1: " |
| 5297 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5298 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5299 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5300 | |
| 5301 | #if ENABLE_HUSH_FUNCTIONS |
| 5302 | skip: |
| 5303 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5304 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 5305 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5306 | endch = ')'; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5307 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5308 | } else { |
| 5309 | /* bash does not allow "{echo...", requires whitespace */ |
| 5310 | ch = i_getch(input); |
| 5311 | if (ch != ' ' && ch != '\t' && ch != '\n') { |
| 5312 | syntax_error_unexpected_ch(ch); |
| 5313 | return 1; |
| 5314 | } |
| 5315 | nommu_addchr(&ctx->as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5316 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5317 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5318 | { |
| 5319 | #if !BB_MMU |
| 5320 | char *as_string = NULL; |
| 5321 | #endif |
| 5322 | pipe_list = parse_stream(&as_string, input, endch); |
| 5323 | #if !BB_MMU |
| 5324 | if (as_string) |
| 5325 | o_addstr(&ctx->as_string, as_string); |
| 5326 | #endif |
| 5327 | /* empty ()/{} or parse error? */ |
| 5328 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5329 | /* parse_stream already emitted error msg */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5330 | #if !BB_MMU |
| 5331 | free(as_string); |
| 5332 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5333 | debug_printf_parse("parse_group return 1: " |
| 5334 | "parse_stream returned %p\n", pipe_list); |
| 5335 | return 1; |
| 5336 | } |
| 5337 | command->group = pipe_list; |
| 5338 | #if !BB_MMU |
| 5339 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 5340 | command->group_as_string = as_string; |
| 5341 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 5342 | command->group_as_string); |
| 5343 | #endif |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5344 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5345 | debug_printf_parse("parse_group return 0\n"); |
| 5346 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5347 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5348 | } |
| 5349 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5350 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5351 | /* Subroutines for copying $(...) and `...` things */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5352 | static void add_till_backquote(o_string *dest, struct in_str *input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5353 | /* '...' */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5354 | 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] | 5355 | { |
| 5356 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5357 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5358 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5359 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5360 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5361 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5362 | if (ch == '\'') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5363 | return; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5364 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5365 | } |
| 5366 | } |
| 5367 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5368 | 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] | 5369 | { |
| 5370 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5371 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5372 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5373 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5374 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5375 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5376 | if (ch == '"') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5377 | return; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5378 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5379 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5380 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5381 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5382 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5383 | if (ch == '`') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5384 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5385 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5386 | continue; |
| 5387 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 5388 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5389 | } |
| 5390 | } |
| 5391 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 5392 | * \` quoting. |
| 5393 | * "Within the backquoted style of command substitution, backslash |
| 5394 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 5395 | * The search for the matching backquote shall be satisfied by the first |
| 5396 | * backquote found without a preceding backslash; during this search, |
| 5397 | * if a non-escaped backquote is encountered within a shell comment, |
| 5398 | * a here-document, an embedded command substitution of the $(command) |
| 5399 | * form, or a quoted string, undefined results occur. A single-quoted |
| 5400 | * or double-quoted string that begins, but does not end, within the |
| 5401 | * "`...`" sequence produces undefined results." |
| 5402 | * Example Output |
| 5403 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 5404 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5405 | static void add_till_backquote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5406 | { |
| 5407 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5408 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5409 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5410 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5411 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5412 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5413 | if (ch == '`') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5414 | return; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5415 | if (ch == '\\') { |
| 5416 | /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5417 | int ch2 = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5418 | if (ch2 == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5419 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5420 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5421 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5422 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5423 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5424 | ch = ch2; |
| 5425 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5426 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5427 | } |
| 5428 | } |
| 5429 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 5430 | * quoting and nested ()s. |
| 5431 | * "With the $(command) style of command substitution, all characters |
| 5432 | * following the open parenthesis to the matching closing parenthesis |
| 5433 | * constitute the command. Any valid shell script can be used for command, |
| 5434 | * except a script consisting solely of redirections which produces |
| 5435 | * unspecified results." |
| 5436 | * Example Output |
| 5437 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 5438 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 5439 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 5440 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5441 | 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] | 5442 | { |
| 5443 | int count = 0; |
| 5444 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5445 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5446 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5447 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5448 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5449 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5450 | if (ch == '(') |
| 5451 | count++; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5452 | if (ch == ')') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5453 | if (--count < 0) { |
| 5454 | if (!dbl) |
| 5455 | break; |
| 5456 | if (i_peek(input) == ')') { |
| 5457 | i_getch(input); |
| 5458 | break; |
| 5459 | } |
| 5460 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5461 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5462 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5463 | if (ch == '\'') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5464 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5465 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5466 | continue; |
| 5467 | } |
| 5468 | if (ch == '"') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5469 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5470 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5471 | continue; |
| 5472 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5473 | if (ch == '\\') { |
| 5474 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5475 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5476 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5477 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5478 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5479 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5480 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5481 | continue; |
| 5482 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5483 | } |
| 5484 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5485 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5486 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5487 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5488 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5489 | #define handle_dollar(as_string, dest, input) \ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5490 | handle_dollar(dest, input) |
| 5491 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5492 | static int handle_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5493 | o_string *dest, |
| 5494 | struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5495 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5496 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5497 | unsigned char quote_mask = dest->o_escape ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5498 | |
| 5499 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5500 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5501 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5502 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 5503 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5504 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5505 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5506 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5507 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5508 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5509 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5510 | if (!isalnum(ch) && ch != '_') |
| 5511 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5512 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5513 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5514 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5515 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5516 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5517 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5518 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5519 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5520 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5521 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5522 | o_addchr(dest, ch | quote_mask); |
| 5523 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5524 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5525 | case '$': /* pid */ |
| 5526 | case '!': /* last bg pid */ |
| 5527 | case '?': /* last exit code */ |
| 5528 | case '#': /* number of args */ |
| 5529 | case '*': /* args */ |
| 5530 | case '@': /* args */ |
| 5531 | goto make_one_char_var; |
| 5532 | case '{': { |
| 5533 | bool first_char, all_digits; |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 5534 | int expansion; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 5535 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5536 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5537 | nommu_addchr(as_string, ch); |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 5538 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5539 | |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 5540 | /* TODO: maybe someone will try to escape the '}' */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5541 | expansion = 0; |
| 5542 | first_char = true; |
| 5543 | all_digits = false; |
| 5544 | while (1) { |
| 5545 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5546 | nommu_addchr(as_string, ch); |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5547 | if (ch == '}') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5548 | break; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5549 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5550 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5551 | if (first_char) { |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5552 | if (ch == '#') { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5553 | /* ${#var}: length of var contents */ |
| 5554 | goto char_ok; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5555 | } |
| 5556 | if (isdigit(ch)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5557 | all_digits = true; |
| 5558 | goto char_ok; |
| 5559 | } |
Mike Frysinger | dc3bc40 | 2009-06-01 14:09:09 -0400 | [diff] [blame] | 5560 | /* They're being verbose and doing ${?} */ |
| 5561 | if (i_peek(input) == '}' && strchr("$!?#*@_", ch)) |
| 5562 | goto char_ok; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5563 | } |
| 5564 | |
| 5565 | if (expansion < 2 |
| 5566 | && ( (all_digits && !isdigit(ch)) |
| 5567 | || (!all_digits && !isalnum(ch) && ch != '_') |
| 5568 | ) |
| 5569 | ) { |
| 5570 | /* handle parameter expansions |
| 5571 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 5572 | */ |
| 5573 | if (first_char) |
| 5574 | goto case_default; |
| 5575 | switch (ch) { |
| 5576 | case ':': /* null modifier */ |
| 5577 | if (expansion == 0) { |
| 5578 | debug_printf_parse(": null modifier\n"); |
| 5579 | ++expansion; |
| 5580 | break; |
| 5581 | } |
| 5582 | goto case_default; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5583 | case '#': /* remove prefix */ |
| 5584 | case '%': /* remove suffix */ |
| 5585 | if (expansion == 0) { |
| 5586 | debug_printf_parse(": remove suffix/prefix\n"); |
| 5587 | expansion = 2; |
| 5588 | break; |
| 5589 | } |
| 5590 | goto case_default; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5591 | case '-': /* default value */ |
| 5592 | case '=': /* assign default */ |
| 5593 | case '+': /* alternative */ |
| 5594 | case '?': /* error indicate */ |
| 5595 | debug_printf_parse(": parameter expansion\n"); |
| 5596 | expansion = 2; |
| 5597 | break; |
| 5598 | default: |
| 5599 | case_default: |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5600 | syntax_error_unterm_str("${name}"); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5601 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 5602 | return 1; |
| 5603 | } |
| 5604 | } |
| 5605 | char_ok: |
| 5606 | debug_printf_parse(": '%c'\n", ch); |
| 5607 | o_addchr(dest, ch | quote_mask); |
| 5608 | quote_mask = 0; |
| 5609 | first_char = false; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5610 | } /* while (1) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5611 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5612 | break; |
| 5613 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5614 | #if (ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK) |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5615 | case '(': { |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5616 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5617 | int pos; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5618 | # endif |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5619 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5620 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5621 | # if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5622 | if (i_peek(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5623 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5624 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5625 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5626 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5627 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5628 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5629 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5630 | add_till_closing_paren(dest, input, true); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5631 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5632 | if (as_string) { |
| 5633 | o_addstr(as_string, dest->data + pos); |
| 5634 | o_addchr(as_string, ')'); |
| 5635 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5636 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5637 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5638 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5639 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5640 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5641 | # endif |
| 5642 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5643 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5644 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5645 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5646 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5647 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5648 | add_till_closing_paren(dest, input, false); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5649 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5650 | if (as_string) { |
| 5651 | o_addstr(as_string, dest->data + pos); |
| 5652 | o_addchr(as_string, '`'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5653 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5654 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5655 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5656 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5657 | break; |
| 5658 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5659 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5660 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5661 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5662 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5663 | ch = i_peek(input); |
| 5664 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 5665 | ch = '_'; |
| 5666 | goto make_var; |
| 5667 | } |
| 5668 | /* else: it's $_ */ |
| 5669 | /* TODO: */ |
| 5670 | /* $_ Shell or shell script name; or last cmd name */ |
| 5671 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 5672 | default: |
| 5673 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5674 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5675 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5676 | return 0; |
| 5677 | } |
| 5678 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5679 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5680 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5681 | parse_stream_dquoted(dest, input, dquote_end) |
| 5682 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5683 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5684 | o_string *dest, |
| 5685 | struct in_str *input, |
| 5686 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5687 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5688 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5689 | int next; |
| 5690 | |
| 5691 | again: |
| 5692 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5693 | if (ch != EOF) |
| 5694 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5695 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5696 | if (dest->o_assignment == NOT_ASSIGNMENT) |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5697 | dest->o_escape ^= 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5698 | debug_printf_parse("parse_stream_dquoted return 0\n"); |
| 5699 | return 0; |
| 5700 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5701 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5702 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5703 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5704 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5705 | } |
| 5706 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5707 | if (ch != '\n') { |
| 5708 | next = i_peek(input); |
| 5709 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5710 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 5711 | ch, ch, dest->o_escape); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5712 | if (ch == '\\') { |
| 5713 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5714 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5715 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5716 | } |
| 5717 | /* bash: |
| 5718 | * "The backslash retains its special meaning [in "..."] |
| 5719 | * only when followed by one of the following characters: |
| 5720 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 5721 | * within double quotes by preceding it with a backslash." |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5722 | */ |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 5723 | if (strchr("$`\"\\\n", next) != NULL) { |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 5724 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 5725 | if (ch != '\n') { |
| 5726 | o_addqchr(dest, ch); |
| 5727 | nommu_addchr(as_string, ch); |
| 5728 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5729 | } else { |
| 5730 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 5731 | nommu_addchr(as_string, '\\'); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5732 | } |
| 5733 | goto again; |
| 5734 | } |
| 5735 | if (ch == '$') { |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5736 | if (handle_dollar(as_string, dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5737 | debug_printf_parse("parse_stream_dquoted return 1: " |
| 5738 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5739 | return 1; |
| 5740 | } |
| 5741 | goto again; |
| 5742 | } |
| 5743 | #if ENABLE_HUSH_TICK |
| 5744 | if (ch == '`') { |
| 5745 | //int pos = dest->length; |
| 5746 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5747 | o_addchr(dest, 0x80 | '`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5748 | add_till_backquote(dest, input); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5749 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5750 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5751 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5752 | } |
| 5753 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5754 | o_addQchr(dest, ch); |
| 5755 | if (ch == '=' |
| 5756 | && (dest->o_assignment == MAYBE_ASSIGNMENT |
| 5757 | || dest->o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5758 | && is_well_formed_var_name(dest->data, '=') |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5759 | ) { |
| 5760 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 5761 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5762 | goto again; |
| 5763 | } |
| 5764 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5765 | /* |
| 5766 | * Scan input until EOF or end_trigger char. |
| 5767 | * Return a list of pipes to execute, or NULL on EOF |
| 5768 | * or if end_trigger character is met. |
| 5769 | * On syntax error, exit is shell is not interactive, |
| 5770 | * reset parsing machinery and start parsing anew, |
| 5771 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5772 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5773 | static struct pipe *parse_stream(char **pstring, |
| 5774 | struct in_str *input, |
| 5775 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5776 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5777 | struct parse_context ctx; |
| 5778 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5779 | int is_in_dquote; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5780 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5781 | |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5782 | /* Double-quote state is handled in the state variable is_in_dquote. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5783 | * 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] | 5784 | * found. When recursing, quote state is passed in via dest->o_escape. |
| 5785 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5786 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
| 5787 | end_trigger ? : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5788 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5789 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5790 | G.ifs = get_local_var_value("IFS"); |
| 5791 | if (G.ifs == NULL) |
| 5792 | G.ifs = " \t\n"; |
| 5793 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5794 | reset: |
| 5795 | #if ENABLE_HUSH_INTERACTIVE |
| 5796 | input->promptmode = 0; /* PS1 */ |
| 5797 | #endif |
| 5798 | /* dest.o_assignment = MAYBE_ASSIGNMENT; - already is */ |
| 5799 | initialize_context(&ctx); |
| 5800 | is_in_dquote = 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5801 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5802 | while (1) { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5803 | const char *is_ifs; |
| 5804 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5805 | int ch; |
| 5806 | int next; |
| 5807 | int redir_fd; |
| 5808 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5809 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5810 | if (is_in_dquote) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5811 | /* dest.o_quoted = 1; - already is (see below) */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5812 | if (parse_stream_dquoted(&ctx.as_string, &dest, input, '"')) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5813 | goto parse_error; |
| 5814 | } |
| 5815 | /* We reached closing '"' */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5816 | is_in_dquote = 0; |
| 5817 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5818 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5819 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 5820 | ch, ch, dest.o_escape); |
| 5821 | if (ch == EOF) { |
| 5822 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5823 | |
| 5824 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5825 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5826 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5827 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5828 | /* end_trigger == '}' case errors out earlier, |
| 5829 | * checking only ')' */ |
| 5830 | if (end_trigger == ')') { |
| 5831 | syntax_error_unterm_ch('('); /* exits */ |
| 5832 | /* goto parse_error; */ |
| 5833 | } |
| 5834 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5835 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5836 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5837 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5838 | o_free(&dest); |
| 5839 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5840 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5841 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5842 | /* (this makes bare "&" cmd a no-op. |
| 5843 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5844 | if (pi->num_cmds == 0 |
| 5845 | IF_HAS_KEYWORDS( && pi->res_word == RES_NONE) |
| 5846 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5847 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5848 | pi = NULL; |
| 5849 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5850 | #if !BB_MMU |
| 5851 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 5852 | if (pstring) |
| 5853 | *pstring = ctx.as_string.data; |
| 5854 | else |
| 5855 | o_free_unsafe(&ctx.as_string); |
| 5856 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5857 | debug_leave(); |
| 5858 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5859 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5860 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5861 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5862 | is_ifs = strchr(G.ifs, ch); |
| 5863 | is_special = strchr("<>;&|(){}#'" /* special outside of "str" */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 5864 | "\\$\"" IF_HUSH_TICK("`") /* always special */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5865 | , ch); |
| 5866 | |
| 5867 | if (!is_special && !is_ifs) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5868 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5869 | o_addQchr(&dest, ch); |
| 5870 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 5871 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5872 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5873 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5874 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5875 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5876 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5877 | continue; |
| 5878 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5879 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5880 | if (is_ifs) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5881 | if (done_word(&dest, &ctx)) { |
| 5882 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 5883 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5884 | if (ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5885 | #if ENABLE_HUSH_CASE |
| 5886 | /* "case ... in <newline> word) ..." - |
| 5887 | * newlines are ignored (but ';' wouldn't be) */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5888 | if (ctx.command->argv == NULL |
| 5889 | && ctx.ctx_res_w == RES_MATCH |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5890 | ) { |
| 5891 | continue; |
| 5892 | } |
| 5893 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5894 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5895 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5896 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 5897 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5898 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5899 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5900 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5901 | heredoc_cnt = 0; |
| 5902 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5903 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5904 | ch = ';'; |
Denis Vlasenko | 7c98612 | 2009-04-04 12:15:42 +0000 | [diff] [blame] | 5905 | /* note: if (is_ifs) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5906 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5907 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5908 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5909 | |
| 5910 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 5911 | * } is an ordinary char in this case, even inside { cmd; } |
| 5912 | * Pathological example: { ""}; } should exec "}" cmd |
| 5913 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5914 | if (ch == '}') { |
| 5915 | if (!IS_NULL_CMD(ctx.command) /* cmd } */ |
| 5916 | || dest.length != 0 /* word} */ |
| 5917 | || dest.o_quoted /* ""} */ |
| 5918 | ) { |
| 5919 | goto ordinary_char; |
| 5920 | } |
| 5921 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
| 5922 | goto skip_end_trigger; |
| 5923 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5924 | } |
| 5925 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5926 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5927 | && (ch != ';' || heredoc_cnt == 0) |
| 5928 | #if ENABLE_HUSH_CASE |
| 5929 | && (ch != ')' |
| 5930 | || ctx.ctx_res_w != RES_MATCH |
| 5931 | || (!dest.o_quoted && strcmp(dest.data, "esac") == 0) |
| 5932 | ) |
| 5933 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5934 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5935 | if (heredoc_cnt) { |
| 5936 | /* This is technically valid: |
| 5937 | * { cat <<HERE; }; echo Ok |
| 5938 | * heredoc |
| 5939 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5940 | * HERE |
| 5941 | * but we don't support this. |
| 5942 | * We require heredoc to be in enclosing {}/(), |
| 5943 | * if any. |
| 5944 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5945 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5946 | goto parse_error; |
| 5947 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5948 | if (done_word(&dest, &ctx)) { |
| 5949 | goto parse_error; |
| 5950 | } |
| 5951 | done_pipe(&ctx, PIPE_SEQ); |
| 5952 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5953 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5954 | if (!HAS_KEYWORDS |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5955 | 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] | 5956 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5957 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5958 | #if !BB_MMU |
| 5959 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 5960 | if (pstring) |
| 5961 | *pstring = ctx.as_string.data; |
| 5962 | else |
| 5963 | o_free_unsafe(&ctx.as_string); |
| 5964 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5965 | debug_leave(); |
| 5966 | debug_printf_parse("parse_stream return %p: " |
| 5967 | "end_trigger char found\n", |
| 5968 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5969 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5970 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5971 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5972 | skip_end_trigger: |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5973 | if (is_ifs) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5974 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5975 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5976 | next = '\0'; |
| 5977 | if (ch != '\n') { |
| 5978 | next = i_peek(input); |
| 5979 | } |
| 5980 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5981 | /* Catch <, > before deciding whether this word is |
| 5982 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 5983 | switch (ch) { |
| 5984 | case '>': |
| 5985 | redir_fd = redirect_opt_num(&dest); |
| 5986 | if (done_word(&dest, &ctx)) { |
| 5987 | goto parse_error; |
| 5988 | } |
| 5989 | redir_style = REDIRECT_OVERWRITE; |
| 5990 | if (next == '>') { |
| 5991 | redir_style = REDIRECT_APPEND; |
| 5992 | ch = i_getch(input); |
| 5993 | nommu_addchr(&ctx.as_string, ch); |
| 5994 | } |
| 5995 | #if 0 |
| 5996 | else if (next == '(') { |
| 5997 | syntax_error(">(process) not supported"); |
| 5998 | goto parse_error; |
| 5999 | } |
| 6000 | #endif |
| 6001 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 6002 | goto parse_error; |
| 6003 | continue; /* back to top of while (1) */ |
| 6004 | case '<': |
| 6005 | redir_fd = redirect_opt_num(&dest); |
| 6006 | if (done_word(&dest, &ctx)) { |
| 6007 | goto parse_error; |
| 6008 | } |
| 6009 | redir_style = REDIRECT_INPUT; |
| 6010 | if (next == '<') { |
| 6011 | redir_style = REDIRECT_HEREDOC; |
| 6012 | heredoc_cnt++; |
| 6013 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 6014 | ch = i_getch(input); |
| 6015 | nommu_addchr(&ctx.as_string, ch); |
| 6016 | } else if (next == '>') { |
| 6017 | redir_style = REDIRECT_IO; |
| 6018 | ch = i_getch(input); |
| 6019 | nommu_addchr(&ctx.as_string, ch); |
| 6020 | } |
| 6021 | #if 0 |
| 6022 | else if (next == '(') { |
| 6023 | syntax_error("<(process) not supported"); |
| 6024 | goto parse_error; |
| 6025 | } |
| 6026 | #endif |
| 6027 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 6028 | goto parse_error; |
| 6029 | continue; /* back to top of while (1) */ |
| 6030 | } |
| 6031 | |
| 6032 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 6033 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 6034 | && !ctx.pending_redirect |
| 6035 | ) { |
| 6036 | /* ch is a special char and thus this word |
| 6037 | * cannot be an assignment */ |
| 6038 | dest.o_assignment = NOT_ASSIGNMENT; |
| 6039 | } |
| 6040 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6041 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 6042 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6043 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6044 | case '#': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6045 | if (dest.length == 0) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6046 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6047 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6048 | if (ch == EOF || ch == '\n') |
| 6049 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6050 | i_getch(input); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6051 | /* note: we do not add it to &ctx.as_string */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6052 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6053 | nommu_addchr(&ctx.as_string, '\n'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6054 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6055 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6056 | } |
| 6057 | break; |
| 6058 | case '\\': |
| 6059 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6060 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6061 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6062 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6063 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6064 | if (ch != '\n') { |
| 6065 | o_addchr(&dest, '\\'); |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6066 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6067 | o_addchr(&dest, ch); |
| 6068 | nommu_addchr(&ctx.as_string, ch); |
| 6069 | /* Example: echo Hello \2>file |
| 6070 | * we need to know that word 2 is quoted */ |
| 6071 | dest.o_quoted = 1; |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6072 | } else { |
| 6073 | #if !BB_MMU |
| 6074 | /* It's "\<newline>". Remove trailing '\' from ctx.as_string */ |
| 6075 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
| 6076 | #endif |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6077 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6078 | break; |
| 6079 | case '$': |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6080 | if (handle_dollar(&ctx.as_string, &dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6081 | debug_printf_parse("parse_stream parse error: " |
| 6082 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6083 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6084 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6085 | break; |
| 6086 | case '\'': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6087 | dest.o_quoted = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6088 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6089 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6090 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6091 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6092 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6093 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6094 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6095 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6096 | break; |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 6097 | o_addqchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6098 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6099 | break; |
| 6100 | case '"': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6101 | dest.o_quoted = 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6102 | is_in_dquote ^= 1; /* invert */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6103 | if (dest.o_assignment == NOT_ASSIGNMENT) |
| 6104 | dest.o_escape ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6105 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 6106 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6107 | case '`': { |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6108 | #if !BB_MMU |
| 6109 | int pos; |
| 6110 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6111 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6112 | o_addchr(&dest, '`'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6113 | #if !BB_MMU |
| 6114 | pos = dest.length; |
| 6115 | #endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6116 | add_till_backquote(&dest, input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6117 | #if !BB_MMU |
| 6118 | o_addstr(&ctx.as_string, dest.data + pos); |
| 6119 | o_addchr(&ctx.as_string, '`'); |
| 6120 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6121 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6122 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6123 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6124 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 6125 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6126 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6127 | #if ENABLE_HUSH_CASE |
| 6128 | case_semi: |
| 6129 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6130 | if (done_word(&dest, &ctx)) { |
| 6131 | goto parse_error; |
| 6132 | } |
| 6133 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6134 | #if ENABLE_HUSH_CASE |
| 6135 | /* Eat multiple semicolons, detect |
| 6136 | * whether it means something special */ |
| 6137 | while (1) { |
| 6138 | ch = i_peek(input); |
| 6139 | if (ch != ';') |
| 6140 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6141 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6142 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 6143 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6144 | ctx.ctx_dsemicolon = 1; |
| 6145 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6146 | break; |
| 6147 | } |
| 6148 | } |
| 6149 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6150 | new_cmd: |
| 6151 | /* We just finished a cmd. New one may start |
| 6152 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6153 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6154 | break; |
| 6155 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6156 | if (done_word(&dest, &ctx)) { |
| 6157 | goto parse_error; |
| 6158 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6159 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6160 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6161 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6162 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6163 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6164 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6165 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6166 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6167 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6168 | if (done_word(&dest, &ctx)) { |
| 6169 | goto parse_error; |
| 6170 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 6171 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6172 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6173 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 6174 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6175 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6176 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6177 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6178 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6179 | } else { |
| 6180 | /* we could pick up a file descriptor choice here |
| 6181 | * with redirect_opt_num(), but bash doesn't do it. |
| 6182 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6183 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6184 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6185 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6186 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6187 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6188 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6189 | if (ctx.ctx_res_w == RES_MATCH |
| 6190 | && ctx.command->argv == NULL /* not (word|(... */ |
| 6191 | && dest.length == 0 /* not word(... */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6192 | && dest.o_quoted == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6193 | ) { |
| 6194 | continue; |
| 6195 | } |
| 6196 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6197 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6198 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 6199 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6200 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6201 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6202 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6203 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6204 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6205 | goto case_semi; |
| 6206 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6207 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 6208 | /* proper use of this character is caught by end_trigger: |
| 6209 | * if we see {, we call parse_group(..., end_trigger='}') |
| 6210 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 6211 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6212 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6213 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 6214 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 6215 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6216 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6217 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6218 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6219 | parse_error: |
| 6220 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6221 | struct parse_context *pctx; |
| 6222 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6223 | |
| 6224 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 6225 | * Sample for finding leaks on syntax error recovery path. |
| 6226 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6227 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 6228 | * Samples to catch leaks at execution: |
| 6229 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 6230 | * 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] | 6231 | */ |
| 6232 | pctx = &ctx; |
| 6233 | do { |
| 6234 | /* Update pipe/command counts, |
| 6235 | * otherwise freeing may miss some */ |
| 6236 | done_pipe(pctx, PIPE_SEQ); |
| 6237 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 6238 | pctx->list_head, pctx); |
| 6239 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6240 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6241 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6242 | #if !BB_MMU |
| 6243 | o_free_unsafe(&pctx->as_string); |
| 6244 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6245 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6246 | if (pctx != &ctx) { |
| 6247 | free(pctx); |
| 6248 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6249 | IF_HAS_KEYWORDS(pctx = p2;) |
| 6250 | } while (HAS_KEYWORDS && pctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6251 | /* Free text, clear all dest fields */ |
| 6252 | o_free(&dest); |
| 6253 | /* If we are not in top-level parse, we return, |
| 6254 | * our caller will propagate error. |
| 6255 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6256 | if (end_trigger != ';') { |
| 6257 | #if !BB_MMU |
| 6258 | if (pstring) |
| 6259 | *pstring = NULL; |
| 6260 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6261 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6262 | return ERR_PTR; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6263 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6264 | /* Discard cached input, force prompt */ |
| 6265 | input->p = NULL; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6266 | IF_HUSH_INTERACTIVE(input->promptme = 1;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6267 | goto reset; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6268 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6269 | } |
| 6270 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6271 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6272 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 6273 | * end_trigger controls how often we stop parsing |
| 6274 | * NUL: parse all, execute, return |
| 6275 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 6276 | */ |
| 6277 | 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] | 6278 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6279 | while (1) { |
| 6280 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6281 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6282 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6283 | if (!pipe_list) /* EOF */ |
| 6284 | break; |
| 6285 | debug_print_tree(pipe_list, 0); |
| 6286 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 6287 | run_and_free_list(pipe_list); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6288 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6289 | } |
| 6290 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6291 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6292 | { |
| 6293 | struct in_str input; |
| 6294 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6295 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6296 | } |
| 6297 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6298 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6299 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6300 | struct in_str input; |
| 6301 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6302 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6303 | } |
| 6304 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6305 | /* Called a few times only (or even once if "sh -c") */ |
| 6306 | static void block_signals(int second_time) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 6307 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6308 | unsigned sig; |
| 6309 | unsigned mask; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 6310 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6311 | mask = (1 << SIGQUIT); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6312 | if (G_interactive_fd) { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 6313 | mask = (1 << SIGQUIT) | SPECIAL_INTERACTIVE_SIGS; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6314 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6315 | mask |= SPECIAL_JOB_SIGS; |
| 6316 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6317 | G.non_DFL_mask = mask; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 6318 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6319 | if (!second_time) |
| 6320 | sigprocmask(SIG_SETMASK, NULL, &G.blocked_set); |
| 6321 | sig = 0; |
| 6322 | while (mask) { |
| 6323 | if (mask & 1) |
| 6324 | sigaddset(&G.blocked_set, sig); |
| 6325 | mask >>= 1; |
| 6326 | sig++; |
| 6327 | } |
| 6328 | sigdelset(&G.blocked_set, SIGCHLD); |
| 6329 | |
| 6330 | sigprocmask(SIG_SETMASK, &G.blocked_set, |
| 6331 | second_time ? NULL : &G.inherited_set); |
| 6332 | /* POSIX allows shell to re-enable SIGCHLD |
| 6333 | * even if it was SIG_IGN on entry */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 6334 | #if ENABLE_HUSH_FAST |
| 6335 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6336 | if (!second_time) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 6337 | signal(SIGCHLD, SIGCHLD_handler); |
| 6338 | #else |
| 6339 | if (!second_time) |
| 6340 | signal(SIGCHLD, SIG_DFL); |
| 6341 | #endif |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6342 | } |
| 6343 | |
| 6344 | #if ENABLE_HUSH_JOB |
| 6345 | /* helper */ |
| 6346 | static void maybe_set_to_sigexit(int sig) |
| 6347 | { |
| 6348 | void (*handler)(int); |
| 6349 | /* non_DFL_mask'ed signals are, well, masked, |
| 6350 | * no need to set handler for them. |
| 6351 | */ |
| 6352 | if (!((G.non_DFL_mask >> sig) & 1)) { |
| 6353 | handler = signal(sig, sigexit); |
| 6354 | if (handler == SIG_IGN) /* oops... restore back to IGN! */ |
| 6355 | signal(sig, handler); |
| 6356 | } |
| 6357 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6358 | /* Set handlers to restore tty pgrp and exit */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6359 | static void set_fatal_handlers(void) |
| 6360 | { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 6361 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6362 | if (HUSH_DEBUG) { |
| 6363 | maybe_set_to_sigexit(SIGILL ); |
| 6364 | maybe_set_to_sigexit(SIGFPE ); |
| 6365 | maybe_set_to_sigexit(SIGBUS ); |
| 6366 | maybe_set_to_sigexit(SIGSEGV); |
| 6367 | maybe_set_to_sigexit(SIGTRAP); |
| 6368 | } /* else: hush is perfect. what SEGV? */ |
| 6369 | maybe_set_to_sigexit(SIGABRT); |
| 6370 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 6371 | maybe_set_to_sigexit(SIGPIPE); |
| 6372 | maybe_set_to_sigexit(SIGALRM); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 6373 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are masked. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6374 | * if we aren't interactive... but in this case |
| 6375 | * 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] | 6376 | /*maybe_set_to_sigexit(SIGHUP );*/ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6377 | /*maybe_set_to_sigexit(SIGTERM);*/ |
| 6378 | /*maybe_set_to_sigexit(SIGINT );*/ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 6379 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 6380 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 6381 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6382 | static int set_mode(const char cstate, const char mode) |
| 6383 | { |
| 6384 | int state = (cstate == '-' ? 1 : 0); |
| 6385 | switch (mode) { |
| 6386 | case 'n': G.fake_mode = state; break; |
| 6387 | case 'x': /*G.debug_mode = state;*/ break; |
| 6388 | default: return EXIT_FAILURE; |
| 6389 | } |
| 6390 | return EXIT_SUCCESS; |
| 6391 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6392 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 6393 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 6394 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6395 | { |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6396 | static const struct variable const_shell_ver = { |
| 6397 | .next = NULL, |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 6398 | .varstr = (char*)hush_version_str, |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6399 | .max_len = 1, /* 0 can provoke free(name) */ |
| 6400 | .flg_export = 1, |
| 6401 | .flg_read_only = 1, |
| 6402 | }; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6403 | int signal_mask_is_inited = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6404 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6405 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6406 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6407 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 6408 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 6409 | INIT_G(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6410 | if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6411 | G.last_exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6412 | #if !BB_MMU |
| 6413 | G.argv0_for_re_execing = argv[0]; |
| 6414 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6415 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6416 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 6417 | G.top_var = &G.shell_ver; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6418 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6419 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 6420 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6421 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6422 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6423 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6424 | if (e) while (*e) { |
| 6425 | char *value = strchr(*e, '='); |
| 6426 | if (value) { /* paranoia */ |
| 6427 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 6428 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 6429 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6430 | cur_var->max_len = strlen(*e); |
| 6431 | cur_var->flg_export = 1; |
| 6432 | } |
| 6433 | e++; |
| 6434 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6435 | debug_printf_env("putenv '%s'\n", hush_version_str); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 6436 | putenv((char *)hush_version_str); /* reinstate HUSH_VERSION */ |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 6437 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6438 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 6439 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6440 | G.global_argc = argc; |
| 6441 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 6442 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 6443 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 6444 | |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 6445 | if (setjmp(die_jmp)) { |
| 6446 | /* xfunc has failed! die die die */ |
| 6447 | /* no EXIT traps, this is an escape hatch! */ |
| 6448 | G.exiting = 1; |
| 6449 | hush_exit(xfunc_error_retval); |
| 6450 | } |
| 6451 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6452 | /* Shell is non-interactive at first. We need to call |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6453 | * block_signals(0) if we are going to execute "sh <script>", |
| 6454 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6455 | * If we later decide that we are interactive, we run block_signals(0) |
| 6456 | * (or re-run block_signals(1) if we ran block_signals(0) before) |
| 6457 | * in order to intercept (more) signals. |
| 6458 | */ |
| 6459 | |
| 6460 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 6461 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6462 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6463 | while (1) { |
Denys Vlasenko | a67a962 | 2009-08-20 03:38:58 +0200 | [diff] [blame^] | 6464 | opt = getopt(argc, argv, "+c:xins" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6465 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 6466 | "<:$:R:V:" |
| 6467 | # if ENABLE_HUSH_FUNCTIONS |
| 6468 | "F:" |
| 6469 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6470 | #endif |
| 6471 | ); |
| 6472 | if (opt <= 0) |
| 6473 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6474 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6475 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6476 | /* Possibilities: |
| 6477 | * sh ... -c 'script' |
| 6478 | * sh ... -c 'script' ARG0 [ARG1...] |
| 6479 | * On NOMMU, if builtin_argc != 0, |
| 6480 | * sh ... -c 'builtin' [BARGV...] "" ARG0 [ARG1...] |
| 6481 | * "" needs to be replaced with NULL |
| 6482 | * and BARGV vector fed to builtin function. |
| 6483 | * Note: this form never happens: |
| 6484 | * sh ... -c 'builtin' [BARGV...] "" |
| 6485 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6486 | if (!G.root_pid) |
| 6487 | G.root_pid = getpid(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6488 | G.global_argv = argv + optind; |
| 6489 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6490 | if (builtin_argc) { |
| 6491 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 6492 | const struct built_in_command *x; |
| 6493 | |
| 6494 | block_signals(0); /* 0: called 1st time */ |
| 6495 | x = find_builtin(optarg); |
| 6496 | if (x) { /* paranoia */ |
| 6497 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 6498 | G.global_argv += builtin_argc; |
| 6499 | G.global_argv[-1] = NULL; /* replace "" */ |
| 6500 | G.last_exitcode = x->function(argv + optind - 1); |
| 6501 | } |
| 6502 | goto final_return; |
| 6503 | } |
| 6504 | if (!G.global_argv[0]) { |
| 6505 | /* -c 'script' (no params): prevent empty $0 */ |
| 6506 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 6507 | G.global_argv[0] = argv[0]; |
| 6508 | G.global_argc--; |
| 6509 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6510 | block_signals(0); /* 0: called 1st time */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6511 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6512 | goto final_return; |
| 6513 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 6514 | /* Well, we cannot just declare interactiveness, |
| 6515 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6516 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6517 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 6518 | case 's': |
| 6519 | /* "-s" means "read from stdin", but this is how we always |
| 6520 | * operate, so simply do nothing here. */ |
| 6521 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6522 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 6523 | case '<': /* "big heredoc" support */ |
| 6524 | full_write(STDOUT_FILENO, optarg, strlen(optarg)); |
| 6525 | _exit(0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6526 | case '$': |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6527 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 6528 | optarg++; |
| 6529 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 6530 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6531 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6532 | optarg++; |
| 6533 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6534 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6535 | optarg++; |
| 6536 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6537 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6538 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6539 | case 'R': |
| 6540 | case 'V': |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6541 | set_local_var(xstrdup(optarg), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ opt == 'R'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6542 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 6543 | # if ENABLE_HUSH_FUNCTIONS |
| 6544 | case 'F': { |
| 6545 | struct function *funcp = new_function(optarg); |
| 6546 | /* funcp->name is already set to optarg */ |
| 6547 | /* funcp->body is set to NULL. It's a special case. */ |
| 6548 | funcp->body_as_string = argv[optind]; |
| 6549 | optind++; |
| 6550 | break; |
| 6551 | } |
| 6552 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6553 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6554 | case 'n': |
| 6555 | case 'x': |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6556 | if (!set_mode('-', opt)) |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6557 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6558 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6559 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6560 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 6561 | " or: sh -c command [args]...\n\n"); |
| 6562 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6563 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6564 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6565 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6566 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6567 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6568 | |
| 6569 | if (!G.root_pid) |
| 6570 | G.root_pid = getpid(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6571 | |
| 6572 | /* If we are login shell... */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6573 | if (argv[0] && argv[0][0] == '-') { |
| 6574 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6575 | debug_printf("sourcing /etc/profile\n"); |
| 6576 | input = fopen_for_read("/etc/profile"); |
| 6577 | if (input != NULL) { |
| 6578 | close_on_exec_on(fileno(input)); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6579 | block_signals(0); /* 0: called 1st time */ |
| 6580 | signal_mask_is_inited = 1; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6581 | parse_and_run_file(input); |
| 6582 | fclose(input); |
| 6583 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6584 | /* bash: after sourcing /etc/profile, |
| 6585 | * tries to source (in the given order): |
| 6586 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 6587 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6588 | * bash also sources ~/.bash_logout on exit. |
| 6589 | * If called as sh, skips .bash_XXX files. |
| 6590 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6591 | } |
| 6592 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6593 | if (argv[optind]) { |
| 6594 | FILE *input; |
| 6595 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6596 | * "bash <script>" (which is never interactive (unless -i?)) |
| 6597 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6598 | * If called as sh, does the same but with $ENV. |
| 6599 | */ |
| 6600 | debug_printf("running script '%s'\n", argv[optind]); |
| 6601 | G.global_argv = argv + optind; |
| 6602 | G.global_argc = argc - optind; |
| 6603 | input = xfopen_for_read(argv[optind]); |
| 6604 | close_on_exec_on(fileno(input)); |
| 6605 | if (!signal_mask_is_inited) |
| 6606 | block_signals(0); /* 0: called 1st time */ |
| 6607 | parse_and_run_file(input); |
| 6608 | #if ENABLE_FEATURE_CLEAN_UP |
| 6609 | fclose(input); |
| 6610 | #endif |
| 6611 | goto final_return; |
| 6612 | } |
| 6613 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6614 | /* Up to here, shell was non-interactive. Now it may become one. |
| 6615 | * NB: don't forget to (re)run block_signals(0/1) as needed. |
| 6616 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6617 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 6618 | /* A shell is interactive if the '-i' flag was given, |
| 6619 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 6620 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6621 | * no arguments remaining or the -s flag given |
| 6622 | * standard input is a terminal |
| 6623 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6624 | * Refer to Posix.2, the description of the 'sh' utility. |
| 6625 | */ |
| 6626 | #if ENABLE_HUSH_JOB |
| 6627 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6628 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 6629 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 6630 | if (G_saved_tty_pgrp < 0) |
| 6631 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6632 | |
| 6633 | /* try to dup stdin to high fd#, >= 255 */ |
| 6634 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 6635 | if (G_interactive_fd < 0) { |
| 6636 | /* try to dup to any fd */ |
| 6637 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6638 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6639 | /* give up */ |
| 6640 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6641 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 6642 | } |
| 6643 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6644 | // TODO: track & disallow any attempts of user |
| 6645 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6646 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6647 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6648 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6649 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6650 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6651 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6652 | /* If we were run as 'hush &', sleep until we are |
| 6653 | * in the foreground (tty pgrp == our pgrp). |
| 6654 | * If we get started under a job aware app (like bash), |
| 6655 | * make sure we are now in charge so we don't fight over |
| 6656 | * who gets the foreground */ |
| 6657 | while (1) { |
| 6658 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6659 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 6660 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6661 | break; |
| 6662 | /* send TTIN to ourself (should stop us) */ |
| 6663 | kill(- shell_pgrp, SIGTTIN); |
| 6664 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6665 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6666 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6667 | /* Block some signals */ |
| 6668 | block_signals(signal_mask_is_inited); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6669 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6670 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6671 | /* Set other signals to restore saved_tty_pgrp */ |
| 6672 | set_fatal_handlers(); |
| 6673 | /* Put ourselves in our own process group |
| 6674 | * (bash, too, does this only if ctty is available) */ |
| 6675 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 6676 | /* Grab control of the terminal */ |
| 6677 | tcsetpgrp(G_interactive_fd, getpid()); |
| 6678 | } |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 6679 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 6680 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6681 | enable_restore_tty_pgrp_on_exit(); /* sets die_sleep = -1 */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6682 | } else if (!signal_mask_is_inited) { |
| 6683 | block_signals(0); /* 0: called 1st time */ |
| 6684 | } /* else: block_signals(0) was done before */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6685 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6686 | /* No job control compiled in, only prompt/line editing */ |
| 6687 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6688 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 6689 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6690 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6691 | G_interactive_fd = dup(STDIN_FILENO); |
| 6692 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6693 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6694 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6695 | } |
| 6696 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6697 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6698 | close_on_exec_on(G_interactive_fd); |
| 6699 | block_signals(signal_mask_is_inited); |
| 6700 | } else if (!signal_mask_is_inited) { |
| 6701 | block_signals(0); |
| 6702 | } |
| 6703 | #else |
| 6704 | /* We have interactiveness code disabled */ |
| 6705 | if (!signal_mask_is_inited) { |
| 6706 | block_signals(0); |
| 6707 | } |
| 6708 | #endif |
| 6709 | /* bash: |
| 6710 | * if interactive but not a login shell, sources ~/.bashrc |
| 6711 | * (--norc turns this off, --rcfile <file> overrides) |
| 6712 | */ |
| 6713 | |
| 6714 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Mike Frysinger | 258275d | 2009-04-05 21:19:43 +0000 | [diff] [blame] | 6715 | printf("\n\n%s hush - the humble shell\n", bb_banner); |
Mike Frysinger | 26cf283 | 2009-04-24 06:40:30 +0000 | [diff] [blame] | 6716 | if (ENABLE_HUSH_HELP) |
| 6717 | puts("Enter 'help' for a list of built-in commands."); |
| 6718 | puts(""); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 6719 | } |
| 6720 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6721 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6722 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6723 | final_return: |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 6724 | #if ENABLE_FEATURE_CLEAN_UP |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6725 | if (G.cwd != bb_msg_unknown) |
| 6726 | free((char*)G.cwd); |
| 6727 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6728 | while (cur_var) { |
| 6729 | struct variable *tmp = cur_var; |
| 6730 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 6731 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6732 | cur_var = cur_var->next; |
| 6733 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 6734 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6735 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6736 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6737 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 6738 | |
| 6739 | |
| 6740 | #if ENABLE_LASH |
| 6741 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 6742 | int lash_main(int argc, char **argv) |
| 6743 | { |
| 6744 | //bb_error_msg("lash is deprecated, please use hush instead"); |
| 6745 | return hush_main(argc, argv); |
| 6746 | } |
| 6747 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6748 | |
| 6749 | |
| 6750 | /* |
| 6751 | * Built-ins |
| 6752 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6753 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6754 | { |
| 6755 | return 0; |
| 6756 | } |
| 6757 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6758 | static int FAST_FUNC builtin_test(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6759 | { |
| 6760 | int argc = 0; |
| 6761 | while (*argv) { |
| 6762 | argc++; |
| 6763 | argv++; |
| 6764 | } |
| 6765 | return test_main(argc, argv - argc); |
| 6766 | } |
| 6767 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6768 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6769 | { |
| 6770 | int argc = 0; |
| 6771 | while (*argv) { |
| 6772 | argc++; |
| 6773 | argv++; |
| 6774 | } |
| 6775 | return echo_main(argc, argv - argc); |
| 6776 | } |
| 6777 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6778 | static int FAST_FUNC builtin_eval(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6779 | { |
| 6780 | int rcode = EXIT_SUCCESS; |
| 6781 | |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6782 | if (*++argv) { |
| 6783 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6784 | /* bash: |
| 6785 | * eval "echo Hi; done" ("done" is syntax error): |
| 6786 | * "echo Hi" will not execute too. |
| 6787 | */ |
| 6788 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6789 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6790 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6791 | } |
| 6792 | return rcode; |
| 6793 | } |
| 6794 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6795 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6796 | { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6797 | const char *newdir = argv[1]; |
| 6798 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6799 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6800 | * bash says "bash: cd: HOME not set" and does nothing |
| 6801 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6802 | */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 6803 | newdir = get_local_var_value("HOME") ? : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6804 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6805 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6806 | /* Mimic bash message exactly */ |
| 6807 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6808 | return EXIT_FAILURE; |
| 6809 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6810 | get_cwd(1); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6811 | return EXIT_SUCCESS; |
| 6812 | } |
| 6813 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6814 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6815 | { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6816 | if (*++argv == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6817 | return EXIT_SUCCESS; /* bash does this */ |
| 6818 | { |
| 6819 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6820 | nommu_save_t dummy; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6821 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 6822 | /* TODO: if exec fails, bash does NOT exit! We do... */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6823 | pseudo_exec_argv(&dummy, argv, 0, NULL); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6824 | /* never returns */ |
| 6825 | } |
| 6826 | } |
| 6827 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6828 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6829 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 6830 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 6831 | |
| 6832 | /* interactive bash: |
| 6833 | * # trap "echo EEE" EXIT |
| 6834 | * # exit |
| 6835 | * exit |
| 6836 | * There are stopped jobs. |
| 6837 | * (if there are _stopped_ jobs, running ones don't count) |
| 6838 | * # exit |
| 6839 | * exit |
| 6840 | # EEE (then bash exits) |
| 6841 | * |
| 6842 | * we can use G.exiting = -1 as indicator "last cmd was exit" |
| 6843 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 6844 | |
| 6845 | /* note: EXIT trap is run by hush_exit */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6846 | if (*++argv == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6847 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6848 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 6849 | xfunc_error_retval = 255; |
| 6850 | /* bash: exit -2 == exit 254, no error msg */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6851 | hush_exit(xatoi(*argv) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6852 | } |
| 6853 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6854 | static void print_escaped(const char *s) |
| 6855 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6856 | if (*s == '\'') |
| 6857 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6858 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6859 | const char *p = strchrnul(s, '\''); |
| 6860 | /* print 'xxxx', possibly just '' */ |
| 6861 | printf("'%.*s'", (int)(p - s), s); |
| 6862 | if (*p == '\0') |
| 6863 | break; |
| 6864 | s = p; |
| 6865 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6866 | /* s points to '; print "'''...'''" */ |
| 6867 | putchar('"'); |
| 6868 | do putchar('\''); while (*++s == '\''); |
| 6869 | putchar('"'); |
| 6870 | } while (*s); |
| 6871 | } |
| 6872 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6873 | #if !ENABLE_HUSH_LOCAL |
| 6874 | #define helper_export_local(argv, exp, lvl) \ |
| 6875 | helper_export_local(argv, exp) |
| 6876 | #endif |
| 6877 | static void helper_export_local(char **argv, int exp, int lvl) |
| 6878 | { |
| 6879 | do { |
| 6880 | char *name = *argv; |
| 6881 | |
| 6882 | /* So far we do not check that name is valid (TODO?) */ |
| 6883 | |
| 6884 | if (strchr(name, '=') == NULL) { |
| 6885 | struct variable *var; |
| 6886 | |
| 6887 | var = get_local_var(name); |
| 6888 | if (exp == -1) { /* unexporting? */ |
| 6889 | /* export -n NAME (without =VALUE) */ |
| 6890 | if (var) { |
| 6891 | var->flg_export = 0; |
| 6892 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 6893 | unsetenv(name); |
| 6894 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 6895 | continue; |
| 6896 | } |
| 6897 | if (exp == 1) { /* exporting? */ |
| 6898 | /* export NAME (without =VALUE) */ |
| 6899 | if (var) { |
| 6900 | var->flg_export = 1; |
| 6901 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 6902 | putenv(var->varstr); |
| 6903 | continue; |
| 6904 | } |
| 6905 | } |
| 6906 | /* Exporting non-existing variable. |
| 6907 | * bash does not put it in environment, |
| 6908 | * but remembers that it is exported, |
| 6909 | * and does put it in env when it is set later. |
| 6910 | * We just set it to "" and export. */ |
| 6911 | /* Or, it's "local NAME" (without =VALUE). |
| 6912 | * bash sets the value to "". */ |
| 6913 | name = xasprintf("%s=", name); |
| 6914 | } else { |
| 6915 | /* (Un)exporting/making local NAME=VALUE */ |
| 6916 | name = xstrdup(name); |
| 6917 | } |
| 6918 | set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0); |
| 6919 | } while (*++argv); |
| 6920 | } |
| 6921 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6922 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6923 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 6924 | unsigned opt_unexport; |
| 6925 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 6926 | #if ENABLE_HUSH_EXPORT_N |
| 6927 | /* "!": do not abort on errors */ |
| 6928 | opt_unexport = getopt32(argv, "!n"); |
| 6929 | if (opt_unexport == (uint32_t)-1) |
| 6930 | return EXIT_FAILURE; |
| 6931 | argv += optind; |
| 6932 | #else |
| 6933 | opt_unexport = 0; |
| 6934 | argv++; |
| 6935 | #endif |
| 6936 | |
| 6937 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6938 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6939 | if (e) { |
| 6940 | while (*e) { |
| 6941 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6942 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6943 | #else |
| 6944 | /* ash emits: export VAR='VAL' |
| 6945 | * bash: declare -x VAR="VAL" |
| 6946 | * we follow ash example */ |
| 6947 | const char *s = *e++; |
| 6948 | const char *p = strchr(s, '='); |
| 6949 | |
| 6950 | if (!p) /* wtf? take next variable */ |
| 6951 | continue; |
| 6952 | /* export var= */ |
| 6953 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6954 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6955 | putchar('\n'); |
| 6956 | #endif |
| 6957 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6958 | /*fflush(stdout); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6959 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6960 | return EXIT_SUCCESS; |
| 6961 | } |
| 6962 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6963 | helper_export_local(argv, (opt_unexport ? -1 : 1), 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6964 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6965 | return EXIT_SUCCESS; |
| 6966 | } |
| 6967 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6968 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6969 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6970 | { |
| 6971 | if (G.func_nest_level == 0) { |
| 6972 | bb_error_msg("%s: not in a function", argv[0]); |
| 6973 | return EXIT_FAILURE; /* bash compat */ |
| 6974 | } |
| 6975 | helper_export_local(argv, 0, G.func_nest_level); |
| 6976 | return EXIT_SUCCESS; |
| 6977 | } |
| 6978 | #endif |
| 6979 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6980 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6981 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6982 | int sig; |
| 6983 | char *new_cmd; |
| 6984 | |
| 6985 | if (!G.traps) |
| 6986 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 6987 | |
| 6988 | argv++; |
| 6989 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 6990 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6991 | /* No args: print all trapped */ |
| 6992 | for (i = 0; i < NSIG; ++i) { |
| 6993 | if (G.traps[i]) { |
| 6994 | printf("trap -- "); |
| 6995 | print_escaped(G.traps[i]); |
| 6996 | printf(" %s\n", get_signame(i)); |
| 6997 | } |
| 6998 | } |
| 6999 | /*fflush(stdout); - done after each builtin anyway */ |
| 7000 | return EXIT_SUCCESS; |
| 7001 | } |
| 7002 | |
| 7003 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7004 | /* If first arg is a number: reset all specified signals */ |
| 7005 | sig = bb_strtou(*argv, NULL, 10); |
| 7006 | if (errno == 0) { |
| 7007 | int ret; |
| 7008 | process_sig_list: |
| 7009 | ret = EXIT_SUCCESS; |
| 7010 | while (*argv) { |
| 7011 | sig = get_signum(*argv++); |
| 7012 | if (sig < 0 || sig >= NSIG) { |
| 7013 | ret = EXIT_FAILURE; |
| 7014 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7015 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7016 | continue; |
| 7017 | } |
| 7018 | |
| 7019 | free(G.traps[sig]); |
| 7020 | G.traps[sig] = xstrdup(new_cmd); |
| 7021 | |
| 7022 | debug_printf("trap: setting SIG%s (%i) to '%s'", |
| 7023 | get_signame(sig), sig, G.traps[sig]); |
| 7024 | |
| 7025 | /* There is no signal for 0 (EXIT) */ |
| 7026 | if (sig == 0) |
| 7027 | continue; |
| 7028 | |
| 7029 | if (new_cmd) { |
| 7030 | sigaddset(&G.blocked_set, sig); |
| 7031 | } else { |
| 7032 | /* There was a trap handler, we are removing it |
| 7033 | * (if sig has non-DFL handling, |
| 7034 | * we don't need to do anything) */ |
| 7035 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 7036 | continue; |
| 7037 | sigdelset(&G.blocked_set, sig); |
| 7038 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7039 | } |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7040 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7041 | return ret; |
| 7042 | } |
| 7043 | |
| 7044 | if (!argv[1]) { /* no second arg */ |
| 7045 | bb_error_msg("trap: invalid arguments"); |
| 7046 | return EXIT_FAILURE; |
| 7047 | } |
| 7048 | |
| 7049 | /* First arg is "-": reset all specified to default */ |
| 7050 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 7051 | /* Everything else: set arg as signal handler |
| 7052 | * (includes "" case, which ignores signal) */ |
| 7053 | if (argv[0][0] == '-') { |
| 7054 | if (argv[0][1] == '\0') { /* "-" */ |
| 7055 | /* new_cmd remains NULL: "reset these sigs" */ |
| 7056 | goto reset_traps; |
| 7057 | } |
| 7058 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 7059 | argv++; |
| 7060 | } |
| 7061 | /* else: "-something", no special meaning */ |
| 7062 | } |
| 7063 | new_cmd = *argv; |
| 7064 | reset_traps: |
| 7065 | argv++; |
| 7066 | goto process_sig_list; |
| 7067 | } |
| 7068 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7069 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7070 | static int FAST_FUNC builtin_type(char **argv) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7071 | { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7072 | int ret = EXIT_SUCCESS; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7073 | |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7074 | while (*++argv) { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7075 | const char *type; |
Denys Vlasenko | 171932d | 2009-05-28 17:07:22 +0200 | [diff] [blame] | 7076 | char *path = NULL; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7077 | |
| 7078 | if (0) {} /* make conditional compile easier below */ |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7079 | /*else if (find_alias(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7080 | type = "an alias";*/ |
| 7081 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7082 | else if (find_function(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7083 | type = "a function"; |
| 7084 | #endif |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7085 | else if (find_builtin(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7086 | type = "a shell builtin"; |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7087 | else if ((path = find_in_path(*argv)) != NULL) |
| 7088 | type = path; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7089 | else { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7090 | bb_error_msg("type: %s: not found", *argv); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7091 | ret = EXIT_FAILURE; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7092 | continue; |
| 7093 | } |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7094 | |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7095 | printf("%s is %s\n", *argv, type); |
| 7096 | free(path); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7097 | } |
| 7098 | |
| 7099 | return ret; |
| 7100 | } |
| 7101 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7102 | #if ENABLE_HUSH_JOB |
| 7103 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7104 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7105 | { |
| 7106 | int i, jobnum; |
| 7107 | struct pipe *pi; |
| 7108 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7109 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7110 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7111 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7112 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 7113 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7114 | for (pi = G.job_list; pi; pi = pi->next) { |
| 7115 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7116 | goto found; |
| 7117 | } |
| 7118 | } |
| 7119 | bb_error_msg("%s: no current job", argv[0]); |
| 7120 | return EXIT_FAILURE; |
| 7121 | } |
| 7122 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 7123 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 7124 | return EXIT_FAILURE; |
| 7125 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7126 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7127 | if (pi->jobid == jobnum) { |
| 7128 | goto found; |
| 7129 | } |
| 7130 | } |
| 7131 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 7132 | return EXIT_FAILURE; |
| 7133 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 7134 | /* TODO: bash prints a string representation |
| 7135 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7136 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7137 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7138 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7139 | } |
| 7140 | |
| 7141 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7142 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 7143 | for (i = 0; i < pi->num_cmds; i++) { |
| 7144 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 7145 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7146 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7147 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7148 | |
| 7149 | i = kill(- pi->pgrp, SIGCONT); |
| 7150 | if (i < 0) { |
| 7151 | if (errno == ESRCH) { |
| 7152 | delete_finished_bg_job(pi); |
| 7153 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7154 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7155 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7156 | } |
| 7157 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7158 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7159 | remove_bg_job(pi); |
| 7160 | return checkjobs_and_fg_shell(pi); |
| 7161 | } |
| 7162 | return EXIT_SUCCESS; |
| 7163 | } |
| 7164 | #endif |
| 7165 | |
| 7166 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7167 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7168 | { |
| 7169 | const struct built_in_command *x; |
| 7170 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7171 | printf( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7172 | "Built-in commands:\n" |
| 7173 | "------------------\n"); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7174 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
| 7175 | if (x->descr) |
| 7176 | printf("%s\t%s\n", x->cmd, x->descr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7177 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7178 | bb_putchar('\n'); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7179 | return EXIT_SUCCESS; |
| 7180 | } |
| 7181 | #endif |
| 7182 | |
| 7183 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7184 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7185 | { |
| 7186 | struct pipe *job; |
| 7187 | const char *status_string; |
| 7188 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7189 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7190 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7191 | status_string = "Stopped"; |
| 7192 | else |
| 7193 | status_string = "Running"; |
| 7194 | |
| 7195 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 7196 | } |
| 7197 | return EXIT_SUCCESS; |
| 7198 | } |
| 7199 | #endif |
| 7200 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7201 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7202 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7203 | { |
| 7204 | void *p; |
| 7205 | unsigned long l; |
| 7206 | |
| 7207 | /* Crude attempt to find where "free memory" starts, |
| 7208 | * sans fragmentation. */ |
| 7209 | p = malloc(240); |
| 7210 | l = (unsigned long)p; |
| 7211 | free(p); |
| 7212 | p = malloc(3400); |
| 7213 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 7214 | free(p); |
| 7215 | |
| 7216 | if (!G.memleak_value) |
| 7217 | G.memleak_value = l; |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 7218 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7219 | l -= G.memleak_value; |
| 7220 | if ((long)l < 0) |
| 7221 | l = 0; |
| 7222 | l /= 1024; |
| 7223 | if (l > 127) |
| 7224 | l = 127; |
| 7225 | |
| 7226 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 7227 | return l; |
| 7228 | } |
| 7229 | #endif |
| 7230 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7231 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7232 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7233 | puts(get_cwd(0)); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7234 | return EXIT_SUCCESS; |
| 7235 | } |
| 7236 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7237 | static int FAST_FUNC builtin_read(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7238 | { |
| 7239 | char *string; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 7240 | const char *name = "REPLY"; |
| 7241 | |
| 7242 | if (argv[1]) { |
| 7243 | name = argv[1]; |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 7244 | /* bash (3.2.33(1)) bug: "read 0abcd" will execute, |
| 7245 | * and _after_ that_ it will complain */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 7246 | if (!is_well_formed_var_name(name, '\0')) { |
| 7247 | /* Mimic bash message */ |
| 7248 | bb_error_msg("read: '%s': not a valid identifier", name); |
| 7249 | return 1; |
| 7250 | } |
| 7251 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7252 | |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 7253 | //TODO: bash unbackslashes input, splits words and puts them in argv[i] |
| 7254 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7255 | string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7256 | return set_local_var(string, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7257 | } |
| 7258 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7259 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 7260 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7261 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7262 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 7263 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7264 | * set -- [argument...] |
| 7265 | * set -o |
| 7266 | * set +o |
| 7267 | * Implementations shall support the options in both their hyphen and |
| 7268 | * plus-sign forms. These options can also be specified as options to sh. |
| 7269 | * Examples: |
| 7270 | * Write out all variables and their values: set |
| 7271 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 7272 | * Turn on the -x and -v options: set -xv |
| 7273 | * Unset all positional parameters: set -- |
| 7274 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 7275 | * Set the positional parameters to the expansion of x, even if x expands |
| 7276 | * with a leading '-' or '+': set -- $x |
| 7277 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7278 | * 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] | 7279 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7280 | static int FAST_FUNC builtin_set(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7281 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7282 | int n; |
| 7283 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7284 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7285 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7286 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7287 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7288 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7289 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7290 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7291 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7292 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7293 | do { |
| 7294 | if (!strcmp(arg, "--")) { |
| 7295 | ++argv; |
| 7296 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7297 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 7298 | if (arg[0] != '+' && arg[0] != '-') |
| 7299 | break; |
| 7300 | for (n = 1; arg[n]; ++n) |
| 7301 | if (set_mode(arg[0], arg[n])) |
| 7302 | goto error; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7303 | } while ((arg = *++argv) != NULL); |
| 7304 | /* Now argv[0] is 1st argument */ |
| 7305 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7306 | if (arg == NULL) |
| 7307 | return EXIT_SUCCESS; |
| 7308 | set_argv: |
| 7309 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7310 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 7311 | g_argv = G.global_argv; |
| 7312 | if (G.global_args_malloced) { |
| 7313 | pp = g_argv; |
| 7314 | while (*++pp) |
| 7315 | free(*pp); |
| 7316 | g_argv[1] = NULL; |
| 7317 | } else { |
| 7318 | G.global_args_malloced = 1; |
| 7319 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 7320 | pp[0] = g_argv[0]; /* retain $0 */ |
| 7321 | g_argv = pp; |
| 7322 | } |
| 7323 | /* This realloc's G.global_argv */ |
| 7324 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 7325 | |
| 7326 | n = 1; |
| 7327 | while (*++pp) |
| 7328 | n++; |
| 7329 | G.global_argc = n; |
| 7330 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7331 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7332 | |
| 7333 | /* Nothing known, so abort */ |
| 7334 | error: |
| 7335 | bb_error_msg("set: %s: invalid option", arg); |
| 7336 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7337 | } |
| 7338 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7339 | static int FAST_FUNC builtin_shift(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7340 | { |
| 7341 | int n = 1; |
| 7342 | if (argv[1]) { |
| 7343 | n = atoi(argv[1]); |
| 7344 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7345 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 7346 | if (G.global_args_malloced) { |
| 7347 | int m = 1; |
| 7348 | while (m <= n) |
| 7349 | free(G.global_argv[m++]); |
| 7350 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7351 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 7352 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 7353 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7354 | return EXIT_SUCCESS; |
| 7355 | } |
| 7356 | return EXIT_FAILURE; |
| 7357 | } |
| 7358 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7359 | static int FAST_FUNC builtin_source(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7360 | { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7361 | char *arg_path; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7362 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7363 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7364 | #if ENABLE_HUSH_FUNCTIONS |
| 7365 | smallint sv_flg; |
| 7366 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7367 | |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7368 | if (*++argv == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7369 | return EXIT_FAILURE; |
| 7370 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7371 | if (strchr(*argv, '/') == NULL && (arg_path = find_in_path(*argv)) != NULL) { |
| 7372 | input = fopen_for_read(arg_path); |
| 7373 | free(arg_path); |
| 7374 | } else |
| 7375 | input = fopen_or_warn(*argv, "r"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7376 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7377 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7378 | return EXIT_FAILURE; |
| 7379 | } |
| 7380 | close_on_exec_on(fileno(input)); |
| 7381 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7382 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7383 | sv_flg = G.flag_return_in_progress; |
| 7384 | /* "we are inside sourced file, ok to use return" */ |
| 7385 | G.flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7386 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7387 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7388 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7389 | parse_and_run_file(input); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7390 | fclose(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7391 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7392 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7393 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7394 | G.flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7395 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7396 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7397 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7398 | } |
| 7399 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7400 | static int FAST_FUNC builtin_umask(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7401 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7402 | int rc; |
| 7403 | mode_t mask; |
| 7404 | |
| 7405 | mask = umask(0); |
| 7406 | if (argv[1]) { |
| 7407 | mode_t old_mask = mask; |
| 7408 | |
| 7409 | mask ^= 0777; |
| 7410 | rc = bb_parse_mode(argv[1], &mask); |
| 7411 | mask ^= 0777; |
| 7412 | if (rc == 0) { |
| 7413 | mask = old_mask; |
| 7414 | /* bash messages: |
| 7415 | * bash: umask: 'q': invalid symbolic mode operator |
| 7416 | * bash: umask: 999: octal number out of range |
| 7417 | */ |
| 7418 | bb_error_msg("%s: '%s' invalid mode", argv[0], argv[1]); |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7419 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7420 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7421 | rc = 1; |
| 7422 | /* Mimic bash */ |
| 7423 | printf("%04o\n", (unsigned) mask); |
| 7424 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7425 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7426 | umask(mask); |
| 7427 | |
| 7428 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7429 | } |
| 7430 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7431 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7432 | static int FAST_FUNC builtin_unset(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7433 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7434 | int ret; |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7435 | unsigned opts; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7436 | |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7437 | /* "!": do not abort on errors */ |
| 7438 | /* "+": stop at 1st non-option */ |
| 7439 | opts = getopt32(argv, "!+vf"); |
| 7440 | if (opts == (unsigned)-1) |
| 7441 | return EXIT_FAILURE; |
| 7442 | if (opts == 3) { |
| 7443 | bb_error_msg("unset: -v and -f are exclusive"); |
| 7444 | return EXIT_FAILURE; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7445 | } |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7446 | argv += optind; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7447 | |
| 7448 | ret = EXIT_SUCCESS; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7449 | while (*argv) { |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7450 | if (!(opts & 2)) { /* not -f */ |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7451 | if (unset_local_var(*argv)) { |
| 7452 | /* unset <nonexistent_var> doesn't fail. |
| 7453 | * Error is when one tries to unset RO var. |
| 7454 | * Message was printed by unset_local_var. */ |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7455 | ret = EXIT_FAILURE; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7456 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7457 | } |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 7458 | #if ENABLE_HUSH_FUNCTIONS |
| 7459 | else { |
| 7460 | unset_func(*argv); |
| 7461 | } |
| 7462 | #endif |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7463 | argv++; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7464 | } |
| 7465 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7466 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7467 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7468 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7469 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7470 | { |
| 7471 | int ret = EXIT_SUCCESS; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7472 | int status, sig; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7473 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7474 | if (*++argv == NULL) { |
| 7475 | /* Don't care about wait results */ |
| 7476 | /* Note 1: must wait until there are no more children */ |
| 7477 | /* Note 2: must be interruptible */ |
| 7478 | /* Examples: |
| 7479 | * $ sleep 3 & sleep 6 & wait |
| 7480 | * [1] 30934 sleep 3 |
| 7481 | * [2] 30935 sleep 6 |
| 7482 | * [1] Done sleep 3 |
| 7483 | * [2] Done sleep 6 |
| 7484 | * $ sleep 3 & sleep 6 & wait |
| 7485 | * [1] 30936 sleep 3 |
| 7486 | * [2] 30937 sleep 6 |
| 7487 | * [1] Done sleep 3 |
| 7488 | * ^C <-- after ~4 sec from keyboard |
| 7489 | * $ |
| 7490 | */ |
| 7491 | sigaddset(&G.blocked_set, SIGCHLD); |
| 7492 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7493 | while (1) { |
| 7494 | checkjobs(NULL); |
| 7495 | if (errno == ECHILD) |
| 7496 | break; |
| 7497 | /* Wait for SIGCHLD or any other signal of interest */ |
| 7498 | /* sigtimedwait with infinite timeout: */ |
| 7499 | sig = sigwaitinfo(&G.blocked_set, NULL); |
| 7500 | if (sig > 0) { |
| 7501 | sig = check_and_run_traps(sig); |
| 7502 | if (sig && sig != SIGCHLD) { /* see note 2 */ |
| 7503 | ret = 128 + sig; |
| 7504 | break; |
| 7505 | } |
| 7506 | } |
| 7507 | } |
| 7508 | sigdelset(&G.blocked_set, SIGCHLD); |
| 7509 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7510 | return ret; |
| 7511 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7512 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7513 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7514 | while (*argv) { |
| 7515 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 7516 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7517 | /* mimic bash message */ |
| 7518 | 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] | 7519 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7520 | } |
| 7521 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7522 | if (WIFSIGNALED(status)) |
| 7523 | ret = 128 + WTERMSIG(status); |
| 7524 | else if (WIFEXITED(status)) |
| 7525 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7526 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7527 | ret = EXIT_FAILURE; |
| 7528 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7529 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7530 | ret = 127; |
| 7531 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7532 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7533 | } |
| 7534 | |
| 7535 | return ret; |
| 7536 | } |
| 7537 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7538 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 7539 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 7540 | { |
| 7541 | if (argv[1]) { |
| 7542 | def = bb_strtou(argv[1], NULL, 10); |
| 7543 | if (errno || def < def_min || argv[2]) { |
| 7544 | bb_error_msg("%s: bad arguments", argv[0]); |
| 7545 | def = UINT_MAX; |
| 7546 | } |
| 7547 | } |
| 7548 | return def; |
| 7549 | } |
| 7550 | #endif |
| 7551 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 7552 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7553 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7554 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7555 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7556 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 7557 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 7558 | return EXIT_SUCCESS; /* bash compat */ |
| 7559 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7560 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7561 | |
| 7562 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 7563 | if (depth == UINT_MAX) |
| 7564 | G.flag_break_continue = BC_BREAK; |
| 7565 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7566 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7567 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7568 | return EXIT_SUCCESS; |
| 7569 | } |
| 7570 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7571 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7572 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 7573 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 7574 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7575 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 7576 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7577 | |
| 7578 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7579 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7580 | { |
| 7581 | int rc; |
| 7582 | |
| 7583 | if (G.flag_return_in_progress != -1) { |
| 7584 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 7585 | return EXIT_FAILURE; /* bash compat */ |
| 7586 | } |
| 7587 | |
| 7588 | G.flag_return_in_progress = 1; |
| 7589 | |
| 7590 | /* bash: |
| 7591 | * out of range: wraps around at 256, does not error out |
| 7592 | * non-numeric param: |
| 7593 | * f() { false; return qwe; }; f; echo $? |
| 7594 | * bash: return: qwe: numeric argument required <== we do this |
| 7595 | * 255 <== we also do this |
| 7596 | */ |
| 7597 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 7598 | return rc; |
| 7599 | } |
| 7600 | #endif |