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 | * |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 45 | * Bash stuff (maybe optionally enable?): |
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} |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 50 | * |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 51 | * TODOs: |
| 52 | * grep for "TODO" and fix (some of them are easy) |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 53 | * builtins: ulimit |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 54 | * follow IFS rules more precisely, including update semantics |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 55 | * export builtin should be special, its arguments are assignments |
| 56 | * and therefore expansion of them should be "one-word" expansion: |
| 57 | * $ export i=`echo 'a b'` # export has one arg: "i=a b" |
| 58 | * compare with: |
| 59 | * $ ls i=`echo 'a b'` # ls has two args: "i=a" and "b" |
| 60 | * ls: cannot access i=a: No such file or directory |
| 61 | * ls: cannot access b: No such file or directory |
| 62 | * Note1: same applies to local builtin when we'll have it. |
| 63 | * Note2: bash 3.2.33(1) does this only if export word itself |
| 64 | * is not quoted: |
| 65 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 66 | * aaa bbb |
| 67 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 68 | * aaa |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 69 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 70 | * 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] | 71 | */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 72 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 73 | #include <glob.h> |
| 74 | /* #include <dmalloc.h> */ |
| 75 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 76 | # include <fnmatch.h> |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 77 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 78 | #include "math.h" |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 79 | #include "match.h" |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 80 | #ifndef PIPE_BUF |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 81 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 82 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 83 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 84 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 85 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 86 | #define LEAK_HUNTING 0 |
| 87 | #define BUILD_AS_NOMMU 0 |
| 88 | /* Enable/disable sanity checks. Ok to enable in production, |
| 89 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 90 | * Keeping 1 for now even in released versions. |
| 91 | */ |
| 92 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 93 | /* Slightly bigger (+200 bytes), but faster hush. |
| 94 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 95 | * which allows us to do fewer waitpid's. |
| 96 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 97 | * and therefore waitpid will return the same result as last time) |
| 98 | */ |
| 99 | #define ENABLE_HUSH_FAST 0 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 100 | |
| 101 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 102 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 103 | # undef BB_MMU |
| 104 | # undef USE_FOR_NOMMU |
| 105 | # undef USE_FOR_MMU |
| 106 | # define BB_MMU 0 |
| 107 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 108 | # define USE_FOR_MMU(...) |
| 109 | #endif |
| 110 | |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 111 | #if defined SINGLE_APPLET_MAIN |
| 112 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 113 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 114 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 115 | # undef IF_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 116 | # define IF_FEATURE_SH_STANDALONE(...) |
| 117 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denys Vlasenko | 673e945 | 2009-05-27 14:39:35 +0200 | [diff] [blame] | 118 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 119 | #endif |
| 120 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 121 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 122 | # undef ENABLE_FEATURE_EDITING |
| 123 | # define ENABLE_FEATURE_EDITING 0 |
| 124 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 125 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 126 | #endif |
| 127 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 128 | /* Do we support ANY keywords? */ |
| 129 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 130 | # define HAS_KEYWORDS 1 |
| 131 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 132 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 133 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 134 | # define HAS_KEYWORDS 0 |
| 135 | # define IF_HAS_KEYWORDS(...) |
| 136 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 137 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 138 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 139 | /* If you comment out one of these below, it will be #defined later |
| 140 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 141 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 142 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 143 | #define debug_printf_parse(...) do {} while (0) |
| 144 | #define debug_print_tree(a, b) do {} while (0) |
| 145 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 146 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 147 | #define debug_printf_jobs(...) do {} while (0) |
| 148 | #define debug_printf_expand(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 149 | #define debug_printf_glob(...) do {} while (0) |
| 150 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 151 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 152 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 153 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 154 | #define ERR_PTR ((void*)(long)1) |
| 155 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 156 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 157 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 158 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 159 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 160 | struct variable; |
| 161 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 162 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 163 | |
| 164 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 165 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 166 | */ |
| 167 | #if !BB_MMU |
| 168 | typedef struct nommu_save_t { |
| 169 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 170 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 171 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 172 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 173 | } nommu_save_t; |
| 174 | #endif |
| 175 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 176 | /* The descrip member of this structure is only used to make |
| 177 | * debugging output pretty */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 178 | static const struct { |
| 179 | int mode; |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 180 | signed char default_fd; |
| 181 | char descrip[3]; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 182 | } redir_table[] = { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 183 | { 0, 0, "??" }, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 184 | { O_RDONLY, 0, "<" }, |
| 185 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 186 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 187 | { O_RDONLY, 0, "<<" }, |
| 188 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 189 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 190 | /* { O_RDONLY, 77, "<<" }, */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 193 | typedef enum reserved_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 194 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 195 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 196 | RES_IF , |
| 197 | RES_THEN , |
| 198 | RES_ELIF , |
| 199 | RES_ELSE , |
| 200 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 201 | #endif |
| 202 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 203 | RES_FOR , |
| 204 | RES_WHILE , |
| 205 | RES_UNTIL , |
| 206 | RES_DO , |
| 207 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 208 | #endif |
| 209 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 210 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 211 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 212 | #if ENABLE_HUSH_CASE |
| 213 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 214 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 215 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 216 | RES_MATCH , /* "word)" */ |
| 217 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 218 | RES_ESAC , |
| 219 | #endif |
| 220 | RES_XXXX , |
| 221 | RES_SNTX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 222 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 223 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 224 | typedef struct o_string { |
| 225 | char *data; |
| 226 | int length; /* position where data is appended */ |
| 227 | int maxlen; |
| 228 | /* Protect newly added chars against globbing |
| 229 | * (by prepending \ to *, ?, [, \) */ |
| 230 | smallint o_escape; |
| 231 | smallint o_glob; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 232 | /* At least some part of the string was inside '' or "", |
| 233 | * possibly empty one: word"", wo''rd etc. */ |
| 234 | smallint o_quoted; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 235 | smallint has_empty_slot; |
| 236 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 237 | } o_string; |
| 238 | enum { |
| 239 | MAYBE_ASSIGNMENT = 0, |
| 240 | DEFINITELY_ASSIGNMENT = 1, |
| 241 | NOT_ASSIGNMENT = 2, |
| 242 | WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */ |
| 243 | }; |
| 244 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 245 | #define NULL_O_STRING { NULL } |
| 246 | |
| 247 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
| 248 | * available? Where is it documented? */ |
| 249 | typedef struct in_str { |
| 250 | const char *p; |
| 251 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 252 | char eof_flag; /* meaningless if ->p == NULL */ |
| 253 | char peek_buf[2]; |
| 254 | #if ENABLE_HUSH_INTERACTIVE |
| 255 | smallint promptme; |
| 256 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 257 | #endif |
| 258 | FILE *file; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 259 | int (*get) (struct in_str *) FAST_FUNC; |
| 260 | int (*peek) (struct in_str *) FAST_FUNC; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 261 | } in_str; |
| 262 | #define i_getch(input) ((input)->get(input)) |
| 263 | #define i_peek(input) ((input)->peek(input)) |
| 264 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 265 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 266 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 267 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 268 | int rd_fd; /* fd to redirect */ |
| 269 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 270 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 271 | smallint rd_type; /* (enum redir_type) */ |
| 272 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 273 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
| 274 | * 1: do we need to trim leading tabs? |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 275 | * 2: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 276 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 277 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 278 | typedef enum redir_type { |
| 279 | REDIRECT_INVALID = 0, |
| 280 | REDIRECT_INPUT = 1, |
| 281 | REDIRECT_OVERWRITE = 2, |
| 282 | REDIRECT_APPEND = 3, |
| 283 | REDIRECT_HEREDOC = 4, |
| 284 | REDIRECT_IO = 5, |
| 285 | REDIRECT_HEREDOC2 = 6, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 286 | |
| 287 | REDIRFD_CLOSE = -3, |
| 288 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 289 | REDIRFD_TO_FILE = -1, |
| 290 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 291 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 292 | HEREDOC_SKIPTABS = 1, |
| 293 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 294 | } redir_type; |
| 295 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 296 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 297 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 298 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 299 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 300 | smallint is_stopped; /* is the command currently running? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 301 | smallint grp_type; /* GRP_xxx */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 302 | #define GRP_NORMAL 0 |
| 303 | #define GRP_SUBSHELL 1 |
| 304 | #if ENABLE_HUSH_FUNCTIONS |
| 305 | # define GRP_FUNCTION 2 |
| 306 | #endif |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 307 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 308 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 309 | #if !BB_MMU |
| 310 | char *group_as_string; |
| 311 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 312 | #if ENABLE_HUSH_FUNCTIONS |
| 313 | struct function *child_func; |
| 314 | /* This field is used to prevent a bug here: |
| 315 | * while...do f1() {a;}; f1; f1 {b;}; f1; done |
| 316 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 317 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
| 318 | * when we execute "f1 {b;}", we notice that f1 exists, |
| 319 | * and that it's "parent cmd" struct is still "alive", |
| 320 | * we put those fields back into cmd->xxx |
| 321 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 322 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 323 | * Without this trick, loop would execute a;b;b;b;... |
| 324 | * instead of correct sequence a;b;a;b;... |
| 325 | * When command is freed, it severs the link |
| 326 | * (sets ->child_func->parent_cmd to NULL). |
| 327 | */ |
| 328 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 329 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 330 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 331 | * and on execution these are substituted with their values. |
| 332 | * Substitution can make _several_ words out of one argv[n]! |
| 333 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 334 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 335 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 336 | struct redir_struct *redirects; /* I/O redirections */ |
| 337 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 338 | /* Is there anything in this command at all? */ |
| 339 | #define IS_NULL_CMD(cmd) \ |
| 340 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 341 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 342 | |
| 343 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 344 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 345 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 346 | int alive_cmds; /* number of commands running (not exited) */ |
| 347 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 348 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 349 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 350 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 351 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 352 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 353 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 354 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 355 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 356 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 357 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 358 | typedef enum pipe_style { |
| 359 | PIPE_SEQ = 1, |
| 360 | PIPE_AND = 2, |
| 361 | PIPE_OR = 3, |
| 362 | PIPE_BG = 4, |
| 363 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 364 | /* Is there anything in this pipe at all? */ |
| 365 | #define IS_NULL_PIPE(pi) \ |
| 366 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 367 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 368 | /* This holds pointers to the various results of parsing */ |
| 369 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 370 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 371 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 372 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 373 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 374 | /* last command in pipe (being constructed right now) */ |
| 375 | struct command *command; |
| 376 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 377 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 378 | #if !BB_MMU |
| 379 | o_string as_string; |
| 380 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 381 | #if HAS_KEYWORDS |
| 382 | smallint ctx_res_w; |
| 383 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 384 | #if ENABLE_HUSH_CASE |
| 385 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 386 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 387 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 388 | int old_flag; |
| 389 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 390 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 391 | * when we see "if" or "then", we malloc and copy current context, |
| 392 | * and make ->stack point to it. then we parse pipeN. |
| 393 | * when closing "then" / fi" / whatever is found, |
| 394 | * we move list_head into ->stack->command->group, |
| 395 | * copy ->stack into current context, and delete ->stack. |
| 396 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 397 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 398 | struct parse_context *stack; |
| 399 | #endif |
| 400 | }; |
| 401 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 402 | /* On program start, environ points to initial environment. |
| 403 | * putenv adds new pointers into it, unsetenv removes them. |
| 404 | * Neither of these (de)allocates the strings. |
| 405 | * setenv allocates new strings in malloc space and does putenv, |
| 406 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 407 | #define setenv(...) setenv_is_leaky_dont_use() |
| 408 | struct variable { |
| 409 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 410 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 411 | #if ENABLE_HUSH_LOCAL |
| 412 | unsigned func_nest_level; |
| 413 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 414 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 415 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 416 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 417 | }; |
| 418 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 419 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 420 | BC_BREAK = 1, |
| 421 | BC_CONTINUE = 2, |
| 422 | }; |
| 423 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 424 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 425 | struct function { |
| 426 | struct function *next; |
| 427 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 428 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 429 | struct pipe *body; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 430 | #if !BB_MMU |
| 431 | char *body_as_string; |
| 432 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 433 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 434 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 435 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 436 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 437 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 438 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 439 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 440 | /* interactive_fd != 0 means we are an interactive shell. |
| 441 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 442 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 443 | * job control still works, but terminal signals |
| 444 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 445 | * process groups can only be created with "cmd &". |
| 446 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 447 | * to give tty to the foreground process group, |
| 448 | * and will take it back when the group is stopped (^Z) |
| 449 | * or killed (^C). |
| 450 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 451 | #if ENABLE_HUSH_INTERACTIVE |
| 452 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 453 | * _AND_ if we decided to act interactively */ |
| 454 | int interactive_fd; |
| 455 | const char *PS1; |
| 456 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 457 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 458 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 459 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 460 | #endif |
| 461 | #if ENABLE_FEATURE_EDITING |
| 462 | line_input_t *line_input_state; |
| 463 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 464 | pid_t root_pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 465 | pid_t last_bg_pid; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 466 | #if ENABLE_HUSH_JOB |
| 467 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 468 | int last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 469 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 470 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 471 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 472 | #else |
| 473 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 474 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 475 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 476 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 477 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 478 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 479 | #if ENABLE_HUSH_FUNCTIONS |
| 480 | /* 0: outside of a function (or sourced file) |
| 481 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 482 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 483 | */ |
| 484 | smallint flag_return_in_progress; |
| 485 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 486 | smallint fake_mode; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 487 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 488 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 489 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 490 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 491 | smalluint global_args_malloced; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 492 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 493 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 494 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 495 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 496 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 497 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 498 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 499 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 500 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 501 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 502 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 503 | const char *cwd; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 504 | struct variable *top_var; /* = &G.shell_ver (set in main()) */ |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 505 | struct variable shell_ver; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 506 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 507 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 508 | # if ENABLE_HUSH_LOCAL |
| 509 | struct variable **shadowed_vars_pp; |
| 510 | unsigned func_nest_level; |
| 511 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 512 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 513 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 514 | #if ENABLE_HUSH_FAST |
| 515 | unsigned count_SIGCHLD; |
| 516 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 517 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 518 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 519 | /* which signals have non-DFL handler (even with no traps set)? */ |
| 520 | unsigned non_DFL_mask; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 521 | char **traps; /* char *traps[NSIG] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 522 | sigset_t blocked_set; |
| 523 | sigset_t inherited_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 524 | #if HUSH_DEBUG |
| 525 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 526 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 527 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 528 | char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2]; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 529 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 530 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 531 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 532 | * (too many defines). Also, I actually prefer to see when a variable |
| 533 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 534 | #define INIT_G() do { \ |
| 535 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 536 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 537 | |
| 538 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 539 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 540 | static int builtin_cd(char **argv) FAST_FUNC; |
| 541 | static int builtin_echo(char **argv) FAST_FUNC; |
| 542 | static int builtin_eval(char **argv) FAST_FUNC; |
| 543 | static int builtin_exec(char **argv) FAST_FUNC; |
| 544 | static int builtin_exit(char **argv) FAST_FUNC; |
| 545 | static int builtin_export(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 546 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 547 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 548 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 549 | #endif |
| 550 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 551 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 552 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 553 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 554 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 555 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 556 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 557 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 558 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 559 | static int builtin_pwd(char **argv) FAST_FUNC; |
| 560 | static int builtin_read(char **argv) FAST_FUNC; |
| 561 | static int builtin_set(char **argv) FAST_FUNC; |
| 562 | static int builtin_shift(char **argv) FAST_FUNC; |
| 563 | static int builtin_source(char **argv) FAST_FUNC; |
| 564 | static int builtin_test(char **argv) FAST_FUNC; |
| 565 | static int builtin_trap(char **argv) FAST_FUNC; |
| 566 | static int builtin_type(char **argv) FAST_FUNC; |
| 567 | static int builtin_true(char **argv) FAST_FUNC; |
| 568 | static int builtin_umask(char **argv) FAST_FUNC; |
| 569 | static int builtin_unset(char **argv) FAST_FUNC; |
| 570 | static int builtin_wait(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 571 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 572 | static int builtin_break(char **argv) FAST_FUNC; |
| 573 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 574 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 575 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 576 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 577 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 578 | |
| 579 | /* Table of built-in functions. They can be forked or not, depending on |
| 580 | * context: within pipes, they fork. As simple commands, they do not. |
| 581 | * When used in non-forking context, they can change global variables |
| 582 | * in the parent shell process. If forked, of course they cannot. |
| 583 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 584 | * still be set at the end. */ |
| 585 | struct built_in_command { |
| 586 | const char *cmd; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 587 | int (*function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 588 | #if ENABLE_HUSH_HELP |
| 589 | const char *descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 590 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 591 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 592 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 593 | #endif |
| 594 | }; |
| 595 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 596 | static const struct built_in_command bltins1[] = { |
| 597 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 598 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 599 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 600 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 601 | #endif |
| 602 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 603 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 604 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 605 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 606 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 607 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 608 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 609 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 610 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 611 | BLTIN("exit" , builtin_exit , "Exit"), |
| 612 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 613 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 614 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 615 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 616 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 617 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 618 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 619 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 620 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 621 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 622 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 623 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 624 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 625 | #if HUSH_DEBUG |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 626 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 627 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 628 | BLTIN("read" , builtin_read , "Input into variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 629 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 630 | BLTIN("return" , builtin_return , "Return from a function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 631 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 632 | BLTIN("set" , builtin_set , "Set/unset positional parameters"), |
| 633 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
| 634 | BLTIN("trap" , builtin_trap , "Trap signals"), |
| 635 | BLTIN("type" , builtin_type , "Write a description of command type"), |
| 636 | // BLTIN("ulimit" , builtin_ulimit , "Control resource limits"), |
| 637 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 638 | BLTIN("unset" , builtin_unset , "Unset variables"), |
| 639 | BLTIN("wait" , builtin_wait , "Wait for process"), |
| 640 | }; |
| 641 | /* For now, echo and test are unconditionally enabled. |
| 642 | * Maybe make it configurable? */ |
| 643 | static const struct built_in_command bltins2[] = { |
| 644 | BLTIN("[" , builtin_test , NULL), |
| 645 | BLTIN("echo" , builtin_echo , NULL), |
| 646 | BLTIN("pwd" , builtin_pwd , NULL), |
| 647 | BLTIN("test" , builtin_test , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 648 | }; |
| 649 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 650 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 651 | /* Debug printouts. |
| 652 | */ |
| 653 | #if HUSH_DEBUG |
| 654 | /* prevent disasters with G.debug_indent < 0 */ |
| 655 | # define indent() fprintf(stderr, "%*s", (G.debug_indent * 2) & 0xff, "") |
| 656 | # define debug_enter() (G.debug_indent++) |
| 657 | # define debug_leave() (G.debug_indent--) |
| 658 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 659 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 660 | # define debug_enter() ((void)0) |
| 661 | # define debug_leave() ((void)0) |
| 662 | #endif |
| 663 | |
| 664 | #ifndef debug_printf |
| 665 | # define debug_printf(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 666 | #endif |
| 667 | |
| 668 | #ifndef debug_printf_parse |
| 669 | # define debug_printf_parse(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 670 | #endif |
| 671 | |
| 672 | #ifndef debug_printf_exec |
| 673 | #define debug_printf_exec(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 674 | #endif |
| 675 | |
| 676 | #ifndef debug_printf_env |
| 677 | # define debug_printf_env(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 678 | #endif |
| 679 | |
| 680 | #ifndef debug_printf_jobs |
| 681 | # define debug_printf_jobs(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 682 | # define DEBUG_JOBS 1 |
| 683 | #else |
| 684 | # define DEBUG_JOBS 0 |
| 685 | #endif |
| 686 | |
| 687 | #ifndef debug_printf_expand |
| 688 | # define debug_printf_expand(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 689 | # define DEBUG_EXPAND 1 |
| 690 | #else |
| 691 | # define DEBUG_EXPAND 0 |
| 692 | #endif |
| 693 | |
| 694 | #ifndef debug_printf_glob |
| 695 | # define debug_printf_glob(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 696 | # define DEBUG_GLOB 1 |
| 697 | #else |
| 698 | # define DEBUG_GLOB 0 |
| 699 | #endif |
| 700 | |
| 701 | #ifndef debug_printf_list |
| 702 | # define debug_printf_list(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 703 | #endif |
| 704 | |
| 705 | #ifndef debug_printf_subst |
| 706 | # define debug_printf_subst(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 707 | #endif |
| 708 | |
| 709 | #ifndef debug_printf_clean |
| 710 | # define debug_printf_clean(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 711 | # define DEBUG_CLEAN 1 |
| 712 | #else |
| 713 | # define DEBUG_CLEAN 0 |
| 714 | #endif |
| 715 | |
| 716 | #if DEBUG_EXPAND |
| 717 | static void debug_print_strings(const char *prefix, char **vv) |
| 718 | { |
| 719 | indent(); |
| 720 | fprintf(stderr, "%s:\n", prefix); |
| 721 | while (*vv) |
| 722 | fprintf(stderr, " '%s'\n", *vv++); |
| 723 | } |
| 724 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 725 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 726 | #endif |
| 727 | |
| 728 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 729 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 730 | */ |
| 731 | #if LEAK_HUNTING |
| 732 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 733 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 734 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 735 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 736 | return ptr; |
| 737 | } |
| 738 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 739 | { |
| 740 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 741 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 742 | return ptr; |
| 743 | } |
| 744 | static char *xxstrdup(int lineno, const char *str) |
| 745 | { |
| 746 | char *ptr = xstrdup(str); |
| 747 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 748 | return ptr; |
| 749 | } |
| 750 | static void xxfree(void *ptr) |
| 751 | { |
| 752 | fdprintf(2, "free %p\n", ptr); |
| 753 | free(ptr); |
| 754 | } |
| 755 | #define xmalloc(s) xxmalloc(__LINE__, s) |
| 756 | #define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 757 | #define xstrdup(s) xxstrdup(__LINE__, s) |
| 758 | #define free(p) xxfree(p) |
| 759 | #endif |
| 760 | |
| 761 | |
| 762 | /* Syntax and runtime errors. They always abort scripts. |
| 763 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 764 | * and return to the prompt. |
| 765 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 766 | */ |
| 767 | #if HUSH_DEBUG < 2 |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 768 | # define die_if_script(lineno, fmt...) die_if_script(fmt) |
| 769 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 770 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 771 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 772 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 773 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 774 | #endif |
| 775 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 776 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 777 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 778 | va_list p; |
| 779 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 780 | #if HUSH_DEBUG >= 2 |
| 781 | bb_error_msg("hush.c:%u", lineno); |
| 782 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 783 | va_start(p, fmt); |
| 784 | bb_verror_msg(fmt, p, NULL); |
| 785 | va_end(p); |
| 786 | if (!G_interactive_fd) |
| 787 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 788 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 789 | |
| 790 | static void syntax_error(unsigned lineno, const char *msg) |
| 791 | { |
| 792 | if (msg) |
| 793 | die_if_script(lineno, "syntax error: %s", msg); |
| 794 | else |
| 795 | die_if_script(lineno, "syntax error", NULL); |
| 796 | } |
| 797 | |
| 798 | static void syntax_error_at(unsigned lineno, const char *msg) |
| 799 | { |
| 800 | die_if_script(lineno, "syntax error at '%s'", msg); |
| 801 | } |
| 802 | |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 803 | static void syntax_error_unterm_str(unsigned lineno, const char *s) |
| 804 | { |
| 805 | die_if_script(lineno, "syntax error: unterminated %s", s); |
| 806 | } |
| 807 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 808 | /* It so happens that all such cases are totally fatal |
| 809 | * even if shell is interactive: EOF while looking for closing |
| 810 | * delimiter. There is nowhere to read stuff from after that, |
| 811 | * it's EOF! The only choice is to terminate. |
| 812 | */ |
| 813 | static void syntax_error_unterm_ch(unsigned lineno, char ch) NORETURN; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 814 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 815 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 816 | char msg[2] = { ch, '\0' }; |
| 817 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 818 | xfunc_die(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 821 | static void syntax_error_unexpected_ch(unsigned lineno, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 822 | { |
| 823 | char msg[2]; |
| 824 | msg[0] = ch; |
| 825 | msg[1] = '\0'; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 826 | die_if_script(lineno, "syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 829 | #if HUSH_DEBUG < 2 |
| 830 | # undef die_if_script |
| 831 | # undef syntax_error |
| 832 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 833 | # undef syntax_error_unterm_ch |
| 834 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 835 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 836 | #else |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 837 | # define die_if_script(fmt...) die_if_script(__LINE__, fmt) |
| 838 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 839 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 840 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 841 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 842 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 843 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 844 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 845 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 846 | #if ENABLE_HUSH_INTERACTIVE |
| 847 | static void cmdedit_update_prompt(void); |
| 848 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 849 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 850 | #endif |
| 851 | |
| 852 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 853 | /* Utility functions |
| 854 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 855 | static int glob_needed(const char *s) |
| 856 | { |
| 857 | while (*s) { |
| 858 | if (*s == '\\') |
| 859 | s++; |
| 860 | if (*s == '*' || *s == '[' || *s == '?') |
| 861 | return 1; |
| 862 | s++; |
| 863 | } |
| 864 | return 0; |
| 865 | } |
| 866 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 867 | static int is_well_formed_var_name(const char *s, char terminator) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 868 | { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 869 | if (!s || !(isalpha(*s) || *s == '_')) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 870 | return 0; |
| 871 | s++; |
| 872 | while (isalnum(*s) || *s == '_') |
| 873 | s++; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 874 | return *s == terminator; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 877 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 878 | static char *unbackslash(char *src) |
| 879 | { |
| 880 | char *dst = src; |
| 881 | while (1) { |
| 882 | if (*src == '\\') |
| 883 | src++; |
| 884 | if ((*dst++ = *src++) == '\0') |
| 885 | break; |
| 886 | } |
| 887 | return dst; |
| 888 | } |
| 889 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 890 | 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] | 891 | { |
| 892 | int i; |
| 893 | unsigned count1; |
| 894 | unsigned count2; |
| 895 | char **v; |
| 896 | |
| 897 | v = strings; |
| 898 | count1 = 0; |
| 899 | if (v) { |
| 900 | while (*v) { |
| 901 | count1++; |
| 902 | v++; |
| 903 | } |
| 904 | } |
| 905 | count2 = 0; |
| 906 | v = add; |
| 907 | while (*v) { |
| 908 | count2++; |
| 909 | v++; |
| 910 | } |
| 911 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 912 | v[count1 + count2] = NULL; |
| 913 | i = count2; |
| 914 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 915 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 916 | return v; |
| 917 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 918 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 919 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 920 | { |
| 921 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 922 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 923 | return ptr; |
| 924 | } |
| 925 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 926 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 927 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 928 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 929 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 930 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 931 | { |
| 932 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 933 | v[0] = add; |
| 934 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 935 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 936 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 937 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 938 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 939 | { |
| 940 | char **ptr = add_string_to_strings(strings, add); |
| 941 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 942 | return ptr; |
| 943 | } |
| 944 | #define add_string_to_strings(strings, add) \ |
| 945 | xx_add_string_to_strings(__LINE__, strings, add) |
| 946 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 947 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 948 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 949 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 950 | char **v; |
| 951 | |
| 952 | if (!strings) |
| 953 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 954 | v = strings; |
| 955 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 956 | free(*v); |
| 957 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 958 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 959 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 962 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 963 | /* Helpers for setting new $n and restoring them back |
| 964 | */ |
| 965 | typedef struct save_arg_t { |
| 966 | char *sv_argv0; |
| 967 | char **sv_g_argv; |
| 968 | int sv_g_argc; |
| 969 | smallint sv_g_malloced; |
| 970 | } save_arg_t; |
| 971 | |
| 972 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 973 | { |
| 974 | int n; |
| 975 | |
| 976 | sv->sv_argv0 = argv[0]; |
| 977 | sv->sv_g_argv = G.global_argv; |
| 978 | sv->sv_g_argc = G.global_argc; |
| 979 | sv->sv_g_malloced = G.global_args_malloced; |
| 980 | |
| 981 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 982 | G.global_argv = argv; |
| 983 | G.global_args_malloced = 0; |
| 984 | |
| 985 | n = 1; |
| 986 | while (*++argv) |
| 987 | n++; |
| 988 | G.global_argc = n; |
| 989 | } |
| 990 | |
| 991 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 992 | { |
| 993 | char **pp; |
| 994 | |
| 995 | if (G.global_args_malloced) { |
| 996 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 997 | pp = G.global_argv; |
| 998 | while (*++pp) /* note: does not free $0 */ |
| 999 | free(*pp); |
| 1000 | free(G.global_argv); |
| 1001 | } |
| 1002 | argv[0] = sv->sv_argv0; |
| 1003 | G.global_argv = sv->sv_g_argv; |
| 1004 | G.global_argc = sv->sv_g_argc; |
| 1005 | G.global_args_malloced = sv->sv_g_malloced; |
| 1006 | } |
| 1007 | |
| 1008 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1009 | /* Basic theory of signal handling in shell |
| 1010 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1011 | * This does not describe what hush does, rather, it is current understanding |
| 1012 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1013 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1014 | * |
| 1015 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1016 | * is finished or backgrounded. It is the same in interactive and |
| 1017 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1018 | * 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] | 1019 | * ^C or ^Z from keyboard seem to execute "at once" because it usually |
| 1020 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1021 | * pipe. |
| 1022 | * |
| 1023 | * Wait builtin in interruptible by signals for which user trap is set |
| 1024 | * or by SIGINT in interactive shell. |
| 1025 | * |
| 1026 | * Trap handlers will execute even within trap handlers. (right?) |
| 1027 | * |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1028 | * User trap handlers are forgotten when subshell ("(cmd)") is entered. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1029 | * |
| 1030 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1031 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1032 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1033 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1034 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1035 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1036 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1037 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1038 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1039 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1040 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1041 | * |
| 1042 | * SIGQUIT: ignore |
| 1043 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1044 | * SIGHUP (interactive): |
| 1045 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1046 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1047 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1048 | * that all pipe members are stopped. Try this in bash: |
| 1049 | * while :; do :; done - ^Z does not background it |
| 1050 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1051 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1052 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1053 | * to interactive shell while shell is waiting for a pipe, |
| 1054 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1055 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1056 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1057 | * Example 2: this does not wait and does not execute ls: |
| 1058 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1059 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1060 | * "sleep 5; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1061 | * |
| 1062 | * (What happens to signals which are IGN on shell start?) |
| 1063 | * (What happens with signal mask on shell start?) |
| 1064 | * |
| 1065 | * Implementation in hush |
| 1066 | * ====================== |
| 1067 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1068 | * We block all signals which we don't want to take action immediately, |
| 1069 | * i.e. we block all signals which need to have special handling as described |
| 1070 | * above, and all signals which have traps set. |
| 1071 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1072 | * and act on them. |
| 1073 | * |
| 1074 | * unsigned non_DFL_mask: a mask of such "special" signals |
| 1075 | * sigset_t blocked_set: current blocked signal set |
| 1076 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1077 | * "trap - SIGxxx": |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1078 | * 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] | 1079 | * "trap 'cmd' SIGxxx": |
| 1080 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1081 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1082 | * unblock signals with special interactive handling |
| 1083 | * (child shell is not interactive), |
| 1084 | * unset all traps (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1085 | * after [v]fork, if we plan to exec: |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1086 | * POSIX says pending signal mask is cleared in child - no need to clear it. |
| 1087 | * 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] | 1088 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1089 | * 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] | 1090 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1091 | * and to restore tty pgrp on signal-induced exit. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1092 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1093 | enum { |
| 1094 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1095 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1096 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1097 | | (1 << SIGHUP) |
| 1098 | , |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1099 | SPECIAL_JOB_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1100 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1101 | | (1 << SIGTTIN) |
| 1102 | | (1 << SIGTTOU) |
| 1103 | | (1 << SIGTSTP) |
| 1104 | #endif |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1105 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1106 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1107 | #if ENABLE_HUSH_FAST |
| 1108 | static void SIGCHLD_handler(int sig UNUSED_PARAM) |
| 1109 | { |
| 1110 | G.count_SIGCHLD++; |
| 1111 | //bb_error_msg("[%d] SIGCHLD_handler: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1112 | } |
| 1113 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1114 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1115 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1116 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1117 | /* After [v]fork, in child: do not restore tty pgrp on xfunc death */ |
| 1118 | #define disable_restore_tty_pgrp_on_exit() (die_sleep = 0) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1119 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1120 | #define enable_restore_tty_pgrp_on_exit() (die_sleep = -1) |
| 1121 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1122 | /* Restores tty foreground process group, and exits. |
| 1123 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1124 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1125 | * or called directly with -EXITCODE. |
| 1126 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1127 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1128 | static void sigexit(int sig) |
| 1129 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1130 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 1131 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1132 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1133 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1134 | * tty pgrp then, only top-level shell process does that */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1135 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 1136 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1137 | |
| 1138 | /* Not a signal, just exit */ |
| 1139 | if (sig <= 0) |
| 1140 | _exit(- sig); |
| 1141 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1142 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1143 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1144 | #else |
| 1145 | |
| 1146 | #define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1147 | #define enable_restore_tty_pgrp_on_exit() ((void)0) |
| 1148 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1149 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1150 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1151 | /* Restores tty foreground process group, and exits. */ |
| 1152 | static void hush_exit(int exitcode) NORETURN; |
| 1153 | static void hush_exit(int exitcode) |
| 1154 | { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1155 | if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) { |
| 1156 | /* Prevent recursion: |
| 1157 | * trap "echo Hi; exit" EXIT; exit |
| 1158 | */ |
| 1159 | char *argv[] = { NULL, G.traps[0], NULL }; |
| 1160 | G.traps[0] = NULL; |
| 1161 | G.exiting = 1; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1162 | builtin_eval(argv); |
| 1163 | free(argv[1]); |
| 1164 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1165 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1166 | #if ENABLE_HUSH_JOB |
| 1167 | fflush(NULL); /* flush all streams */ |
| 1168 | sigexit(- (exitcode & 0xff)); |
| 1169 | #else |
| 1170 | exit(exitcode); |
| 1171 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1174 | static int check_and_run_traps(int sig) |
| 1175 | { |
| 1176 | static const struct timespec zero_timespec = { 0, 0 }; |
| 1177 | smalluint save_rcode; |
| 1178 | int last_sig = 0; |
| 1179 | |
| 1180 | if (sig) |
| 1181 | goto jump_in; |
| 1182 | while (1) { |
| 1183 | sig = sigtimedwait(&G.blocked_set, NULL, &zero_timespec); |
| 1184 | if (sig <= 0) |
| 1185 | break; |
| 1186 | jump_in: |
| 1187 | last_sig = sig; |
| 1188 | if (G.traps && G.traps[sig]) { |
| 1189 | if (G.traps[sig][0]) { |
| 1190 | /* We have user-defined handler */ |
| 1191 | char *argv[] = { NULL, xstrdup(G.traps[sig]), NULL }; |
| 1192 | save_rcode = G.last_exitcode; |
| 1193 | builtin_eval(argv); |
| 1194 | free(argv[1]); |
| 1195 | G.last_exitcode = save_rcode; |
| 1196 | } /* else: "" trap, ignoring signal */ |
| 1197 | continue; |
| 1198 | } |
| 1199 | /* not a trap: special action */ |
| 1200 | switch (sig) { |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1201 | #if ENABLE_HUSH_FAST |
| 1202 | case SIGCHLD: |
| 1203 | G.count_SIGCHLD++; |
| 1204 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1205 | break; |
| 1206 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1207 | case SIGINT: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1208 | /* Builtin was ^C'ed, make it look prettier: */ |
| 1209 | bb_putchar('\n'); |
| 1210 | G.flag_SIGINT = 1; |
| 1211 | break; |
| 1212 | #if ENABLE_HUSH_JOB |
| 1213 | case SIGHUP: { |
| 1214 | struct pipe *job; |
| 1215 | /* bash is observed to signal whole process groups, |
| 1216 | * not individual processes */ |
| 1217 | for (job = G.job_list; job; job = job->next) { |
| 1218 | if (job->pgrp <= 0) |
| 1219 | continue; |
| 1220 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 1221 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 1222 | kill(- job->pgrp, SIGCONT); |
| 1223 | } |
| 1224 | sigexit(SIGHUP); |
| 1225 | } |
| 1226 | #endif |
| 1227 | default: /* ignored: */ |
| 1228 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
| 1229 | break; |
| 1230 | } |
| 1231 | } |
| 1232 | return last_sig; |
| 1233 | } |
| 1234 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1235 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1236 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1237 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1238 | if (force || G.cwd == NULL) { |
| 1239 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1240 | * we must not try to free(bb_msg_unknown) */ |
| 1241 | if (G.cwd == bb_msg_unknown) |
| 1242 | G.cwd = NULL; |
| 1243 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1244 | if (!G.cwd) |
| 1245 | G.cwd = bb_msg_unknown; |
| 1246 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1247 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1250 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1251 | /* |
| 1252 | * Shell and environment variable support |
| 1253 | */ |
| 1254 | static struct variable **get_ptr_to_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1255 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1256 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1257 | struct variable *cur; |
| 1258 | int len; |
| 1259 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1260 | len = strlen(name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1261 | pp = &G.top_var; |
| 1262 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1263 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1264 | return pp; |
| 1265 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1266 | } |
| 1267 | return NULL; |
| 1268 | } |
| 1269 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1270 | static struct variable *get_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1271 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1272 | struct variable **pp = get_ptr_to_local_var(name); |
| 1273 | if (pp) |
| 1274 | return *pp; |
| 1275 | return NULL; |
| 1276 | } |
| 1277 | |
| 1278 | static const char *get_local_var_value(const char *name) |
| 1279 | { |
| 1280 | struct variable **pp = get_ptr_to_local_var(name); |
| 1281 | if (pp) |
| 1282 | return strchr((*pp)->varstr, '=') + 1; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1283 | return NULL; |
| 1284 | } |
| 1285 | |
| 1286 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1287 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1288 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1289 | * 0: do not change export flag |
| 1290 | * (if creating new variable, flag will be 0) |
| 1291 | * 1: set export flag and putenv the variable |
| 1292 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1293 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1294 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1295 | #if !BB_MMU && ENABLE_HUSH_LOCAL |
| 1296 | /* all params are used */ |
| 1297 | #elif BB_MMU && ENABLE_HUSH_LOCAL |
| 1298 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1299 | set_local_var(str, flg_export, local_lvl) |
| 1300 | #elif BB_MMU && !ENABLE_HUSH_LOCAL |
| 1301 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1302 | set_local_var(str, flg_export) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1303 | #elif !BB_MMU && !ENABLE_HUSH_LOCAL |
| 1304 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1305 | set_local_var(str, flg_export, flg_read_only) |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1306 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1307 | 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] | 1308 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1309 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1310 | struct variable *cur; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1311 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1312 | int name_len; |
| 1313 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1314 | eq_sign = strchr(str, '='); |
| 1315 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1316 | free(str); |
| 1317 | return -1; |
| 1318 | } |
| 1319 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1320 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1321 | var_pp = &G.top_var; |
| 1322 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1323 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1324 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1325 | continue; |
| 1326 | } |
| 1327 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1328 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1329 | #if !BB_MMU |
| 1330 | if (!flg_read_only) |
| 1331 | #endif |
| 1332 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1333 | free(str); |
| 1334 | return -1; |
| 1335 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1336 | if (flg_export == -1) { // "&& cur->flg_export" ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1337 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1338 | *eq_sign = '\0'; |
| 1339 | unsetenv(str); |
| 1340 | *eq_sign = '='; |
| 1341 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1342 | #if ENABLE_HUSH_LOCAL |
| 1343 | if (cur->func_nest_level < local_lvl) { |
| 1344 | /* New variable is declared as local, |
| 1345 | * and existing one is global, or local |
| 1346 | * from enclosing function. |
| 1347 | * Remove and save old one: */ |
| 1348 | *var_pp = cur->next; |
| 1349 | cur->next = *G.shadowed_vars_pp; |
| 1350 | *G.shadowed_vars_pp = cur; |
| 1351 | /* bash 3.2.33(1) and exported vars: |
| 1352 | * # export z=z |
| 1353 | * # f() { local z=a; env | grep ^z; } |
| 1354 | * # f |
| 1355 | * z=a |
| 1356 | * # env | grep ^z |
| 1357 | * z=z |
| 1358 | */ |
| 1359 | if (cur->flg_export) |
| 1360 | flg_export = 1; |
| 1361 | break; |
| 1362 | } |
| 1363 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1364 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1365 | free_and_exp: |
| 1366 | free(str); |
| 1367 | goto exp; |
| 1368 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1369 | if (cur->max_len != 0) { |
| 1370 | if (cur->max_len >= strlen(str)) { |
| 1371 | /* This one is from startup env, reuse space */ |
| 1372 | strcpy(cur->varstr, str); |
| 1373 | goto free_and_exp; |
| 1374 | } |
| 1375 | } else { |
| 1376 | /* max_len == 0 signifies "malloced" var, which we can |
| 1377 | * (and has to) free */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1378 | free(cur->varstr); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1379 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1380 | cur->max_len = 0; |
| 1381 | goto set_str_and_exp; |
| 1382 | } |
| 1383 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1384 | /* Not found - create new variable struct */ |
| 1385 | cur = xzalloc(sizeof(*cur)); |
| 1386 | #if ENABLE_HUSH_LOCAL |
| 1387 | cur->func_nest_level = local_lvl; |
| 1388 | #endif |
| 1389 | cur->next = *var_pp; |
| 1390 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1391 | |
| 1392 | set_str_and_exp: |
| 1393 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1394 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1395 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1396 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1397 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1398 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1399 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1400 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1401 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1402 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1403 | if (flg_export == -1) { |
| 1404 | cur->flg_export = 0; |
| 1405 | /* unsetenv was already done */ |
| 1406 | } else { |
| 1407 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1408 | return putenv(cur->varstr); |
| 1409 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1410 | } |
| 1411 | return 0; |
| 1412 | } |
| 1413 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1414 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1415 | { |
| 1416 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1417 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1418 | |
| 1419 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1420 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1421 | var_pp = &G.top_var; |
| 1422 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1423 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1424 | if (cur->flg_read_only) { |
| 1425 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1426 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1427 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1428 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1429 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1430 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1431 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1432 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1433 | if (!cur->max_len) |
| 1434 | free(cur->varstr); |
| 1435 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1436 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1437 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1438 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1439 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1440 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1443 | static int unset_local_var(const char *name) |
| 1444 | { |
| 1445 | return unset_local_var_len(name, strlen(name)); |
| 1446 | } |
| 1447 | |
| 1448 | static void unset_vars(char **strings) |
| 1449 | { |
| 1450 | char **v; |
| 1451 | |
| 1452 | if (!strings) |
| 1453 | return; |
| 1454 | v = strings; |
| 1455 | while (*v) { |
| 1456 | const char *eq = strchrnul(*v, '='); |
| 1457 | unset_local_var_len(*v, (int)(eq - *v)); |
| 1458 | v++; |
| 1459 | } |
| 1460 | free(strings); |
| 1461 | } |
| 1462 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1463 | #if ENABLE_SH_MATH_SUPPORT |
| 1464 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1465 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 1466 | static char *endofname(const char *name) |
| 1467 | { |
| 1468 | char *p; |
| 1469 | |
| 1470 | p = (char *) name; |
| 1471 | if (!is_name(*p)) |
| 1472 | return p; |
| 1473 | while (*++p) { |
| 1474 | if (!is_in_name(*p)) |
| 1475 | break; |
| 1476 | } |
| 1477 | return p; |
| 1478 | } |
| 1479 | |
| 1480 | static void arith_set_local_var(const char *name, const char *val, int flags) |
| 1481 | { |
| 1482 | /* arith code doesnt malloc space, so do it for it */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1483 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1484 | set_local_var(var, flags, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1485 | } |
| 1486 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1487 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1488 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1489 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1490 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 1491 | */ |
| 1492 | static void add_vars(struct variable *var) |
| 1493 | { |
| 1494 | struct variable *next; |
| 1495 | |
| 1496 | while (var) { |
| 1497 | next = var->next; |
| 1498 | var->next = G.top_var; |
| 1499 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1500 | if (var->flg_export) { |
| 1501 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1502 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1503 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1504 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1505 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1506 | var = next; |
| 1507 | } |
| 1508 | } |
| 1509 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1510 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1511 | { |
| 1512 | char **s; |
| 1513 | struct variable *old = NULL; |
| 1514 | |
| 1515 | if (!strings) |
| 1516 | return old; |
| 1517 | s = strings; |
| 1518 | while (*s) { |
| 1519 | struct variable *var_p; |
| 1520 | struct variable **var_pp; |
| 1521 | char *eq; |
| 1522 | |
| 1523 | eq = strchr(*s, '='); |
| 1524 | if (eq) { |
| 1525 | *eq = '\0'; |
| 1526 | var_pp = get_ptr_to_local_var(*s); |
| 1527 | *eq = '='; |
| 1528 | if (var_pp) { |
| 1529 | /* Remove variable from global linked list */ |
| 1530 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1531 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1532 | *var_pp = var_p->next; |
| 1533 | /* Add it to returned list */ |
| 1534 | var_p->next = old; |
| 1535 | old = var_p; |
| 1536 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1537 | set_local_var(*s, /*exp:*/ 1, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1538 | } |
| 1539 | s++; |
| 1540 | } |
| 1541 | return old; |
| 1542 | } |
| 1543 | |
| 1544 | |
| 1545 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1546 | * in_str support |
| 1547 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1548 | static int FAST_FUNC static_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1549 | { |
| 1550 | int ch = *i->p++; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1551 | if (ch != '\0') |
| 1552 | return ch; |
| 1553 | i->p--; |
| 1554 | return EOF; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1557 | static int FAST_FUNC static_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1558 | { |
| 1559 | return *i->p; |
| 1560 | } |
| 1561 | |
| 1562 | #if ENABLE_HUSH_INTERACTIVE |
| 1563 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1564 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1565 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1566 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1567 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1568 | if (G.PS1 == NULL) |
| 1569 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1570 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1571 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1572 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1573 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1574 | if (G.PS2 == NULL) |
| 1575 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1576 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1577 | |
| 1578 | static const char* setup_prompt_string(int promptmode) |
| 1579 | { |
| 1580 | const char *prompt_str; |
| 1581 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1582 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1583 | /* Set up the prompt */ |
| 1584 | if (promptmode == 0) { /* PS1 */ |
| 1585 | free((char*)G.PS1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1586 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1587 | prompt_str = G.PS1; |
| 1588 | } else |
| 1589 | prompt_str = G.PS2; |
| 1590 | } else |
| 1591 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1592 | debug_printf("result '%s'\n", prompt_str); |
| 1593 | return prompt_str; |
| 1594 | } |
| 1595 | |
| 1596 | static void get_user_input(struct in_str *i) |
| 1597 | { |
| 1598 | int r; |
| 1599 | const char *prompt_str; |
| 1600 | |
| 1601 | prompt_str = setup_prompt_string(i->promptmode); |
| 1602 | #if ENABLE_FEATURE_EDITING |
| 1603 | /* Enable command line editing only while a command line |
| 1604 | * is actually being read */ |
| 1605 | do { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1606 | G.flag_SIGINT = 0; |
| 1607 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 1608 | * only after <Enter>. (^C will work) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1609 | 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] | 1610 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1611 | check_and_run_traps(0); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1612 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1613 | i->eof_flag = (r < 0); |
| 1614 | if (i->eof_flag) { /* EOF/error detected */ |
| 1615 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1616 | G.user_input_buf[1] = '\0'; |
| 1617 | } |
| 1618 | #else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1619 | do { |
| 1620 | G.flag_SIGINT = 0; |
| 1621 | fputs(prompt_str, stdout); |
| 1622 | fflush(stdout); |
| 1623 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1624 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1625 | //do we need check_and_run_traps(0)? (maybe only if stdin) |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1626 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1627 | i->eof_flag = (r == EOF); |
| 1628 | #endif |
| 1629 | i->p = G.user_input_buf; |
| 1630 | } |
| 1631 | |
| 1632 | #endif /* INTERACTIVE */ |
| 1633 | |
| 1634 | /* This is the magic location that prints prompts |
| 1635 | * and gets data back from the user */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1636 | static int FAST_FUNC file_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1637 | { |
| 1638 | int ch; |
| 1639 | |
| 1640 | /* If there is data waiting, eat it up */ |
| 1641 | if (i->p && *i->p) { |
| 1642 | #if ENABLE_HUSH_INTERACTIVE |
| 1643 | take_cached: |
| 1644 | #endif |
| 1645 | ch = *i->p++; |
| 1646 | if (i->eof_flag && !*i->p) |
| 1647 | ch = EOF; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1648 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1649 | } else { |
| 1650 | /* need to double check i->file because we might be doing something |
| 1651 | * more complicated by now, like sourcing or substituting. */ |
| 1652 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 1653 | if (G_interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1654 | do { |
| 1655 | get_user_input(i); |
| 1656 | } while (!*i->p); /* need non-empty line */ |
| 1657 | i->promptmode = 1; /* PS2 */ |
| 1658 | i->promptme = 0; |
| 1659 | goto take_cached; |
| 1660 | } |
| 1661 | #endif |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1662 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1663 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1664 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1665 | #if ENABLE_HUSH_INTERACTIVE |
| 1666 | if (ch == '\n') |
| 1667 | i->promptme = 1; |
| 1668 | #endif |
| 1669 | return ch; |
| 1670 | } |
| 1671 | |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1672 | /* All callers guarantee this routine will never |
| 1673 | * be used right after a newline, so prompting is not needed. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1674 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1675 | static int FAST_FUNC file_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1676 | { |
| 1677 | int ch; |
| 1678 | if (i->p && *i->p) { |
| 1679 | if (i->eof_flag && !i->p[1]) |
| 1680 | return EOF; |
| 1681 | return *i->p; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1682 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1683 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1684 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1685 | i->eof_flag = (ch == EOF); |
| 1686 | i->peek_buf[0] = ch; |
| 1687 | i->peek_buf[1] = '\0'; |
| 1688 | i->p = i->peek_buf; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1689 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1690 | return ch; |
| 1691 | } |
| 1692 | |
| 1693 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1694 | { |
| 1695 | i->peek = file_peek; |
| 1696 | i->get = file_get; |
| 1697 | #if ENABLE_HUSH_INTERACTIVE |
| 1698 | i->promptme = 1; |
| 1699 | i->promptmode = 0; /* PS1 */ |
| 1700 | #endif |
| 1701 | i->file = f; |
| 1702 | i->p = NULL; |
| 1703 | } |
| 1704 | |
| 1705 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1706 | { |
| 1707 | i->peek = static_peek; |
| 1708 | i->get = static_get; |
| 1709 | #if ENABLE_HUSH_INTERACTIVE |
| 1710 | i->promptme = 1; |
| 1711 | i->promptmode = 0; /* PS1 */ |
| 1712 | #endif |
| 1713 | i->p = s; |
| 1714 | i->eof_flag = 0; |
| 1715 | } |
| 1716 | |
| 1717 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1718 | /* |
| 1719 | * o_string support |
| 1720 | */ |
| 1721 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1722 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1723 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1724 | { |
| 1725 | o->length = 0; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 1726 | o->o_quoted = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1727 | if (o->data) |
| 1728 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1731 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1732 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1733 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1734 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1737 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 1738 | { |
| 1739 | free(o->data); |
| 1740 | } |
| 1741 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1742 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1743 | { |
| 1744 | if (o->length + len > o->maxlen) { |
| 1745 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 1746 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 1747 | } |
| 1748 | } |
| 1749 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1750 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1751 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1752 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 1753 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1754 | o->data[o->length] = ch; |
| 1755 | o->length++; |
| 1756 | o->data[o->length] = '\0'; |
| 1757 | } |
| 1758 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1759 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1760 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1761 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1762 | memcpy(&o->data[o->length], str, len); |
| 1763 | o->length += len; |
| 1764 | o->data[o->length] = '\0'; |
| 1765 | } |
| 1766 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1767 | #if !BB_MMU |
| 1768 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1769 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1770 | o_addblock(o, str, strlen(str)); |
| 1771 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1772 | static void nommu_addchr(o_string *o, int ch) |
| 1773 | { |
| 1774 | if (o) |
| 1775 | o_addchr(o, ch); |
| 1776 | } |
| 1777 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1778 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1779 | #endif |
| 1780 | |
| 1781 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 1782 | { |
| 1783 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1786 | 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] | 1787 | { |
| 1788 | while (len) { |
| 1789 | o_addchr(o, *str); |
| 1790 | if (*str++ == '\\' |
| 1791 | && (*str != '*' && *str != '?' && *str != '[') |
| 1792 | ) { |
| 1793 | o_addchr(o, '\\'); |
| 1794 | } |
| 1795 | len--; |
| 1796 | } |
| 1797 | } |
| 1798 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1799 | /* My analysis of quoting semantics tells me that state information |
| 1800 | * is associated with a destination, not a source. |
| 1801 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1802 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1803 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1804 | int sz = 1; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1805 | char *found = strchr("*?[\\", ch); |
| 1806 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1807 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1808 | o_grow_by(o, sz); |
| 1809 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1810 | o->data[o->length] = '\\'; |
| 1811 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1812 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1813 | o->data[o->length] = ch; |
| 1814 | o->length++; |
| 1815 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1818 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1819 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1820 | int sz = 1; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1821 | if (o->o_escape && strchr("*?[\\", ch)) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1822 | sz++; |
| 1823 | o->data[o->length] = '\\'; |
| 1824 | o->length++; |
| 1825 | } |
| 1826 | o_grow_by(o, sz); |
| 1827 | o->data[o->length] = ch; |
| 1828 | o->length++; |
| 1829 | o->data[o->length] = '\0'; |
| 1830 | } |
| 1831 | |
| 1832 | static void o_addQstr(o_string *o, const char *str, int len) |
| 1833 | { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1834 | if (!o->o_escape) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1835 | o_addblock(o, str, len); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1836 | return; |
| 1837 | } |
| 1838 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1839 | char ch; |
| 1840 | int sz; |
| 1841 | int ordinary_cnt = strcspn(str, "*?[\\"); |
| 1842 | if (ordinary_cnt > len) /* paranoia */ |
| 1843 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1844 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1845 | if (ordinary_cnt == len) |
| 1846 | return; |
| 1847 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1848 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1849 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1850 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1851 | sz = 1; |
| 1852 | if (ch) { /* it is necessarily one of "*?[\\" */ |
| 1853 | sz++; |
| 1854 | o->data[o->length] = '\\'; |
| 1855 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1856 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1857 | o_grow_by(o, sz); |
| 1858 | o->data[o->length] = ch; |
| 1859 | o->length++; |
| 1860 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1861 | } |
| 1862 | } |
| 1863 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1864 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1865 | * 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] | 1866 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1867 | * list[i] contains an INDEX (int!) into this string data. |
| 1868 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1869 | * but list[i]'s need not be modified. |
| 1870 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1871 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1872 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1873 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1874 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1875 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1876 | { |
| 1877 | char **list = (char**)o->data; |
| 1878 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1879 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1880 | |
| 1881 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1882 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1883 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1884 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1885 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1886 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1887 | o->data + (int)list[i] + string_start, |
| 1888 | o->data + (int)list[i] + string_start); |
| 1889 | i++; |
| 1890 | } |
| 1891 | if (n) { |
| 1892 | const char *p = o->data + (int)list[n - 1] + string_start; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1893 | indent(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1894 | 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] | 1895 | } |
| 1896 | } |
| 1897 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1898 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1899 | #endif |
| 1900 | |
| 1901 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1902 | * in list[n] so that it points past last stored byte so far. |
| 1903 | * It returns n+1. */ |
| 1904 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1905 | { |
| 1906 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1907 | int string_start; |
| 1908 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1909 | |
| 1910 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1911 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1912 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1913 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1914 | 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] | 1915 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1916 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1917 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 1918 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1919 | memmove(list + n + 0x10, list + n, string_len); |
| 1920 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1921 | } else { |
| 1922 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 1923 | n, string_len, string_start); |
| 1924 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1925 | } else { |
| 1926 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1927 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1928 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1929 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 1930 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1931 | o->has_empty_slot = 0; |
| 1932 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1933 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1934 | return n + 1; |
| 1935 | } |
| 1936 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1937 | /* "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] | 1938 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1939 | { |
| 1940 | char **list = (char**)o->data; |
| 1941 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1942 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1943 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1946 | /* o_glob performs globbing on last list[], saving each result |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1947 | * as a new list[]. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1948 | static int o_glob(o_string *o, int n) |
| 1949 | { |
| 1950 | glob_t globdata; |
| 1951 | int gr; |
| 1952 | char *pattern; |
| 1953 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1954 | 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] | 1955 | if (!o->data) |
| 1956 | return o_save_ptr_helper(o, n); |
| 1957 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1958 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1959 | if (!glob_needed(pattern)) { |
| 1960 | literal: |
| 1961 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1962 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1963 | return o_save_ptr_helper(o, n); |
| 1964 | } |
| 1965 | |
| 1966 | memset(&globdata, 0, sizeof(globdata)); |
| 1967 | gr = glob(pattern, 0, NULL, &globdata); |
| 1968 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 1969 | if (gr == GLOB_NOSPACE) |
| 1970 | bb_error_msg_and_die("out of memory during glob"); |
| 1971 | if (gr == GLOB_NOMATCH) { |
| 1972 | globfree(&globdata); |
| 1973 | goto literal; |
| 1974 | } |
| 1975 | if (gr != 0) { /* GLOB_ABORTED ? */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 1976 | /* TODO: testcase for bad glob pattern behavior */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1977 | bb_error_msg("glob(3) error %d on '%s'", gr, pattern); |
| 1978 | } |
| 1979 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 1980 | char **argv = globdata.gl_pathv; |
| 1981 | o->length = pattern - o->data; /* "forget" pattern */ |
| 1982 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1983 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1984 | n = o_save_ptr_helper(o, n); |
| 1985 | argv++; |
| 1986 | if (!*argv) |
| 1987 | break; |
| 1988 | } |
| 1989 | } |
| 1990 | globfree(&globdata); |
| 1991 | if (DEBUG_GLOB) |
| 1992 | debug_print_list("o_glob returning", o, n); |
| 1993 | return n; |
| 1994 | } |
| 1995 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1996 | /* If o->o_glob == 1, glob the string so far remembered. |
| 1997 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1998 | static int o_save_ptr(o_string *o, int n) |
| 1999 | { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2000 | if (o->o_glob) { /* if globbing is requested */ |
| 2001 | /* If o->has_empty_slot, list[n] was already globbed |
| 2002 | * (if it was requested back then when it was filled) |
| 2003 | * so don't do that again! */ |
| 2004 | if (!o->has_empty_slot) |
| 2005 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 2006 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2007 | return o_save_ptr_helper(o, n); |
| 2008 | } |
| 2009 | |
| 2010 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2011 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2012 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2013 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2014 | int string_start; |
| 2015 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2016 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 2017 | if (DEBUG_EXPAND) |
| 2018 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2019 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2020 | list = (char**)o->data; |
| 2021 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2022 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2023 | while (n) { |
| 2024 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 2025 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2026 | } |
| 2027 | return list; |
| 2028 | } |
| 2029 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2030 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2031 | /* Expansion can recurse */ |
| 2032 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2033 | static int process_command_subs(o_string *dest, const char *s); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2034 | #endif |
| 2035 | static char *expand_string_to_string(const char *str); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2036 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2037 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2038 | parse_stream_dquoted(dest, input, dquote_end) |
| 2039 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2040 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2041 | o_string *dest, |
| 2042 | struct in_str *input, |
| 2043 | int dquote_end); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2044 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2045 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 2046 | * all variable references within and returns a pointer to |
| 2047 | * a list of expanded strings, possibly with larger number |
| 2048 | * of strings. (Think VAR="a b"; echo $VAR). |
| 2049 | * This new list is allocated as a single malloc block. |
| 2050 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 2051 | * followed by strings themself. |
| 2052 | * Caller can deallocate entire list by single free(list). */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2053 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2054 | /* Store given string, finalizing the word and starting new one whenever |
| 2055 | * we encounter IFS char(s). This is used for expanding variable values. |
| 2056 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 2057 | 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] | 2058 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2059 | while (1) { |
| 2060 | int word_len = strcspn(str, G.ifs); |
| 2061 | if (word_len) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2062 | if (output->o_escape || !output->o_glob) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2063 | o_addQstr(output, str, word_len); |
| 2064 | else /* protect backslashes against globbing up :) */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2065 | o_addblock_duplicate_backslash(output, str, word_len); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2066 | str += word_len; |
| 2067 | } |
| 2068 | if (!*str) /* EOL - do not finalize word */ |
| 2069 | break; |
| 2070 | o_addchr(output, '\0'); |
| 2071 | debug_print_list("expand_on_ifs", output, n); |
| 2072 | n = o_save_ptr(output, n); |
| 2073 | str += strspn(str, G.ifs); /* skip ifs chars */ |
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 | debug_print_list("expand_on_ifs[1]", output, n); |
| 2076 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2079 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 2080 | * they are in double quotes, with the exception that they are not :). |
| 2081 | * Just the rules are similar: "expand only $var and `cmd`" |
| 2082 | * |
| 2083 | * Returns malloced string. |
| 2084 | * As an optimization, we return NULL if expansion is not needed. |
| 2085 | */ |
| 2086 | static char *expand_pseudo_dquoted(const char *str) |
| 2087 | { |
| 2088 | char *exp_str; |
| 2089 | struct in_str input; |
| 2090 | o_string dest = NULL_O_STRING; |
| 2091 | |
| 2092 | if (strchr(str, '$') == NULL |
| 2093 | #if ENABLE_HUSH_TICK |
| 2094 | && strchr(str, '`') == NULL |
| 2095 | #endif |
| 2096 | ) { |
| 2097 | return NULL; |
| 2098 | } |
| 2099 | |
| 2100 | /* We need to expand. Example: |
| 2101 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 2102 | */ |
| 2103 | setup_string_in_str(&input, str); |
| 2104 | parse_stream_dquoted(NULL, &dest, &input, EOF); |
| 2105 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
| 2106 | exp_str = expand_string_to_string(dest.data); |
| 2107 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 2108 | o_free_unsafe(&dest); |
| 2109 | return exp_str; |
| 2110 | } |
| 2111 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2112 | /* Expand all variable references in given string, adding words to list[] |
| 2113 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 2114 | * to be filled). This routine is extremely tricky: has to deal with |
| 2115 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 2116 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
| 2117 | 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] | 2118 | { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 2119 | /* or_mask is either 0 (normal case) or 0x80 - |
| 2120 | * expansion of right-hand side of assignment == 1-element expand. |
| 2121 | * It will also do no globbing, and thus we must not backslash-quote! |
| 2122 | */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2123 | char ored_ch; |
| 2124 | char *p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2125 | |
| 2126 | ored_ch = 0; |
| 2127 | |
| 2128 | debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg); |
| 2129 | debug_print_list("expand_vars_to_list", output, n); |
| 2130 | n = o_save_ptr(output, n); |
| 2131 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 2132 | |
| 2133 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2134 | char first_ch; |
| 2135 | int i; |
| 2136 | char *dyn_val = NULL; |
| 2137 | const char *val = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2138 | #if ENABLE_HUSH_TICK |
| 2139 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2140 | #endif |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2141 | #if ENABLE_SH_MATH_SUPPORT |
| 2142 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 2143 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2144 | o_addblock(output, arg, p - arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2145 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 2146 | arg = ++p; |
| 2147 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 2148 | |
| 2149 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
| 2150 | /* "$@" is special. Even if quoted, it can still |
| 2151 | * expand to nothing (not even an empty string) */ |
| 2152 | if ((first_ch & 0x7f) != '@') |
| 2153 | ored_ch |= first_ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2154 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2155 | switch (first_ch & 0x7f) { |
| 2156 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 2157 | case '$': /* pid */ |
| 2158 | val = utoa(G.root_pid); |
| 2159 | break; |
| 2160 | case '!': /* bg pid */ |
| 2161 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
| 2162 | break; |
| 2163 | case '?': /* exitcode */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 2164 | val = utoa(G.last_exitcode); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2165 | break; |
| 2166 | case '#': /* argc */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2167 | if (arg[1] != SPECIAL_VAR_SYMBOL) |
| 2168 | /* actually, it's a ${#var} */ |
| 2169 | goto case_default; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2170 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 2171 | break; |
| 2172 | case '*': |
| 2173 | case '@': |
| 2174 | i = 1; |
| 2175 | if (!G.global_argv[i]) |
| 2176 | break; |
| 2177 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
| 2178 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2179 | smallint sv = output->o_escape; |
| 2180 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2181 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2182 | while (G.global_argv[i]) { |
| 2183 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 2184 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 2185 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 2186 | /* this argv[] is not empty and not last: |
| 2187 | * put terminating NUL, start new word */ |
| 2188 | o_addchr(output, '\0'); |
| 2189 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 2190 | n = o_save_ptr(output, n); |
| 2191 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 2192 | } |
| 2193 | } |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2194 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2195 | } else |
| 2196 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
| 2197 | * and in this case should treat it like '$*' - see 'else...' below */ |
| 2198 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
| 2199 | while (1) { |
| 2200 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2201 | if (++i >= G.global_argc) |
| 2202 | break; |
| 2203 | o_addchr(output, '\0'); |
| 2204 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 2205 | n = o_save_ptr(output, n); |
| 2206 | } |
| 2207 | } else { /* quoted $*: add as one word */ |
| 2208 | while (1) { |
| 2209 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2210 | if (!G.global_argv[++i]) |
| 2211 | break; |
| 2212 | if (G.ifs[0]) |
| 2213 | o_addchr(output, G.ifs[0]); |
| 2214 | } |
| 2215 | } |
| 2216 | break; |
| 2217 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 2218 | /* "Empty variable", used to make "" etc to not disappear */ |
| 2219 | arg++; |
| 2220 | ored_ch = 0x80; |
| 2221 | break; |
| 2222 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2223 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2224 | *p = '\0'; |
| 2225 | arg++; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2226 | /* Can't just stuff it into output o_string, |
| 2227 | * expanded result may need to be globbed |
| 2228 | * and $IFS-splitted */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2229 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2230 | process_command_subs(&subst_result, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2231 | debug_printf_subst("SUBST RES '%s'\n", subst_result.data); |
| 2232 | val = subst_result.data; |
| 2233 | goto store_val; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2234 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2235 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2236 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2237 | arith_eval_hooks_t hooks; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2238 | arith_t res; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2239 | int errcode; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2240 | char *exp_str; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2241 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2242 | arg++; /* skip '+' */ |
| 2243 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2244 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2245 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2246 | exp_str = expand_pseudo_dquoted(arg); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 2247 | hooks.lookupvar = get_local_var_value; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2248 | hooks.setvar = arith_set_local_var; |
| 2249 | hooks.endofname = endofname; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2250 | res = arith(exp_str ? exp_str : arg, &errcode, &hooks); |
| 2251 | free(exp_str); |
| 2252 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2253 | if (errcode < 0) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2254 | const char *msg = "error in arithmetic"; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2255 | switch (errcode) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2256 | case -3: |
| 2257 | msg = "exponent less than 0"; |
| 2258 | break; |
| 2259 | case -2: |
| 2260 | msg = "divide by 0"; |
| 2261 | break; |
| 2262 | case -5: |
| 2263 | msg = "expression recursion loop detected"; |
| 2264 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2265 | } |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2266 | die_if_script(msg); |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2267 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2268 | debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2269 | sprintf(arith_buf, arith_t_fmt, res); |
| 2270 | val = arith_buf; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2271 | break; |
| 2272 | } |
| 2273 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2274 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2275 | case_default: { |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2276 | bool exp_len = false; |
| 2277 | bool exp_null = false; |
| 2278 | char *var = arg; |
| 2279 | char exp_save = exp_save; /* for compiler */ |
| 2280 | char exp_op = exp_op; /* for compiler */ |
| 2281 | char *exp_word = exp_word; /* for compiler */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2282 | size_t exp_off = 0; |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2283 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2284 | *p = '\0'; |
| 2285 | arg[0] = first_ch & 0x7f; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2286 | |
| 2287 | /* prepare for expansions */ |
| 2288 | if (var[0] == '#') { |
| 2289 | /* handle length expansion ${#var} */ |
| 2290 | exp_len = true; |
| 2291 | ++var; |
| 2292 | } else { |
| 2293 | /* maybe handle parameter expansion */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2294 | exp_off = strcspn(var, ":-=+?%#"); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2295 | if (!var[exp_off]) |
| 2296 | exp_off = 0; |
| 2297 | if (exp_off) { |
| 2298 | exp_save = var[exp_off]; |
| 2299 | exp_null = exp_save == ':'; |
| 2300 | exp_word = var + exp_off; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2301 | if (exp_null) |
| 2302 | ++exp_word; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2303 | exp_op = *exp_word++; |
| 2304 | var[exp_off] = '\0'; |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | /* lookup the variable in question */ |
| 2309 | if (isdigit(var[0])) { |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 2310 | /* handle_dollar() should have vetted var for us */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2311 | i = xatoi_u(var); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2312 | if (i < G.global_argc) |
| 2313 | val = G.global_argv[i]; |
| 2314 | /* else val remains NULL: $N with too big N */ |
| 2315 | } else |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 2316 | val = get_local_var_value(var); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2317 | |
| 2318 | /* handle any expansions */ |
| 2319 | if (exp_len) { |
| 2320 | debug_printf_expand("expand: length of '%s' = ", val); |
| 2321 | val = utoa(val ? strlen(val) : 0); |
| 2322 | debug_printf_expand("%s\n", val); |
| 2323 | } else if (exp_off) { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2324 | if (exp_op == '%' || exp_op == '#') { |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2325 | if (val) { |
| 2326 | /* we need to do a pattern match */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2327 | bool match_at_left; |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2328 | char *loc; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2329 | scan_t scan = pick_scan(exp_op, *exp_word, &match_at_left); |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2330 | if (exp_op == *exp_word) /* ## or %% */ |
| 2331 | ++exp_word; |
| 2332 | val = dyn_val = xstrdup(val); |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2333 | loc = scan(dyn_val, exp_word, match_at_left); |
| 2334 | if (match_at_left) /* # or ## */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2335 | val = loc; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2336 | else if (loc) /* % or %% and match was found */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2337 | *loc = '\0'; |
| 2338 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2339 | } else { |
| 2340 | /* we need to do an expansion */ |
| 2341 | int exp_test = (!val || (exp_null && !val[0])); |
| 2342 | if (exp_op == '+') |
| 2343 | exp_test = !exp_test; |
| 2344 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 2345 | exp_null ? "true" : "false", exp_test); |
| 2346 | if (exp_test) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2347 | if (exp_op == '?') { |
| 2348 | //TODO: how interactive bash aborts expansion mid-command? |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2349 | /* ${var?[error_msg_if_unset]} */ |
| 2350 | /* ${var:?[error_msg_if_unset_or_null]} */ |
| 2351 | /* mimic bash message */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2352 | die_if_script("%s: %s", |
| 2353 | var, |
| 2354 | exp_word[0] ? exp_word : "parameter null or not set" |
| 2355 | ); |
| 2356 | } else { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2357 | val = exp_word; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2358 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2359 | |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2360 | if (exp_op == '=') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2361 | /* ${var=[word]} or ${var:=[word]} */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2362 | if (isdigit(var[0]) || var[0] == '#') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2363 | /* mimic bash message */ |
| 2364 | die_if_script("$%s: cannot assign in this way", var); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2365 | val = NULL; |
| 2366 | } else { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2367 | char *new_var = xasprintf("%s=%s", var, val); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2368 | set_local_var(new_var, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2369 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2370 | } |
| 2371 | } |
| 2372 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2373 | |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2374 | var[exp_off] = exp_save; |
| 2375 | } |
| 2376 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2377 | arg[0] = first_ch; |
| 2378 | #if ENABLE_HUSH_TICK |
| 2379 | store_val: |
| 2380 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2381 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2382 | 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] | 2383 | if (val) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2384 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2385 | smallint sv = output->o_escape; |
| 2386 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2387 | n = expand_on_ifs(output, n, val); |
| 2388 | val = NULL; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2389 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2390 | } |
| 2391 | } else { /* quoted $VAR, val will be appended below */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2392 | 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] | 2393 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2394 | } /* default: */ |
| 2395 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2396 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2397 | if (val) { |
| 2398 | o_addQstr(output, val, strlen(val)); |
| 2399 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2400 | free(dyn_val); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2401 | /* Do the check to avoid writing to a const string */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2402 | if (*p != SPECIAL_VAR_SYMBOL) |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2403 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2404 | |
| 2405 | #if ENABLE_HUSH_TICK |
| 2406 | o_free(&subst_result); |
| 2407 | #endif |
| 2408 | arg = ++p; |
| 2409 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 2410 | |
| 2411 | if (arg[0]) { |
| 2412 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 2413 | /* this part is literal, and it was already pre-quoted |
| 2414 | * if needed (much earlier), do not use o_addQstr here! */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2415 | o_addstr_with_NUL(output, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2416 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 2417 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 2418 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 2419 | ) { |
| 2420 | n--; |
| 2421 | /* allow to reuse list[n] later without re-growth */ |
| 2422 | output->has_empty_slot = 1; |
| 2423 | } else { |
| 2424 | o_addchr(output, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2425 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2426 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2429 | static char **expand_variables(char **argv, int or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2430 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2431 | int n; |
| 2432 | char **list; |
| 2433 | char **v; |
| 2434 | o_string output = NULL_O_STRING; |
| 2435 | |
| 2436 | if (or_mask & 0x100) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2437 | output.o_escape = 1; /* protect against globbing for "$var" */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2438 | /* (unquoted $var will temporarily switch it off) */ |
| 2439 | output.o_glob = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2440 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2441 | |
| 2442 | n = 0; |
| 2443 | v = argv; |
| 2444 | while (*v) { |
| 2445 | n = expand_vars_to_list(&output, n, *v, (char)or_mask); |
| 2446 | v++; |
| 2447 | } |
| 2448 | debug_print_list("expand_variables", &output, n); |
| 2449 | |
| 2450 | /* output.data (malloced in one block) gets returned in "list" */ |
| 2451 | list = o_finalize_list(&output, n); |
| 2452 | debug_print_strings("expand_variables[1]", list); |
| 2453 | return list; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2456 | static char **expand_strvec_to_strvec(char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2457 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2458 | return expand_variables(argv, 0x100); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2461 | /* Used for expansion of right hand of assignments */ |
| 2462 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 2463 | * "v=/bin/c*" */ |
| 2464 | static char *expand_string_to_string(const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2465 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2466 | char *argv[2], **list; |
| 2467 | |
| 2468 | argv[0] = (char*)str; |
| 2469 | argv[1] = NULL; |
| 2470 | list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */ |
| 2471 | if (HUSH_DEBUG) |
| 2472 | if (!list[0] || list[1]) |
| 2473 | bb_error_msg_and_die("BUG in varexp2"); |
| 2474 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 2475 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 2476 | unbackslash((char*)list); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2477 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 2478 | return (char*)list; |
| 2479 | } |
| 2480 | |
| 2481 | /* Used for "eval" builtin */ |
| 2482 | static char* expand_strvec_to_string(char **argv) |
| 2483 | { |
| 2484 | char **list; |
| 2485 | |
| 2486 | list = expand_variables(argv, 0x80); |
| 2487 | /* Convert all NULs to spaces */ |
| 2488 | if (list[0]) { |
| 2489 | int n = 1; |
| 2490 | while (list[n]) { |
| 2491 | if (HUSH_DEBUG) |
| 2492 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 2493 | bb_error_msg_and_die("BUG in varexp3"); |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 2494 | /* bash uses ' ' regardless of $IFS contents */ |
| 2495 | list[n][-1] = ' '; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2496 | n++; |
| 2497 | } |
| 2498 | } |
| 2499 | overlapping_strcpy((char*)list, list[0]); |
| 2500 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 2501 | return (char*)list; |
| 2502 | } |
| 2503 | |
| 2504 | static char **expand_assignments(char **argv, int count) |
| 2505 | { |
| 2506 | int i; |
| 2507 | char **p = NULL; |
| 2508 | /* Expand assignments into one string each */ |
| 2509 | for (i = 0; i < count; i++) { |
| 2510 | p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
| 2511 | } |
| 2512 | return p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2515 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2516 | #if BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2517 | /* never called */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2518 | void re_execute_shell(char ***to_free, const char *s, |
| 2519 | char *g_argv0, char **g_argv, |
| 2520 | char **builtin_argv) NORETURN; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2521 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2522 | static void reset_traps_to_defaults(void) |
| 2523 | { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2524 | /* This function is always called in a child shell |
| 2525 | * after fork (not vfork, NOMMU doesn't use this function). |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2526 | * Child shells are not interactive. |
| 2527 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 2528 | * Testcase: (while :; do :; done) + ^Z should background. |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2529 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2530 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2531 | unsigned sig; |
| 2532 | unsigned mask; |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2533 | |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2534 | if (!G.traps && !(G.non_DFL_mask & SPECIAL_INTERACTIVE_SIGS)) |
| 2535 | return; |
| 2536 | |
| 2537 | /* Stupid. It can be done with *single* &= op, but we can't use |
| 2538 | * the fact that G.blocked_set is implemented as a bitmask... */ |
| 2539 | mask = (SPECIAL_INTERACTIVE_SIGS >> 1); |
| 2540 | sig = 1; |
| 2541 | while (1) { |
| 2542 | if (mask & 1) |
| 2543 | sigdelset(&G.blocked_set, sig); |
| 2544 | mask >>= 1; |
| 2545 | if (!mask) |
| 2546 | break; |
| 2547 | sig++; |
| 2548 | } |
| 2549 | |
| 2550 | G.non_DFL_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
| 2551 | mask = G.non_DFL_mask; |
| 2552 | if (G.traps) for (sig = 0; sig < NSIG; sig++, mask >>= 1) { |
| 2553 | if (!G.traps[sig]) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2554 | continue; |
| 2555 | free(G.traps[sig]); |
| 2556 | G.traps[sig] = NULL; |
| 2557 | /* There is no signal for 0 (EXIT) */ |
| 2558 | if (sig == 0) |
| 2559 | continue; |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2560 | /* There was a trap handler, we are removing it. |
| 2561 | * But if sig still has non-DFL handling, |
| 2562 | * we should not unblock it. */ |
| 2563 | if (mask & 1) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2564 | continue; |
| 2565 | sigdelset(&G.blocked_set, sig); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2566 | } |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2567 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | #else /* !BB_MMU */ |
| 2571 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2572 | static void re_execute_shell(char ***to_free, const char *s, |
| 2573 | char *g_argv0, char **g_argv, |
| 2574 | char **builtin_argv) NORETURN; |
| 2575 | static void re_execute_shell(char ***to_free, const char *s, |
| 2576 | char *g_argv0, char **g_argv, |
| 2577 | char **builtin_argv) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2578 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2579 | char param_buf[sizeof("-$%x:%x:%x:%x:%x") + sizeof(unsigned) * 2]; |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2580 | char *heredoc_argv[4]; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2581 | struct variable *cur; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2582 | #if ENABLE_HUSH_FUNCTIONS |
| 2583 | struct function *funcp; |
| 2584 | #endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2585 | char **argv, **pp; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2586 | unsigned cnt; |
| 2587 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2588 | if (!g_argv0) { /* heredoc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2589 | argv = heredoc_argv; |
| 2590 | argv[0] = (char *) G.argv0_for_re_execing; |
| 2591 | argv[1] = (char *) "-<"; |
| 2592 | argv[2] = (char *) s; |
| 2593 | argv[3] = NULL; |
Denis Vlasenko | f50caac | 2009-04-09 01:40:15 +0000 | [diff] [blame] | 2594 | pp = &argv[3]; /* used as pointer to empty environment */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2595 | goto do_exec; |
| 2596 | } |
| 2597 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2598 | cnt = 0; |
| 2599 | pp = builtin_argv; |
| 2600 | if (pp) while (*pp++) |
| 2601 | cnt++; |
| 2602 | |
| 2603 | sprintf(param_buf, "-$%x:%x:%x:%x" IF_HUSH_LOOPS(":%x") |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2604 | , (unsigned) G.root_pid |
| 2605 | , (unsigned) G.last_bg_pid |
| 2606 | , (unsigned) G.last_exitcode |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2607 | , cnt |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 2608 | IF_HUSH_LOOPS(, G.depth_of_loop) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2609 | ); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2610 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<depth> <vars...> <funcs...> |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2611 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2612 | */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2613 | cnt += 6; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2614 | for (cur = G.top_var; cur; cur = cur->next) { |
| 2615 | if (!cur->flg_export || cur->flg_read_only) |
| 2616 | cnt += 2; |
| 2617 | } |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2618 | #if ENABLE_HUSH_FUNCTIONS |
| 2619 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 2620 | cnt += 3; |
| 2621 | #endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2622 | pp = g_argv; |
| 2623 | while (*pp++) |
| 2624 | cnt++; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2625 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2626 | *pp++ = (char *) G.argv0_for_re_execing; |
| 2627 | *pp++ = param_buf; |
| 2628 | for (cur = G.top_var; cur; cur = cur->next) { |
| 2629 | if (cur->varstr == hush_version_str) |
| 2630 | continue; |
| 2631 | if (cur->flg_read_only) { |
| 2632 | *pp++ = (char *) "-R"; |
| 2633 | *pp++ = cur->varstr; |
| 2634 | } else if (!cur->flg_export) { |
| 2635 | *pp++ = (char *) "-V"; |
| 2636 | *pp++ = cur->varstr; |
| 2637 | } |
| 2638 | } |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2639 | #if ENABLE_HUSH_FUNCTIONS |
| 2640 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 2641 | *pp++ = (char *) "-F"; |
| 2642 | *pp++ = funcp->name; |
| 2643 | *pp++ = funcp->body_as_string; |
| 2644 | } |
| 2645 | #endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2646 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 2647 | * |
| 2648 | * However, POSIX says that subshells reset signals with traps |
| 2649 | * to SIG_DFL. |
| 2650 | * I tested bash-3.2 and it not only does that with true subshells |
| 2651 | * of the form ( list ), but with any forked children shells. |
| 2652 | * I set trap "echo W" WINCH; and then tried: |
| 2653 | * |
| 2654 | * { echo 1; sleep 20; echo 2; } & |
| 2655 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 2656 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 2657 | * |
| 2658 | * In all these cases sending SIGWINCH to the child shell |
| 2659 | * did not run the trap. If I add trap "echo V" WINCH; |
| 2660 | * _inside_ group (just before echo 1), it works. |
| 2661 | * |
| 2662 | * I conclude it means we don't need to pass active traps here. |
| 2663 | * exec syscall below resets them to SIG_DFL for us. |
| 2664 | */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2665 | *pp++ = (char *) "-c"; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2666 | *pp++ = (char *) s; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2667 | if (builtin_argv) { |
| 2668 | while (*++builtin_argv) |
| 2669 | *pp++ = *builtin_argv; |
| 2670 | *pp++ = (char *) ""; |
| 2671 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2672 | *pp++ = g_argv0; |
| 2673 | while (*g_argv) |
| 2674 | *pp++ = *g_argv++; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2675 | /* *pp = NULL; - is already there */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2676 | pp = environ; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2677 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2678 | do_exec: |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2679 | debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s); |
| 2680 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2681 | execve(bb_busybox_exec_path, argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2682 | /* Fallback. Useful for init=/bin/hush usage etc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2683 | if (argv[0][0] == '/') |
| 2684 | execve(argv[0], argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2685 | xfunc_error_retval = 127; |
| 2686 | bb_error_msg_and_die("can't re-execute the shell"); |
| 2687 | } |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2688 | #endif /* !BB_MMU */ |
| 2689 | |
| 2690 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2691 | static void setup_heredoc(struct redir_struct *redir) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2692 | { |
| 2693 | struct fd_pair pair; |
| 2694 | pid_t pid; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2695 | int len, written; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2696 | /* the _body_ of heredoc (misleading field name) */ |
| 2697 | const char *heredoc = redir->rd_filename; |
| 2698 | char *expanded; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2699 | #if !BB_MMU |
| 2700 | char **to_free; |
| 2701 | #endif |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2702 | |
| 2703 | expanded = NULL; |
| 2704 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
| 2705 | expanded = expand_pseudo_dquoted(heredoc); |
| 2706 | if (expanded) |
| 2707 | heredoc = expanded; |
| 2708 | } |
| 2709 | len = strlen(heredoc); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2710 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2711 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2712 | xpiped_pair(pair); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2713 | xmove_fd(pair.rd, redir->rd_fd); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2714 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2715 | /* Try writing without forking. Newer kernels have |
| 2716 | * dynamically growing pipes. Must use non-blocking write! */ |
| 2717 | ndelay_on(pair.wr); |
| 2718 | while (1) { |
| 2719 | written = write(pair.wr, heredoc, len); |
| 2720 | if (written <= 0) |
| 2721 | break; |
| 2722 | len -= written; |
| 2723 | if (len == 0) { |
| 2724 | close(pair.wr); |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 2725 | free(expanded); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2726 | return; |
| 2727 | } |
| 2728 | heredoc += written; |
| 2729 | } |
| 2730 | ndelay_off(pair.wr); |
| 2731 | |
| 2732 | /* Okay, pipe buffer was not big enough */ |
| 2733 | /* Note: we must not create a stray child (bastard? :) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2734 | * for the unsuspecting parent process. Child creates a grandchild |
| 2735 | * and exits before parent execs the process which consumes heredoc |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2736 | * (that exec happens after we return from this function) */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 2737 | #if !BB_MMU |
| 2738 | to_free = NULL; |
| 2739 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2740 | pid = vfork(); |
| 2741 | if (pid < 0) |
| 2742 | bb_perror_msg_and_die("vfork"); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2743 | if (pid == 0) { |
| 2744 | /* child */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 2745 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2746 | pid = BB_MMU ? fork() : vfork(); |
| 2747 | if (pid < 0) |
| 2748 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
| 2749 | if (pid != 0) |
| 2750 | _exit(0); |
| 2751 | /* grandchild */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2752 | close(redir->rd_fd); /* read side of the pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2753 | #if BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2754 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2755 | _exit(0); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2756 | #else |
| 2757 | /* Delegate blocking writes to another process */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2758 | xmove_fd(pair.wr, STDOUT_FILENO); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2759 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2760 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2761 | } |
| 2762 | /* parent */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 2763 | #if ENABLE_HUSH_FAST |
| 2764 | G.count_SIGCHLD++; |
| 2765 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 2766 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2767 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2768 | #if !BB_MMU |
| 2769 | free(to_free); |
| 2770 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2771 | close(pair.wr); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2772 | free(expanded); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2773 | wait(NULL); /* wait till child has died */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2776 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 2777 | * and stderr if they are redirected. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2778 | static int setup_redirects(struct command *prog, int squirrel[]) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2779 | { |
| 2780 | int openfd, mode; |
| 2781 | struct redir_struct *redir; |
| 2782 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2783 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2784 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2785 | /* rd_fd<<HERE case */ |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 2786 | if (squirrel && redir->rd_fd < 3 |
| 2787 | && squirrel[redir->rd_fd] < 0 |
| 2788 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2789 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2790 | } |
| 2791 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 2792 | * of the heredoc */ |
| 2793 | debug_printf_parse("set heredoc '%s'\n", |
| 2794 | redir->rd_filename); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2795 | setup_heredoc(redir); |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2796 | continue; |
| 2797 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2798 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2799 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
| 2800 | /* rd_fd<*>file case (<*> is <,>,>>,<>) */ |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2801 | char *p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2802 | if (redir->rd_filename == NULL) { |
| 2803 | /* Something went wrong in the parse. |
| 2804 | * Pretend it didn't happen */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2805 | bb_error_msg("bug in redirect parse"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2806 | continue; |
| 2807 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2808 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2809 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2810 | openfd = open_or_warn(p, mode); |
| 2811 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2812 | if (openfd < 0) { |
| 2813 | /* this could get lost if stderr has been redirected, but |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2814 | * bash and ash both lose it as well (though zsh doesn't!) */ |
| 2815 | //what the above comment tries to say? |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2816 | return 1; |
| 2817 | } |
| 2818 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2819 | /* rd_fd<*>rd_dup or rd_fd<*>- cases */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2820 | openfd = redir->rd_dup; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2823 | if (openfd != redir->rd_fd) { |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 2824 | if (squirrel && redir->rd_fd < 3 |
| 2825 | && squirrel[redir->rd_fd] < 0 |
| 2826 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2827 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2828 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2829 | if (openfd == REDIRFD_CLOSE) { |
| 2830 | /* "n>-" means "close me" */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2831 | close(redir->rd_fd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2832 | } else { |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2833 | xdup2(openfd, redir->rd_fd); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2834 | if (redir->rd_dup == REDIRFD_TO_FILE) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2835 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2836 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2837 | } |
| 2838 | } |
| 2839 | return 0; |
| 2840 | } |
| 2841 | |
| 2842 | static void restore_redirects(int squirrel[]) |
| 2843 | { |
| 2844 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2845 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2846 | fd = squirrel[i]; |
| 2847 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2848 | /* We simply die on error */ |
| 2849 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2850 | } |
| 2851 | } |
| 2852 | } |
| 2853 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2854 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2855 | static void free_pipe_list(struct pipe *head); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2856 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2857 | /* Return code is the exit status of the pipe */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2858 | static void free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2859 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2860 | char **p; |
| 2861 | struct command *command; |
| 2862 | struct redir_struct *r, *rnext; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2863 | int a, i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2864 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2865 | if (pi->stopped_cmds > 0) /* why? */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2866 | return; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2867 | debug_printf_clean("run pipe: (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2868 | for (i = 0; i < pi->num_cmds; i++) { |
| 2869 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2870 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2871 | if (command->argv) { |
| 2872 | for (a = 0, p = command->argv; *p; a++, p++) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2873 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2874 | } |
| 2875 | free_strings(command->argv); |
| 2876 | command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2877 | } |
| 2878 | /* not "else if": on syntax error, we may have both! */ |
| 2879 | if (command->group) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2880 | debug_printf_clean(" begin group (grp_type:%d)\n", |
| 2881 | command->grp_type); |
| 2882 | free_pipe_list(command->group); |
| 2883 | debug_printf_clean(" end group\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2884 | command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2885 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 2886 | /* else is crucial here. |
| 2887 | * If group != NULL, child_func is meaningless */ |
| 2888 | #if ENABLE_HUSH_FUNCTIONS |
| 2889 | else if (command->child_func) { |
| 2890 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 2891 | command->child_func->parent_cmd = NULL; |
| 2892 | } |
| 2893 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2894 | #if !BB_MMU |
| 2895 | free(command->group_as_string); |
| 2896 | command->group_as_string = NULL; |
| 2897 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2898 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2899 | debug_printf_clean(" redirect %d%s", |
| 2900 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2901 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 2902 | if (r->rd_filename) { |
| 2903 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 2904 | free(r->rd_filename); |
| 2905 | r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2906 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2907 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2908 | rnext = r->next; |
| 2909 | free(r); |
| 2910 | } |
| 2911 | command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2912 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2913 | free(pi->cmds); /* children are an array, they get freed all at once */ |
| 2914 | pi->cmds = NULL; |
| 2915 | #if ENABLE_HUSH_JOB |
| 2916 | free(pi->cmdtext); |
| 2917 | pi->cmdtext = NULL; |
| 2918 | #endif |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2919 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2920 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2921 | static void free_pipe_list(struct pipe *head) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2922 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2923 | struct pipe *pi, *next; |
| 2924 | |
| 2925 | for (pi = head; pi; pi = next) { |
| 2926 | #if HAS_KEYWORDS |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2927 | debug_printf_clean(" pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2928 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2929 | free_pipe(pi); |
| 2930 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2931 | next = pi->next; |
| 2932 | /*pi->next = NULL;*/ |
| 2933 | free(pi); |
| 2934 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2938 | static int run_list(struct pipe *pi); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2939 | #if BB_MMU |
| 2940 | #define parse_stream(pstring, input, end_trigger) \ |
| 2941 | parse_stream(input, end_trigger) |
| 2942 | #endif |
| 2943 | static struct pipe *parse_stream(char **pstring, |
| 2944 | struct in_str *input, |
| 2945 | int end_trigger); |
| 2946 | static void parse_and_run_string(const char *s); |
| 2947 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2948 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 2949 | static char *find_in_path(const char *arg) |
| 2950 | { |
| 2951 | char *ret = NULL; |
| 2952 | const char *PATH = get_local_var_value("PATH"); |
| 2953 | |
| 2954 | if (!PATH) |
| 2955 | return NULL; |
| 2956 | |
| 2957 | while (1) { |
| 2958 | const char *end = strchrnul(PATH, ':'); |
| 2959 | int sz = end - PATH; /* must be int! */ |
| 2960 | |
| 2961 | free(ret); |
| 2962 | if (sz != 0) { |
| 2963 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 2964 | } else { |
| 2965 | /* We have xxx::yyyy in $PATH, |
| 2966 | * it means "use current dir" */ |
| 2967 | ret = xstrdup(arg); |
| 2968 | } |
| 2969 | if (access(ret, F_OK) == 0) |
| 2970 | break; |
| 2971 | |
| 2972 | if (*end == '\0') { |
| 2973 | free(ret); |
| 2974 | return NULL; |
| 2975 | } |
| 2976 | PATH = end + 1; |
| 2977 | } |
| 2978 | |
| 2979 | return ret; |
| 2980 | } |
| 2981 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2982 | static const struct built_in_command* find_builtin_helper(const char *name, |
| 2983 | const struct built_in_command *x, |
| 2984 | const struct built_in_command *end) |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2985 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2986 | while (x != end) { |
| 2987 | if (strcmp(name, x->cmd) != 0) { |
| 2988 | x++; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2989 | continue; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2990 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2991 | debug_printf_exec("found builtin '%s'\n", name); |
| 2992 | return x; |
| 2993 | } |
| 2994 | return NULL; |
| 2995 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2996 | static const struct built_in_command* find_builtin1(const char *name) |
| 2997 | { |
| 2998 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 2999 | } |
| 3000 | static const struct built_in_command* find_builtin(const char *name) |
| 3001 | { |
| 3002 | const struct built_in_command *x = find_builtin1(name); |
| 3003 | if (x) |
| 3004 | return x; |
| 3005 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 3006 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3007 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3008 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3009 | static const struct function *find_function(const char *name) |
| 3010 | { |
| 3011 | const struct function *funcp = G.top_func; |
| 3012 | while (funcp) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3013 | if (strcmp(name, funcp->name) == 0) { |
| 3014 | break; |
| 3015 | } |
| 3016 | funcp = funcp->next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3017 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3018 | if (funcp) |
| 3019 | debug_printf_exec("found function '%s'\n", name); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3020 | return funcp; |
| 3021 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3022 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3023 | /* Note: takes ownership on name ptr */ |
| 3024 | static struct function *new_function(char *name) |
| 3025 | { |
| 3026 | struct function *funcp; |
| 3027 | struct function **funcpp = &G.top_func; |
| 3028 | |
| 3029 | while ((funcp = *funcpp) != NULL) { |
| 3030 | struct command *cmd; |
| 3031 | |
| 3032 | if (strcmp(funcp->name, name) != 0) { |
| 3033 | funcpp = &funcp->next; |
| 3034 | continue; |
| 3035 | } |
| 3036 | |
| 3037 | cmd = funcp->parent_cmd; |
| 3038 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 3039 | if (!cmd) { |
| 3040 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 3041 | free(funcp->name); |
| 3042 | /* Note: if !funcp->body, do not free body_as_string! |
| 3043 | * This is a special case of "-F name body" function: |
| 3044 | * body_as_string was not malloced! */ |
| 3045 | if (funcp->body) { |
| 3046 | free_pipe_list(funcp->body); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3047 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3048 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3049 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3050 | } |
| 3051 | } else { |
| 3052 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 3053 | cmd->argv[0] = funcp->name; |
| 3054 | cmd->group = funcp->body; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3055 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3056 | cmd->group_as_string = funcp->body_as_string; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3057 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3058 | } |
| 3059 | goto skip; |
| 3060 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3061 | debug_printf_exec("remembering new function '%s'\n", name); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3062 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 3063 | /*funcp->next = NULL;*/ |
| 3064 | skip: |
| 3065 | funcp->name = name; |
| 3066 | return funcp; |
| 3067 | } |
| 3068 | |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3069 | static void unset_func(const char *name) |
| 3070 | { |
| 3071 | struct function *funcp; |
| 3072 | struct function **funcpp = &G.top_func; |
| 3073 | |
| 3074 | while ((funcp = *funcpp) != NULL) { |
| 3075 | if (strcmp(funcp->name, name) == 0) { |
| 3076 | *funcpp = funcp->next; |
root | 6245202 | 2009-05-04 12:00:19 +0200 | [diff] [blame] | 3077 | /* funcp is unlinked now, deleting it. |
| 3078 | * Note: if !funcp->body, the function was created by |
| 3079 | * "-F name body", do not free ->body_as_string |
| 3080 | * and ->name as they were not malloced. */ |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3081 | if (funcp->body) { |
| 3082 | free_pipe_list(funcp->body); |
root | 6245202 | 2009-05-04 12:00:19 +0200 | [diff] [blame] | 3083 | free(funcp->name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3084 | # if !BB_MMU |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3085 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3086 | # endif |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3087 | } |
| 3088 | free(funcp); |
| 3089 | break; |
| 3090 | } |
Denis Vlasenko | 7301067 | 2009-04-18 11:25:18 +0000 | [diff] [blame] | 3091 | funcpp = &funcp->next; |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3092 | } |
| 3093 | } |
| 3094 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3095 | # if BB_MMU |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3096 | #define exec_function(nommu_save, funcp, argv) \ |
| 3097 | exec_function(funcp, argv) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3098 | # endif |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3099 | static void exec_function(nommu_save_t *nommu_save, |
| 3100 | const struct function *funcp, |
| 3101 | char **argv) NORETURN; |
| 3102 | static void exec_function(nommu_save_t *nommu_save, |
| 3103 | const struct function *funcp, |
| 3104 | char **argv) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3105 | { |
| 3106 | # if BB_MMU |
| 3107 | int n = 1; |
| 3108 | |
| 3109 | argv[0] = G.global_argv[0]; |
| 3110 | G.global_argv = argv; |
| 3111 | while (*++argv) |
| 3112 | n++; |
| 3113 | G.global_argc = n; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3114 | /* On MMU, funcp->body is always non-NULL */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3115 | n = run_list(funcp->body); |
| 3116 | fflush(NULL); |
| 3117 | _exit(n); |
| 3118 | # else |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3119 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 3120 | funcp->body_as_string, |
| 3121 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3122 | argv + 1, |
| 3123 | NULL); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3124 | # endif |
| 3125 | } |
| 3126 | |
| 3127 | static int run_function(const struct function *funcp, char **argv) |
| 3128 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3129 | int rc; |
| 3130 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 3131 | smallint sv_flg; |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3132 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3133 | save_and_replace_G_args(&sv, argv); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3134 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3135 | /* "we are in function, ok to use return" */ |
| 3136 | sv_flg = G.flag_return_in_progress; |
| 3137 | G.flag_return_in_progress = -1; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3138 | #if ENABLE_HUSH_LOCAL |
| 3139 | G.func_nest_level++; |
| 3140 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3141 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3142 | /* On MMU, funcp->body is always non-NULL */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3143 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3144 | if (!funcp->body) { |
| 3145 | /* Function defined by -F */ |
| 3146 | parse_and_run_string(funcp->body_as_string); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3147 | rc = G.last_exitcode; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3148 | } else |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3149 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3150 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3151 | rc = run_list(funcp->body); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3152 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3153 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3154 | #if ENABLE_HUSH_LOCAL |
| 3155 | { |
| 3156 | struct variable *var; |
| 3157 | struct variable **var_pp; |
| 3158 | |
| 3159 | var_pp = &G.top_var; |
| 3160 | while ((var = *var_pp) != NULL) { |
| 3161 | if (var->func_nest_level < G.func_nest_level) { |
| 3162 | var_pp = &var->next; |
| 3163 | continue; |
| 3164 | } |
| 3165 | /* Unexport */ |
| 3166 | if (var->flg_export) |
| 3167 | bb_unsetenv(var->varstr); |
| 3168 | /* Remove from global list */ |
| 3169 | *var_pp = var->next; |
| 3170 | /* Free */ |
| 3171 | if (!var->max_len) |
| 3172 | free(var->varstr); |
| 3173 | free(var); |
| 3174 | } |
| 3175 | G.func_nest_level--; |
| 3176 | } |
| 3177 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3178 | G.flag_return_in_progress = sv_flg; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3179 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3180 | restore_G_args(&sv, argv); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3181 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3182 | return rc; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3183 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3184 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3185 | |
| 3186 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3187 | # if BB_MMU |
| 3188 | #define exec_builtin(nommu_save, x, argv) \ |
| 3189 | exec_builtin(x, argv) |
| 3190 | # else |
| 3191 | #define exec_builtin(nommu_save, x, argv) \ |
| 3192 | exec_builtin(nommu_save, argv) |
| 3193 | # endif |
| 3194 | static void exec_builtin(nommu_save_t *nommu_save, |
| 3195 | const struct built_in_command *x, |
| 3196 | char **argv) NORETURN; |
| 3197 | static void exec_builtin(nommu_save_t *nommu_save, |
| 3198 | const struct built_in_command *x, |
| 3199 | char **argv) |
| 3200 | { |
| 3201 | # if BB_MMU |
| 3202 | int rcode = x->function(argv); |
| 3203 | fflush(NULL); |
| 3204 | _exit(rcode); |
| 3205 | # else |
| 3206 | /* On NOMMU, we must never block! |
| 3207 | * Example: { sleep 99 | read line; } & echo Ok |
| 3208 | */ |
| 3209 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 3210 | argv[0], |
| 3211 | G.global_argv[0], |
| 3212 | G.global_argv + 1, |
| 3213 | argv); |
| 3214 | # endif |
| 3215 | } |
| 3216 | |
| 3217 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 3218 | #if BB_MMU |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3219 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 3220 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 3221 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 3222 | pseudo_exec(command, argv_expanded) |
| 3223 | #endif |
| 3224 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3225 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3226 | * Never returns. |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3227 | * Don't exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3228 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3229 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3230 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 3231 | char **argv, int assignment_cnt, |
| 3232 | char **argv_expanded) NORETURN; |
| 3233 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 3234 | char **argv, int assignment_cnt, |
| 3235 | char **argv_expanded) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3236 | { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3237 | char **new_env; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3238 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3239 | /* Case when we are here: ... | var=val | ... */ |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3240 | if (!argv[assignment_cnt]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3241 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3242 | |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 3243 | new_env = expand_assignments(argv, assignment_cnt); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3244 | #if BB_MMU |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3245 | set_vars_and_save_old(new_env); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3246 | free(new_env); /* optional */ |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3247 | /* we can also destroy set_vars_and_save_old's return value, |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3248 | * to save memory */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3249 | #else |
| 3250 | nommu_save->new_env = new_env; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3251 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3252 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3253 | if (argv_expanded) { |
| 3254 | argv = argv_expanded; |
| 3255 | } else { |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 3256 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3257 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3258 | nommu_save->argv = argv; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3259 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3260 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 3261 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3262 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 3263 | if (strchr(argv[0], '/') != NULL) |
| 3264 | goto skip; |
| 3265 | #endif |
| 3266 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3267 | /* Check if the command matches any of the builtins. |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3268 | * Depending on context, this might be redundant. But it's |
| 3269 | * easier to waste a few CPU cycles than it is to figure out |
| 3270 | * if this is one of those cases. |
| 3271 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3272 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3273 | /* On NOMMU, it is more expensive to re-execute shell |
| 3274 | * just in order to run echo or test builtin. |
| 3275 | * It's better to skip it here and run corresponding |
| 3276 | * non-builtin later. */ |
| 3277 | const struct built_in_command *x; |
| 3278 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3279 | if (x) { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3280 | exec_builtin(nommu_save, x, argv); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3281 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3282 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3283 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3284 | /* Check if the command matches any functions */ |
| 3285 | { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3286 | const struct function *funcp = find_function(argv[0]); |
| 3287 | if (funcp) { |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3288 | exec_function(nommu_save, funcp, argv); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3289 | } |
| 3290 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3291 | #endif |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3292 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 3293 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3294 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3295 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3296 | int a = find_applet_by_name(argv[0]); |
| 3297 | if (a >= 0) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3298 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3299 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3300 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3301 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3302 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3303 | # endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3304 | /* Re-exec ourselves */ |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3305 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3306 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 3307 | execv(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3308 | /* If they called chroot or otherwise made the binary no longer |
| 3309 | * executable, fall through */ |
| 3310 | } |
| 3311 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 3312 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3313 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3314 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3315 | skip: |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3316 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3317 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3318 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3319 | execvp(argv[0], argv); |
Denis Vlasenko | f9d4fc3 | 2009-04-21 20:40:51 +0000 | [diff] [blame] | 3320 | bb_perror_msg("can't execute '%s'", argv[0]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 3321 | _exit(EXIT_FAILURE); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3324 | /* Called after [v]fork() in run_pipe |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3325 | */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3326 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3327 | struct command *command, |
| 3328 | char **argv_expanded) NORETURN; |
| 3329 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3330 | struct command *command, |
| 3331 | char **argv_expanded) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3332 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3333 | if (command->argv) { |
| 3334 | pseudo_exec_argv(nommu_save, command->argv, |
| 3335 | command->assignment_cnt, argv_expanded); |
| 3336 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3337 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3338 | if (command->group) { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3339 | /* Cases when we are here: |
| 3340 | * ( list ) |
| 3341 | * { list } & |
| 3342 | * ... | ( list ) | ... |
| 3343 | * ... | { list } | ... |
| 3344 | */ |
| 3345 | #if BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 3346 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3347 | debug_printf_exec("pseudo_exec: run_list\n"); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3348 | reset_traps_to_defaults(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3349 | rcode = run_list(command->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 3350 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3351 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3352 | _exit(rcode); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3353 | #else |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3354 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 3355 | command->group_as_string, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3356 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3357 | G.global_argv + 1, |
| 3358 | NULL); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3359 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3360 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3361 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3362 | /* Case when we are here: ... | >file */ |
| 3363 | debug_printf_exec("pseudo_exec'ed null command\n"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3364 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3365 | } |
| 3366 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3367 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3368 | static const char *get_cmdtext(struct pipe *pi) |
| 3369 | { |
| 3370 | char **argv; |
| 3371 | char *p; |
| 3372 | int len; |
| 3373 | |
| 3374 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 3375 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3376 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3377 | if (pi->cmdtext) |
| 3378 | return pi->cmdtext; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3379 | argv = pi->cmds[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3380 | if (!argv || !argv[0]) { |
| 3381 | pi->cmdtext = xzalloc(1); |
| 3382 | return pi->cmdtext; |
| 3383 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3384 | |
| 3385 | len = 0; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3386 | do { |
| 3387 | len += strlen(*argv) + 1; |
| 3388 | } while (*++argv); |
| 3389 | p = xmalloc(len); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3390 | pi->cmdtext = p; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3391 | argv = pi->cmds[0].argv; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3392 | do { |
| 3393 | len = strlen(*argv); |
| 3394 | memcpy(p, *argv, len); |
| 3395 | p += len; |
| 3396 | *p++ = ' '; |
| 3397 | } while (*++argv); |
| 3398 | p[-1] = '\0'; |
| 3399 | return pi->cmdtext; |
| 3400 | } |
| 3401 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3402 | static void insert_bg_job(struct pipe *pi) |
| 3403 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3404 | struct pipe *job, **jobp; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3405 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3406 | |
| 3407 | /* Linear search for the ID of the job to use */ |
| 3408 | pi->jobid = 1; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3409 | for (job = G.job_list; job; job = job->next) |
| 3410 | if (job->jobid >= pi->jobid) |
| 3411 | pi->jobid = job->jobid + 1; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3412 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3413 | /* Add job to the list of running jobs */ |
| 3414 | jobp = &G.job_list; |
| 3415 | while ((job = *jobp) != NULL) |
| 3416 | jobp = &job->next; |
| 3417 | job = *jobp = xmalloc(sizeof(*job)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3418 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3419 | *job = *pi; /* physical copy */ |
| 3420 | job->next = NULL; |
| 3421 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 3422 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3423 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3424 | job->cmds[i].pid = pi->cmds[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3425 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3426 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3427 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3428 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3429 | if (G_interactive_fd) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3430 | printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext); |
| 3431 | /* Last command's pid goes to $! */ |
| 3432 | G.last_bg_pid = job->cmds[job->num_cmds - 1].pid; |
| 3433 | G.last_jobid = job->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3434 | } |
| 3435 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3436 | static void remove_bg_job(struct pipe *pi) |
| 3437 | { |
| 3438 | struct pipe *prev_pipe; |
| 3439 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3440 | if (pi == G.job_list) { |
| 3441 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3442 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3443 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3444 | while (prev_pipe->next != pi) |
| 3445 | prev_pipe = prev_pipe->next; |
| 3446 | prev_pipe->next = pi->next; |
| 3447 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3448 | if (G.job_list) |
| 3449 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3450 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3451 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3452 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3453 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3454 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3455 | static void delete_finished_bg_job(struct pipe *pi) |
| 3456 | { |
| 3457 | remove_bg_job(pi); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3458 | pi->stopped_cmds = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3459 | free_pipe(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3460 | free(pi); |
| 3461 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3462 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3463 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3464 | /* Check to see if any processes have exited -- if they |
| 3465 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3466 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3467 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3468 | int attributes; |
| 3469 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3470 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3471 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3472 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3473 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3474 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3475 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3476 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 3477 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3478 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3479 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3480 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3481 | |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3482 | errno = 0; |
| 3483 | #if ENABLE_HUSH_FAST |
| 3484 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 3485 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 3486 | //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] | 3487 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 3488 | /* Avoid doing waitpid syscall if possible */ |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3489 | if (!G.we_have_children) { |
| 3490 | errno = ECHILD; |
| 3491 | return -1; |
| 3492 | } |
| 3493 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 3494 | /* We have children, but they did not exit |
| 3495 | * or stop yet (we saw no SIGCHLD) */ |
| 3496 | return 0; |
| 3497 | } |
| 3498 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 3499 | } |
| 3500 | #endif |
| 3501 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3502 | /* Do we do this right? |
| 3503 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3504 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3505 | * [3]+ Stopped sleep 20 | false |
| 3506 | * bash-3.00# echo $? |
| 3507 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3508 | * [hush 1.14.0: yes we do it right] |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3509 | */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3510 | wait_more: |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3511 | while (1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3512 | int i; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3513 | int dead; |
| 3514 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3515 | #if ENABLE_HUSH_FAST |
| 3516 | i = G.count_SIGCHLD; |
| 3517 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3518 | childpid = waitpid(-1, &status, attributes); |
| 3519 | if (childpid <= 0) { |
| 3520 | if (childpid && errno != ECHILD) |
| 3521 | bb_perror_msg("waitpid"); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3522 | #if ENABLE_HUSH_FAST |
| 3523 | else { /* Until next SIGCHLD, waitpid's are useless */ |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3524 | G.we_have_children = (childpid == 0); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3525 | G.handled_SIGCHLD = i; |
| 3526 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 3527 | } |
| 3528 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3529 | break; |
| 3530 | } |
| 3531 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 3532 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3533 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3534 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3535 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3536 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 3537 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3538 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3539 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 3540 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3541 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3542 | childpid, WEXITSTATUS(status)); |
| 3543 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3544 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3545 | if (fg_pipe) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3546 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 3547 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 3548 | if (fg_pipe->cmds[i].pid != childpid) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3549 | continue; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3550 | if (dead) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3551 | fg_pipe->cmds[i].pid = 0; |
| 3552 | fg_pipe->alive_cmds--; |
| 3553 | if (i == fg_pipe->num_cmds - 1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3554 | /* last process gives overall exitstatus */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3555 | /* Note: is WIFSIGNALED, WEXITSTATUS = sig + 128 */ |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3556 | rcode = WEXITSTATUS(status); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3557 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3558 | /* bash prints killing signal's name for *last* |
| 3559 | * process in pipe (prints just newline for SIGINT). |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3560 | * Mimic this. Example: "sleep 5" + ^\ |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3561 | */ |
| 3562 | if (WIFSIGNALED(status)) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3563 | int sig = WTERMSIG(status); |
| 3564 | printf("%s\n", sig == SIGINT ? "" : get_signame(sig)); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3565 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3566 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3567 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3568 | fg_pipe->cmds[i].is_stopped = 1; |
| 3569 | fg_pipe->stopped_cmds++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3570 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3571 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 3572 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 3573 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3574 | /* All processes in fg pipe have exited or stopped */ |
| 3575 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 3576 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 3577 | * and "killall -STOP cat" */ |
| 3578 | if (G_interactive_fd) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3579 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3580 | if (fg_pipe->alive_cmds) |
| 3581 | insert_bg_job(fg_pipe); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3582 | #endif |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3583 | return rcode; |
| 3584 | } |
Denys Vlasenko | 6f22624 | 2009-06-03 14:37:30 +0200 | [diff] [blame] | 3585 | if (!fg_pipe->alive_cmds) |
| 3586 | return rcode; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3587 | } |
| 3588 | /* There are still running processes in the fg pipe */ |
| 3589 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3590 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3591 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3594 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3595 | /* We asked to wait for bg or orphaned children */ |
| 3596 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3597 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3598 | for (i = 0; i < pi->num_cmds; i++) { |
| 3599 | if (pi->cmds[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3600 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3601 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3602 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3603 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 3604 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3605 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 3606 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3607 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3608 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3609 | /* child exited */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3610 | pi->cmds[i].pid = 0; |
| 3611 | pi->alive_cmds--; |
| 3612 | if (!pi->alive_cmds) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3613 | if (G_interactive_fd) |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 3614 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 3615 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3616 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3617 | } |
| 3618 | } else { |
| 3619 | /* child stopped */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3620 | pi->cmds[i].is_stopped = 1; |
| 3621 | pi->stopped_cmds++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3622 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3623 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3624 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3625 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3626 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 3627 | } |
| 3628 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3629 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3630 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 3631 | { |
| 3632 | pid_t p; |
| 3633 | int rcode = checkjobs(fg_pipe); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 3634 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3635 | /* Job finished, move the shell to the foreground */ |
| 3636 | p = getpgrp(); /* our process group id */ |
| 3637 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 3638 | tcsetpgrp(G_interactive_fd, p); |
| 3639 | } |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3640 | return rcode; |
| 3641 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3642 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3643 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3644 | /* Start all the jobs, but don't wait for anything to finish. |
| 3645 | * See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3646 | * |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3647 | * 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] | 3648 | * to finish to determine the exit status of the pipe. If the pipe |
| 3649 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3650 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3651 | * return value. |
| 3652 | * |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3653 | * Returns -1 only if started some children. IOW: we have to |
| 3654 | * mask out retvals of builtins etc with 0xff! |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3655 | * |
| 3656 | * The only case when we do not need to [v]fork is when the pipe |
| 3657 | * is single, non-backgrounded, non-subshell command. Examples: |
| 3658 | * cmd ; ... { list } ; ... |
| 3659 | * cmd && ... { list } && ... |
| 3660 | * cmd || ... { list } || ... |
| 3661 | * If it is, then we can run cmd as a builtin, NOFORK [do we do this?], |
| 3662 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3663 | * 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] | 3664 | * |
| 3665 | * Cases when we must fork: |
| 3666 | * non-single: cmd | cmd |
| 3667 | * backgrounded: cmd & { list } & |
| 3668 | * subshell: ( list ) [&] |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3669 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3670 | static int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3671 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3672 | static const char *const null_ptr = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3673 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3674 | int nextin; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3675 | struct command *command; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3676 | char **argv_expanded; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3677 | char **argv; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3678 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3679 | /* it is not always needed, but we aim to smaller code */ |
| 3680 | int squirrel[] = { -1, -1, -1 }; |
| 3681 | int rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3682 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3683 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3684 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 3685 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 3686 | IF_HUSH_JOB(pi->pgrp = -1;) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3687 | pi->stopped_cmds = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3688 | command = &(pi->cmds[0]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3689 | argv_expanded = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3690 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3691 | if (pi->num_cmds != 1 |
| 3692 | || pi->followup == PIPE_BG |
| 3693 | || command->grp_type == GRP_SUBSHELL |
| 3694 | ) { |
| 3695 | goto must_fork; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3696 | } |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3697 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3698 | pi->alive_cmds = 1; |
| 3699 | |
| 3700 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 3701 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 3702 | |
| 3703 | if (command->group) { |
| 3704 | #if ENABLE_HUSH_FUNCTIONS |
| 3705 | if (command->grp_type == GRP_FUNCTION) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3706 | /* "executing" func () { list } */ |
| 3707 | struct function *funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3708 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3709 | funcp = new_function(command->argv[0]); |
| 3710 | /* funcp->name is already set to argv[0] */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3711 | funcp->body = command->group; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3712 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3713 | funcp->body_as_string = command->group_as_string; |
| 3714 | command->group_as_string = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3715 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3716 | command->group = NULL; |
| 3717 | command->argv[0] = NULL; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3718 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 3719 | funcp->parent_cmd = command; |
| 3720 | command->child_func = funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3721 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3722 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 3723 | debug_leave(); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3724 | return EXIT_SUCCESS; |
| 3725 | } |
| 3726 | #endif |
| 3727 | /* { list } */ |
| 3728 | debug_printf("non-subshell group\n"); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3729 | rcode = 1; /* exitcode if redir failed */ |
| 3730 | if (setup_redirects(command, squirrel) == 0) { |
| 3731 | debug_printf_exec(": run_list\n"); |
| 3732 | rcode = run_list(command->group) & 0xff; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3733 | } |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 3734 | restore_redirects(squirrel); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3735 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3736 | debug_leave(); |
| 3737 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3738 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3741 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 3742 | { |
| 3743 | const struct built_in_command *x; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3744 | #if ENABLE_HUSH_FUNCTIONS |
| 3745 | const struct function *funcp; |
| 3746 | #else |
| 3747 | enum { funcp = 0 }; |
| 3748 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3749 | char **new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3750 | struct variable *old_vars = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3751 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3752 | if (argv[command->assignment_cnt] == NULL) { |
| 3753 | /* Assignments, but no command */ |
| 3754 | /* Ensure redirects take effect. Try "a=t >file" */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3755 | rcode = setup_redirects(command, squirrel); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3756 | restore_redirects(squirrel); |
| 3757 | /* Set shell variables */ |
| 3758 | while (*argv) { |
| 3759 | p = expand_string_to_string(*argv); |
| 3760 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 3761 | *argv, p); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3762 | set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3763 | argv++; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3764 | } |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3765 | /* Do we need to flag set_local_var() errors? |
| 3766 | * "assignment to readonly var" and "putenv error" |
| 3767 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3768 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3769 | debug_leave(); |
| 3770 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3771 | return rcode; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3772 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3773 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3774 | /* Expand the rest into (possibly) many strings each */ |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3775 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3776 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3777 | x = find_builtin(argv_expanded[0]); |
| 3778 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3779 | funcp = NULL; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3780 | if (!x) |
| 3781 | funcp = find_function(argv_expanded[0]); |
| 3782 | #endif |
| 3783 | if (x || funcp) { |
| 3784 | if (!funcp) { |
| 3785 | if (x->function == builtin_exec && argv_expanded[1] == NULL) { |
| 3786 | debug_printf("exec with redirects only\n"); |
| 3787 | rcode = setup_redirects(command, NULL); |
| 3788 | goto clean_up_and_ret1; |
| 3789 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3790 | } |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3791 | /* setup_redirects acts on file descriptors, not FILEs. |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3792 | * This is perfect for work that comes after exec(). |
| 3793 | * Is it really safe for inline use? Experimentally, |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3794 | * things seem to work. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3795 | rcode = setup_redirects(command, squirrel); |
| 3796 | if (rcode == 0) { |
| 3797 | new_env = expand_assignments(argv, command->assignment_cnt); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3798 | old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3799 | if (!funcp) { |
| 3800 | debug_printf_exec(": builtin '%s' '%s'...\n", |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3801 | x->cmd, argv_expanded[1]); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3802 | rcode = x->function(argv_expanded) & 0xff; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3803 | fflush(NULL); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3804 | } |
| 3805 | #if ENABLE_HUSH_FUNCTIONS |
| 3806 | else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3807 | # if ENABLE_HUSH_LOCAL |
| 3808 | struct variable **sv; |
| 3809 | sv = G.shadowed_vars_pp; |
| 3810 | G.shadowed_vars_pp = &old_vars; |
| 3811 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3812 | debug_printf_exec(": function '%s' '%s'...\n", |
| 3813 | funcp->name, argv_expanded[1]); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3814 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3815 | # if ENABLE_HUSH_LOCAL |
| 3816 | G.shadowed_vars_pp = sv; |
| 3817 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3818 | } |
| 3819 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3820 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3821 | #if ENABLE_FEATURE_SH_STANDALONE |
| 3822 | clean_up_and_ret: |
| 3823 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3824 | restore_redirects(squirrel); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3825 | unset_vars(new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3826 | add_vars(old_vars); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3827 | clean_up_and_ret1: |
| 3828 | free(argv_expanded); |
| 3829 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3830 | debug_leave(); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3831 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 3832 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3833 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3834 | |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3835 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3836 | i = find_applet_by_name(argv_expanded[0]); |
| 3837 | if (i >= 0 && APPLET_IS_NOFORK(i)) { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3838 | rcode = setup_redirects(command, squirrel); |
| 3839 | if (rcode == 0) { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3840 | new_env = expand_assignments(argv, command->assignment_cnt); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3841 | old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3842 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3843 | argv_expanded[0], argv_expanded[1]); |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 3844 | rcode = run_nofork_applet(i, argv_expanded); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3845 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3846 | goto clean_up_and_ret; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3847 | } |
| 3848 | #endif |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3849 | /* It is neither builtin nor applet. We must fork. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3852 | must_fork: |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3853 | /* NB: argv_expanded may already be created, and that |
| 3854 | * might include `cmd` runs! Do not rerun it! We *must* |
| 3855 | * use argv_expanded if it's non-NULL */ |
| 3856 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3857 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3858 | pi->alive_cmds = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3859 | nextin = 0; |
| 3860 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3861 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3862 | struct fd_pair pipefds; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3863 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3864 | volatile nommu_save_t nommu_save; |
| 3865 | nommu_save.new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3866 | nommu_save.old_vars = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3867 | nommu_save.argv = NULL; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3868 | nommu_save.argv_from_re_execing = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3869 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3870 | command = &(pi->cmds[i]); |
| 3871 | if (command->argv) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3872 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 3873 | command->argv[0], command->argv[1]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3874 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3875 | debug_printf_exec(": pipe member with no argv\n"); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3876 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3877 | |
| 3878 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3879 | pipefds.rd = 0; |
| 3880 | pipefds.wr = 1; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3881 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3882 | xpiped_pair(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3883 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3884 | command->pid = BB_MMU ? fork() : vfork(); |
| 3885 | if (!command->pid) { /* child */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3886 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3887 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3888 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3889 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3890 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3891 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3892 | pid_t pgrp; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3893 | pgrp = pi->pgrp; |
| 3894 | if (pgrp < 0) /* true for 1st process only */ |
| 3895 | pgrp = getpid(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3896 | if (setpgid(0, pgrp) == 0 |
| 3897 | && pi->followup != PIPE_BG |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 3898 | && G_saved_tty_pgrp /* we have ctty */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3899 | ) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3900 | /* We do it in *every* child, not just first, |
| 3901 | * to avoid races */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3902 | tcsetpgrp(G_interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3903 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3904 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3905 | #endif |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3906 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 3907 | /* 1st cmd in backgrounded pipe |
| 3908 | * should have its stdin /dev/null'ed */ |
| 3909 | close(0); |
| 3910 | if (open(bb_dev_null, O_RDONLY)) |
| 3911 | xopen("/", O_RDONLY); |
| 3912 | } else { |
| 3913 | xmove_fd(nextin, 0); |
| 3914 | } |
| 3915 | xmove_fd(pipefds.wr, 1); |
| 3916 | if (pipefds.rd > 1) |
| 3917 | close(pipefds.rd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3918 | /* Like bash, explicit redirects override pipes, |
| 3919 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3920 | if (setup_redirects(command, NULL)) |
| 3921 | _exit(1); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3922 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3923 | /* Restore default handlers just prior to exec */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 3924 | /*signal(SIGCHLD, SIG_DFL); - so far we don't have any handlers */ |
| 3925 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3926 | /* Stores to nommu_save list of env vars putenv'ed |
| 3927 | * (NOMMU, on MMU we don't need that) */ |
| 3928 | /* cast away volatility... */ |
| 3929 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3930 | /* pseudo_exec() does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3931 | } |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 3932 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 3933 | /* parent or error */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3934 | #if ENABLE_HUSH_FAST |
| 3935 | G.count_SIGCHLD++; |
| 3936 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 3937 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3938 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3939 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3940 | /* Clean up after vforked child */ |
| 3941 | free(nommu_save.argv); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3942 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3943 | unset_vars(nommu_save.new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3944 | add_vars(nommu_save.old_vars); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3945 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3946 | free(argv_expanded); |
| 3947 | argv_expanded = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3948 | if (command->pid < 0) { /* [v]fork failed */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3949 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 3950 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3951 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3952 | pi->alive_cmds++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3953 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3954 | /* Second and next children need to know pid of first one */ |
| 3955 | if (pi->pgrp < 0) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3956 | pi->pgrp = command->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3957 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3958 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3959 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3960 | if (i) |
| 3961 | close(nextin); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3962 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3963 | close(pipefds.wr); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3964 | /* Pass read (output) pipe end to next iteration */ |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3965 | nextin = pipefds.rd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3966 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3967 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3968 | if (!pi->alive_cmds) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3969 | debug_leave(); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3970 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 3971 | return 1; |
| 3972 | } |
| 3973 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3974 | debug_leave(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3975 | 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] | 3976 | return -1; |
| 3977 | } |
| 3978 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 3979 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3980 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3981 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3982 | static const char *const PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3983 | [PIPE_SEQ] = "SEQ", |
| 3984 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3985 | [PIPE_OR ] = "OR" , |
| 3986 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3987 | }; |
| 3988 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3989 | [RES_NONE ] = "NONE" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3990 | #if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3991 | [RES_IF ] = "IF" , |
| 3992 | [RES_THEN ] = "THEN" , |
| 3993 | [RES_ELIF ] = "ELIF" , |
| 3994 | [RES_ELSE ] = "ELSE" , |
| 3995 | [RES_FI ] = "FI" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3996 | #endif |
| 3997 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3998 | [RES_FOR ] = "FOR" , |
| 3999 | [RES_WHILE] = "WHILE", |
| 4000 | [RES_UNTIL] = "UNTIL", |
| 4001 | [RES_DO ] = "DO" , |
| 4002 | [RES_DONE ] = "DONE" , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 4003 | #endif |
| 4004 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4005 | [RES_IN ] = "IN" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4006 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4007 | #if ENABLE_HUSH_CASE |
| 4008 | [RES_CASE ] = "CASE" , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4009 | [RES_CASE_IN ] = "CASE_IN" , |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4010 | [RES_MATCH] = "MATCH", |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4011 | [RES_CASE_BODY] = "CASE_BODY", |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4012 | [RES_ESAC ] = "ESAC" , |
| 4013 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4014 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4015 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4016 | }; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4017 | static const char *const GRPTYPE[] = { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4018 | "{}", |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 4019 | "()", |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4020 | #if ENABLE_HUSH_FUNCTIONS |
| 4021 | "func()", |
| 4022 | #endif |
| 4023 | }; |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4024 | |
| 4025 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4026 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4027 | pin = 0; |
| 4028 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4029 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 4030 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4031 | prn = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4032 | while (prn < pi->num_cmds) { |
| 4033 | struct command *command = &pi->cmds[prn]; |
| 4034 | char **argv = command->argv; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4035 | |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4036 | fprintf(stderr, "%*s cmd %d assignment_cnt:%d", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4037 | lvl*2, "", prn, |
| 4038 | command->assignment_cnt); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4039 | if (command->group) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4040 | fprintf(stderr, " group %s: (argv=%p)\n", |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4041 | GRPTYPE[command->grp_type], |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4042 | argv); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4043 | debug_print_tree(command->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4044 | prn++; |
| 4045 | continue; |
| 4046 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4047 | if (argv) while (*argv) { |
| 4048 | fprintf(stderr, " '%s'", *argv); |
| 4049 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 4050 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4051 | fprintf(stderr, "\n"); |
| 4052 | prn++; |
| 4053 | } |
| 4054 | pi = pi->next; |
| 4055 | pin++; |
| 4056 | } |
| 4057 | } |
| 4058 | #endif |
| 4059 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 4060 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 4061 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4062 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4063 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4064 | #if ENABLE_HUSH_CASE |
| 4065 | char *case_word = NULL; |
| 4066 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4067 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4068 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4069 | char **for_lcur = NULL; |
| 4070 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4071 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4072 | smallint last_followup; |
| 4073 | smalluint rcode; |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 4074 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4075 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4076 | #else |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4077 | enum { cond_code = 0 }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4078 | #endif |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4079 | #if HAS_KEYWORDS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4080 | smallint rword; /* enum reserved_style */ |
| 4081 | smallint last_rword; /* ditto */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4082 | #endif |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4083 | |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4084 | 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] | 4085 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4086 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4087 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4088 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4089 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 4090 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4091 | continue; |
| 4092 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4093 | if (cpipe->next == NULL) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4094 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4095 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4096 | 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] | 4097 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4098 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4099 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4100 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4101 | continue; |
| 4102 | /* 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] | 4103 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 4104 | || 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] | 4105 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4106 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4107 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4108 | 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] | 4109 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4110 | } |
| 4111 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4112 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4113 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4114 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 4115 | * in order to return, no direct "return" statements please. |
| 4116 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4117 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4118 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4119 | G.run_list_level++; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4120 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4121 | |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4122 | #if HAS_KEYWORDS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4123 | rword = RES_NONE; |
| 4124 | last_rword = RES_XXXX; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4125 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4126 | last_followup = PIPE_SEQ; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4127 | rcode = G.last_exitcode; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4128 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4129 | /* Go through list of pipes, (maybe) executing them. */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4130 | 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] | 4131 | if (G.flag_SIGINT) |
| 4132 | break; |
| 4133 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4134 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4135 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 4136 | rword, cond_code, last_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4137 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 4138 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4139 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 4140 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4141 | /* start of a loop: remember where loop starts */ |
| 4142 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4143 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4144 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4145 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4146 | /* Still in the same "if...", "then..." or "do..." branch? */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4147 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4148 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 4149 | || (rcode != 0 && last_followup == PIPE_AND) |
| 4150 | ) { |
| 4151 | /* It is "<true> || CMD" or "<false> && CMD" |
| 4152 | * and we should not execute CMD */ |
| 4153 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 4154 | last_followup = pi->followup; |
| 4155 | continue; |
| 4156 | } |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4157 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4158 | last_followup = pi->followup; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4159 | IF_HAS_KEYWORDS(last_rword = rword;) |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4160 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4161 | if (cond_code) { |
| 4162 | if (rword == RES_THEN) { |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4163 | /* if false; then ... fi has exitcode 0! */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4164 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4165 | /* "if <false> THEN cmd": skip cmd */ |
| 4166 | continue; |
| 4167 | } |
| 4168 | } else { |
| 4169 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 4170 | /* "if <true> then ... ELSE/ELIF cmd": |
| 4171 | * skip cmd and all following ones */ |
| 4172 | break; |
| 4173 | } |
| 4174 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4175 | #endif |
| 4176 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4177 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4178 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 4179 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4180 | |
| 4181 | static const char encoded_dollar_at[] ALIGN1 = { |
| 4182 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 4183 | }; /* encoded representation of "$@" */ |
| 4184 | static const char *const encoded_dollar_at_argv[] = { |
| 4185 | encoded_dollar_at, NULL |
| 4186 | }; /* argv list with one element: "$@" */ |
| 4187 | char **vals; |
| 4188 | |
| 4189 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4190 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4191 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4192 | if (!pi->next->cmds[0].argv) { |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4193 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4194 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4195 | break; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4196 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4197 | vals = pi->next->cmds[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4198 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4199 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4200 | debug_print_strings("for_list made from", vals); |
| 4201 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4202 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4203 | debug_print_strings("for_list", for_list); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4204 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4205 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 4206 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4207 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4208 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4209 | for_lcur = NULL; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4210 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4211 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4212 | /* Insert next value from for_lcur */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4213 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4214 | 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] | 4215 | continue; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4216 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4217 | if (rword == RES_IN) { |
| 4218 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 4219 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4220 | if (rword == RES_DONE) { |
| 4221 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4222 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4223 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4224 | #if ENABLE_HUSH_CASE |
| 4225 | if (rword == RES_CASE) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4226 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4227 | continue; |
| 4228 | } |
| 4229 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4230 | char **argv; |
| 4231 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4232 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 4233 | break; |
| 4234 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4235 | argv = pi->cmds->argv; |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4236 | while (*argv) { |
| 4237 | char *pattern = expand_string_to_string(*argv); |
| 4238 | /* TODO: which FNM_xxx flags to use? */ |
| 4239 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 4240 | free(pattern); |
| 4241 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 4242 | free(case_word); /* make future "word)" stop */ |
| 4243 | case_word = NULL; |
| 4244 | break; |
| 4245 | } |
| 4246 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4247 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4248 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4249 | } |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4250 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4251 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4252 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4253 | } |
| 4254 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4255 | /* Just pressing <enter> in shell should check for jobs. |
| 4256 | * OTOH, in non-interactive shell this is useless |
| 4257 | * and only leads to extra job checks */ |
| 4258 | if (pi->num_cmds == 0) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4259 | if (G_interactive_fd) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4260 | goto check_jobs_and_continue; |
| 4261 | continue; |
| 4262 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4263 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4264 | /* After analyzing all keywords and conditions, we decided |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4265 | * to execute this pipe. NB: have to do checkjobs(NULL) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4266 | * after run_pipe to collect any background children, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4267 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4268 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4269 | { |
| 4270 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4271 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4272 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4273 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4274 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 4275 | if (r != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4276 | /* We ran a builtin, function, or group. |
| 4277 | * rcode is already known |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4278 | * and we don't need to wait for anything. */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4279 | G.last_exitcode = rcode; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4280 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4281 | check_and_run_traps(0); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4282 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4283 | /* Was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4284 | if (G.flag_break_continue) { |
| 4285 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4286 | /* We might fall into outer *loop*, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4287 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4288 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4289 | G.depth_break_continue--; |
| 4290 | if (G.depth_break_continue == 0) |
| 4291 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4292 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 4293 | } /* 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] | 4294 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4295 | goto check_jobs_and_break; |
| 4296 | /* "continue": simulate end of loop */ |
| 4297 | rword = RES_DONE; |
| 4298 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4299 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4300 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4301 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 673e945 | 2009-05-27 14:39:35 +0200 | [diff] [blame] | 4302 | if (G.flag_return_in_progress == 1) { |
| 4303 | /* same as "goto check_jobs_and_break" */ |
| 4304 | checkjobs(NULL); |
| 4305 | break; |
| 4306 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4307 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4308 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4309 | /* What does bash do with attempts to background builtins? */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4310 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4311 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 4312 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4313 | check_and_run_traps(0); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4314 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4315 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4316 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4317 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4318 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4319 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4320 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4321 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4322 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4323 | /* Waits for completion, then fg's main shell */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4324 | rcode = checkjobs_and_fg_shell(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4325 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4326 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4327 | } else |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4328 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4329 | { /* This one just waits for completion */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4330 | rcode = checkjobs(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4331 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4332 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4333 | } |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4334 | G.last_exitcode = rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4335 | } |
| 4336 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4337 | |
| 4338 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4339 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4340 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4341 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4342 | #endif |
| 4343 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4344 | /* Beware of "while false; true; do ..."! */ |
| 4345 | if (pi->next && pi->next->res_word == RES_DO) { |
| 4346 | if (rword == RES_WHILE) { |
| 4347 | if (rcode) { |
| 4348 | /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4349 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4350 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
| 4351 | goto check_jobs_and_break; |
| 4352 | } |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 4353 | } |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4354 | if (rword == RES_UNTIL) { |
| 4355 | if (!rcode) { |
| 4356 | debug_printf_exec(": until expr is true: breaking\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4357 | check_jobs_and_break: |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4358 | checkjobs(NULL); |
| 4359 | break; |
| 4360 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4361 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4362 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4363 | #endif |
Mike Frysinger | 8ec1c9d | 2009-03-29 00:45:26 +0000 | [diff] [blame] | 4364 | |
| 4365 | check_jobs_and_continue: |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 4366 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4367 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4368 | |
| 4369 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4370 | G.run_list_level--; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4371 | #endif |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4372 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4373 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4374 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4375 | free(for_list); |
| 4376 | #endif |
| 4377 | #if ENABLE_HUSH_CASE |
| 4378 | free(case_word); |
| 4379 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4380 | debug_leave(); |
| 4381 | 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] | 4382 | return rcode; |
| 4383 | } |
| 4384 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4385 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4386 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4387 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4388 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4389 | debug_printf_exec("run_and_free_list entered\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4390 | if (!G.fake_mode) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4391 | 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] | 4392 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4393 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4394 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4395 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4396 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4397 | free_pipe_list(pi); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 4398 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4399 | return rcode; |
| 4400 | } |
| 4401 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4402 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 4403 | static struct pipe *new_pipe(void) |
| 4404 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4405 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4406 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4407 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4408 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4409 | return pi; |
| 4410 | } |
| 4411 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4412 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 4413 | * if ctx->command is NULL. |
| 4414 | * No errors possible here. |
| 4415 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4416 | static int done_command(struct parse_context *ctx) |
| 4417 | { |
| 4418 | /* The command is really already in the pipe structure, so |
| 4419 | * advance the pipe counter and make a new, null command. */ |
| 4420 | struct pipe *pi = ctx->pipe; |
| 4421 | struct command *command = ctx->command; |
| 4422 | |
| 4423 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4424 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4425 | 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] | 4426 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4427 | } |
| 4428 | pi->num_cmds++; |
| 4429 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4430 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4431 | } else { |
| 4432 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 4433 | } |
| 4434 | |
| 4435 | /* Only real trickiness here is that the uncommitted |
| 4436 | * command structure is not counted in pi->num_cmds. */ |
| 4437 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4438 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 4439 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4440 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4441 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 4442 | } |
| 4443 | |
| 4444 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 4445 | { |
| 4446 | int not_null; |
| 4447 | |
| 4448 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 4449 | /* Close previous command */ |
| 4450 | not_null = done_command(ctx); |
| 4451 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4452 | #if HAS_KEYWORDS |
| 4453 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 4454 | ctx->ctx_inverted = 0; |
| 4455 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 4456 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4457 | |
| 4458 | /* Without this check, even just <enter> on command line generates |
| 4459 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4460 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4461 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4462 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4463 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4464 | #endif |
| 4465 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4466 | || ctx->ctx_res_w == RES_DONE |
| 4467 | || ctx->ctx_res_w == RES_FOR |
| 4468 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4469 | #endif |
| 4470 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4471 | || ctx->ctx_res_w == RES_ESAC |
| 4472 | #endif |
| 4473 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4474 | struct pipe *new_p; |
| 4475 | debug_printf_parse("done_pipe: adding new pipe: " |
| 4476 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 4477 | not_null, ctx->ctx_res_w); |
| 4478 | new_p = new_pipe(); |
| 4479 | ctx->pipe->next = new_p; |
| 4480 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4481 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4482 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4483 | * This is used to control execution. |
| 4484 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 4485 | * cases where variable or value happens to match a keyword): |
| 4486 | */ |
| 4487 | #if ENABLE_HUSH_LOOPS |
| 4488 | if (ctx->ctx_res_w == RES_FOR |
| 4489 | || ctx->ctx_res_w == RES_IN) |
| 4490 | ctx->ctx_res_w = RES_NONE; |
| 4491 | #endif |
| 4492 | #if ENABLE_HUSH_CASE |
| 4493 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4494 | ctx->ctx_res_w = RES_CASE_BODY; |
| 4495 | if (ctx->ctx_res_w == RES_CASE) |
| 4496 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4497 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4498 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4499 | /* Create the memory for command, roughly: |
| 4500 | * ctx->pipe->cmds = new struct command; |
| 4501 | * ctx->command = &ctx->pipe->cmds[0]; |
| 4502 | */ |
| 4503 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4504 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4505 | } |
| 4506 | debug_printf_parse("done_pipe return\n"); |
| 4507 | } |
| 4508 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4509 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4510 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4511 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4512 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4513 | /* Create the memory for command, roughly: |
| 4514 | * ctx->pipe->cmds = new struct command; |
| 4515 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4516 | */ |
| 4517 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4520 | /* If a reserved word is found and processed, parse context is modified |
| 4521 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4522 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4523 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4524 | struct reserved_combo { |
| 4525 | char literal[6]; |
| 4526 | unsigned char res; |
| 4527 | unsigned char assignment_flag; |
| 4528 | int flag; |
| 4529 | }; |
| 4530 | enum { |
| 4531 | FLAG_END = (1 << RES_NONE ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4532 | #if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4533 | FLAG_IF = (1 << RES_IF ), |
| 4534 | FLAG_THEN = (1 << RES_THEN ), |
| 4535 | FLAG_ELIF = (1 << RES_ELIF ), |
| 4536 | FLAG_ELSE = (1 << RES_ELSE ), |
| 4537 | FLAG_FI = (1 << RES_FI ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4538 | #endif |
| 4539 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4540 | FLAG_FOR = (1 << RES_FOR ), |
| 4541 | FLAG_WHILE = (1 << RES_WHILE), |
| 4542 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 4543 | FLAG_DO = (1 << RES_DO ), |
| 4544 | FLAG_DONE = (1 << RES_DONE ), |
| 4545 | FLAG_IN = (1 << RES_IN ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4546 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4547 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4548 | FLAG_MATCH = (1 << RES_MATCH), |
| 4549 | FLAG_ESAC = (1 << RES_ESAC ), |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4550 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4551 | FLAG_START = (1 << RES_XXXX ), |
| 4552 | }; |
| 4553 | |
| 4554 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 4555 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4556 | /* Mostly a list of accepted follow-up reserved words. |
| 4557 | * FLAG_END means we are done with the sequence, and are ready |
| 4558 | * to turn the compound list into a command. |
| 4559 | * FLAG_START means the word must start a new compound list. |
| 4560 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4561 | static const struct reserved_combo reserved_list[] = { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4562 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4563 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 4564 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 4565 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 4566 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 4567 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 4568 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4569 | #endif |
| 4570 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4571 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 4572 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 4573 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 4574 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 4575 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 4576 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4577 | #endif |
| 4578 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4579 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 4580 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4581 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4582 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4583 | const struct reserved_combo *r; |
| 4584 | |
| 4585 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 4586 | if (strcmp(word->data, r->literal) == 0) |
| 4587 | return r; |
| 4588 | } |
| 4589 | return NULL; |
| 4590 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4591 | /* Return 0: not a keyword, 1: keyword |
| 4592 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4593 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 4594 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4595 | #if ENABLE_HUSH_CASE |
| 4596 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4597 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4598 | }; |
| 4599 | #endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4600 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4601 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4602 | if (word->o_quoted) |
| 4603 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4604 | r = match_reserved_word(word); |
| 4605 | if (!r) |
| 4606 | return 0; |
| 4607 | |
| 4608 | 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] | 4609 | #if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4610 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 4611 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4612 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4613 | } else |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4614 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4615 | if (r->flag == 0) { /* '!' */ |
| 4616 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4617 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4618 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4619 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4620 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4621 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4622 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4623 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4624 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4625 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4626 | old = xmalloc(sizeof(*old)); |
| 4627 | debug_printf_parse("push stack %p\n", old); |
| 4628 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4629 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4630 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4631 | } 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] | 4632 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4633 | ctx->ctx_res_w = RES_SNTX; |
| 4634 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4635 | } else { |
| 4636 | /* "{...} fi" is ok. "{...} if" is not |
| 4637 | * Example: |
| 4638 | * if { echo foo; } then { echo bar; } fi */ |
| 4639 | if (ctx->command->group) |
| 4640 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4641 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4642 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4643 | ctx->ctx_res_w = r->res; |
| 4644 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4645 | word->o_assignment = r->assignment_flag; |
| 4646 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4647 | if (ctx->old_flag & FLAG_END) { |
| 4648 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4649 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4650 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4651 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4652 | old = ctx->stack; |
| 4653 | old->command->group = ctx->list_head; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4654 | old->command->grp_type = GRP_NORMAL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4655 | #if !BB_MMU |
| 4656 | o_addstr(&old->as_string, ctx->as_string.data); |
| 4657 | o_free_unsafe(&ctx->as_string); |
| 4658 | old->command->group_as_string = xstrdup(old->as_string.data); |
| 4659 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 4660 | old->command->group_as_string); |
| 4661 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4662 | *ctx = *old; /* physical copy */ |
| 4663 | free(old); |
| 4664 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4665 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4666 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4667 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4668 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4669 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4670 | * Normal return is 0. Syntax errors return 1. |
| 4671 | * Note: on return, word is reset, but not o_free'd! |
| 4672 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4673 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4674 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4675 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4676 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4677 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4678 | if (word->length == 0 && word->o_quoted == 0) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4679 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 4680 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4681 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4682 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4683 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4684 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 4685 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4686 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4687 | * "2.7 Redirection |
| 4688 | * ...the word that follows the redirection operator |
| 4689 | * shall be subjected to tilde expansion, parameter expansion, |
| 4690 | * command substitution, arithmetic expansion, and quote |
| 4691 | * removal. Pathname expansion shall not be performed |
| 4692 | * on the word by a non-interactive shell; an interactive |
| 4693 | * shell may perform it, but shall do so only when |
| 4694 | * the expansion would result in one word." |
| 4695 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4696 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4697 | /* Cater for >\file case: |
| 4698 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 4699 | * Same with heredocs: |
| 4700 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 4701 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4702 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 4703 | unbackslash(ctx->pending_redirect->rd_filename); |
| 4704 | /* Is it <<"HEREDOC"? */ |
| 4705 | if (word->o_quoted) { |
| 4706 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 4707 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4708 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4709 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4710 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4711 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4712 | /* If this word wasn't an assignment, next ones definitely |
| 4713 | * can't be assignments. Even if they look like ones. */ |
| 4714 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 4715 | && word->o_assignment != WORD_IS_KEYWORD |
| 4716 | ) { |
| 4717 | word->o_assignment = NOT_ASSIGNMENT; |
| 4718 | } else { |
| 4719 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
| 4720 | command->assignment_cnt++; |
| 4721 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 4722 | } |
| 4723 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4724 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4725 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 4726 | if (ctx->ctx_dsemicolon |
| 4727 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 4728 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 4729 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4730 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 4731 | ctx->ctx_dsemicolon = 0; |
| 4732 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4733 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4734 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4735 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4736 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 4737 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4738 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4739 | # if ENABLE_HUSH_CASE |
| 4740 | && ctx->ctx_res_w != RES_CASE |
| 4741 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4742 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4743 | debug_printf_parse("checking '%s' for reserved-ness\n", word->data); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4744 | if (reserved_word(word, ctx)) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4745 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4746 | debug_printf_parse("done_word return %d\n", |
| 4747 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4748 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4749 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4750 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4751 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4752 | if (command->group) { |
| 4753 | /* "{ echo foo; } echo bar" - bad */ |
| 4754 | syntax_error_at(word->data); |
| 4755 | debug_printf_parse("done_word return 1: syntax error, " |
| 4756 | "groups and arglists don't mix\n"); |
| 4757 | return 1; |
| 4758 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4759 | 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] | 4760 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 4761 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4762 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4763 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4764 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 4765 | char *p = word->data; |
| 4766 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 4767 | && (p[1] & 0x7f) == '@' |
| 4768 | && p[2] == SPECIAL_VAR_SYMBOL |
| 4769 | ) { |
| 4770 | p += 3; |
| 4771 | } |
| 4772 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4773 | /* saw no "$@", or not only "$@" but some |
| 4774 | * real text is there too */ |
| 4775 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 4776 | * e.g. "", $empty"" etc to not disappear */ |
| 4777 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 4778 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 4779 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 4780 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4781 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4782 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4783 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4784 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4785 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4786 | if (ctx->ctx_res_w == RES_FOR) { |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4787 | if (word->o_quoted |
| 4788 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 4789 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4790 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4791 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4792 | return 1; |
| 4793 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4794 | /* Force FOR to have just one word (variable name) */ |
| 4795 | /* NB: basically, this makes hush see "for v in ..." |
| 4796 | * syntax as if it is "for v; in ...". FOR and IN become |
| 4797 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4798 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4799 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4800 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4801 | #if ENABLE_HUSH_CASE |
| 4802 | /* Force CASE to have just one word */ |
| 4803 | if (ctx->ctx_res_w == RES_CASE) { |
| 4804 | done_pipe(ctx, PIPE_SEQ); |
| 4805 | } |
| 4806 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4807 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4808 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4809 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4810 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4811 | return 0; |
| 4812 | } |
| 4813 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4814 | |
| 4815 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 4816 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4817 | * Return: |
| 4818 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 4819 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 4820 | * REDIRFD_TO_FILE if no & was seen, |
| 4821 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4822 | */ |
| 4823 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4824 | #define parse_redir_right_fd(as_string, input) \ |
| 4825 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4826 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4827 | 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] | 4828 | { |
| 4829 | int ch, d, ok; |
| 4830 | |
| 4831 | ch = i_peek(input); |
| 4832 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4833 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4834 | |
| 4835 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4836 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4837 | ch = i_peek(input); |
| 4838 | if (ch == '-') { |
| 4839 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4840 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4841 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4842 | } |
| 4843 | d = 0; |
| 4844 | ok = 0; |
| 4845 | while (ch != EOF && isdigit(ch)) { |
| 4846 | d = d*10 + (ch-'0'); |
| 4847 | ok = 1; |
| 4848 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4849 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4850 | ch = i_peek(input); |
| 4851 | } |
| 4852 | if (ok) return d; |
| 4853 | |
| 4854 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 4855 | |
| 4856 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4857 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4858 | } |
| 4859 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4860 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4861 | */ |
| 4862 | static int parse_redirect(struct parse_context *ctx, |
| 4863 | int fd, |
| 4864 | redir_type style, |
| 4865 | struct in_str *input) |
| 4866 | { |
| 4867 | struct command *command = ctx->command; |
| 4868 | struct redir_struct *redir; |
| 4869 | struct redir_struct **redirp; |
| 4870 | int dup_num; |
| 4871 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4872 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4873 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4874 | /* Check for a '>&1' type redirect */ |
| 4875 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 4876 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 4877 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4878 | } else { |
| 4879 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4880 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4881 | if (dup_num) { /* <<-... */ |
| 4882 | ch = i_getch(input); |
| 4883 | nommu_addchr(&ctx->as_string, ch); |
| 4884 | ch = i_peek(input); |
| 4885 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4886 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4887 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4888 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4889 | int ch = i_peek(input); |
| 4890 | if (ch == '|') { |
| 4891 | /* >|FILE redirect ("clobbering" >). |
| 4892 | * Since we do not support "set -o noclobber" yet, |
| 4893 | * >| and > are the same for now. Just eat |. |
| 4894 | */ |
| 4895 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4896 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4897 | } |
| 4898 | } |
| 4899 | |
| 4900 | /* Create a new redir_struct and append it to the linked list */ |
| 4901 | redirp = &command->redirects; |
| 4902 | while ((redir = *redirp) != NULL) { |
| 4903 | redirp = &(redir->next); |
| 4904 | } |
| 4905 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 4906 | /* redir->next = NULL; */ |
| 4907 | /* redir->rd_filename = NULL; */ |
| 4908 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4909 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4910 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4911 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 4912 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4913 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4914 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4915 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4916 | /* Erik had a check here that the file descriptor in question |
| 4917 | * is legit; I postpone that to "run time" |
| 4918 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4919 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 4920 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4921 | } else { |
| 4922 | /* Set ctx->pending_redirect, so we know what to do at the |
| 4923 | * end of the next parsed word. */ |
| 4924 | ctx->pending_redirect = redir; |
| 4925 | } |
| 4926 | return 0; |
| 4927 | } |
| 4928 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4929 | /* If a redirect is immediately preceded by a number, that number is |
| 4930 | * supposed to tell which file descriptor to redirect. This routine |
| 4931 | * looks for such preceding numbers. In an ideal world this routine |
| 4932 | * needs to handle all the following classes of redirects... |
| 4933 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 4934 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 4935 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 4936 | * 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] | 4937 | * |
| 4938 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4939 | * "2.7 Redirection |
| 4940 | * ... If n is quoted, the number shall not be recognized as part of |
| 4941 | * the redirection expression. For example: |
| 4942 | * echo \2>a |
| 4943 | * writes the character 2 into file a" |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4944 | * We are getting it right by setting ->o_quoted on any \<char> |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4945 | * |
| 4946 | * A -1 return means no valid number was found, |
| 4947 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4948 | */ |
| 4949 | static int redirect_opt_num(o_string *o) |
| 4950 | { |
| 4951 | int num; |
| 4952 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4953 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4954 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4955 | num = bb_strtou(o->data, NULL, 10); |
| 4956 | if (errno || num < 0) |
| 4957 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4958 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4959 | return num; |
| 4960 | } |
| 4961 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4962 | #if BB_MMU |
| 4963 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 4964 | fetch_till_str(input, word, skip_tabs) |
| 4965 | #endif |
| 4966 | static char *fetch_till_str(o_string *as_string, |
| 4967 | struct in_str *input, |
| 4968 | const char *word, |
| 4969 | int skip_tabs) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4970 | { |
| 4971 | o_string heredoc = NULL_O_STRING; |
| 4972 | int past_EOL = 0; |
| 4973 | int ch; |
| 4974 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4975 | goto jump_in; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4976 | while (1) { |
| 4977 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4978 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4979 | if (ch == '\n') { |
| 4980 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 4981 | heredoc.data[past_EOL] = '\0'; |
| 4982 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 4983 | return heredoc.data; |
| 4984 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4985 | do { |
| 4986 | o_addchr(&heredoc, ch); |
| 4987 | past_EOL = heredoc.length; |
| 4988 | jump_in: |
| 4989 | do { |
| 4990 | ch = i_getch(input); |
| 4991 | nommu_addchr(as_string, ch); |
| 4992 | } while (skip_tabs && ch == '\t'); |
| 4993 | } while (ch == '\n'); |
| 4994 | } |
| 4995 | if (ch == EOF) { |
| 4996 | o_free_unsafe(&heredoc); |
| 4997 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4998 | } |
| 4999 | o_addchr(&heredoc, ch); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5000 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5001 | } |
| 5002 | } |
| 5003 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5004 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 5005 | * and load them all. There should be exactly heredoc_cnt of them. |
| 5006 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5007 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 5008 | { |
| 5009 | struct pipe *pi = ctx->list_head; |
| 5010 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5011 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5012 | int i; |
| 5013 | struct command *cmd = pi->cmds; |
| 5014 | |
| 5015 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 5016 | pi->num_cmds, |
| 5017 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 5018 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5019 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5020 | |
| 5021 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 5022 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5023 | while (redir) { |
| 5024 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5025 | char *p; |
| 5026 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5027 | redir->rd_type = REDIRECT_HEREDOC2; |
| 5028 | /* redir->dup is (ab)used to indicate <<- */ |
| 5029 | p = fetch_till_str(&ctx->as_string, input, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5030 | redir->rd_filename, redir->rd_dup & HEREDOC_SKIPTABS); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5031 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5032 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5033 | return 1; |
| 5034 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5035 | free(redir->rd_filename); |
| 5036 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5037 | heredoc_cnt--; |
| 5038 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5039 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5040 | } |
| 5041 | cmd++; |
| 5042 | } |
| 5043 | pi = pi->next; |
| 5044 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5045 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5046 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5047 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5048 | bb_error_msg_and_die("heredoc BUG 2"); |
| 5049 | #endif |
| 5050 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5051 | } |
| 5052 | |
| 5053 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5054 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5055 | static FILE *generate_stream_from_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5056 | { |
| 5057 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5058 | int pid, channel[2]; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5059 | #if !BB_MMU |
| 5060 | char **to_free; |
| 5061 | #endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 5062 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 5063 | xpipe(channel); |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 5064 | pid = BB_MMU ? fork() : vfork(); |
| 5065 | if (pid < 0) |
| 5066 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5067 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5068 | if (pid == 0) { /* child */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5069 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 5070 | /* Process substitution is not considered to be usual |
| 5071 | * 'command execution'. |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 5072 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 5073 | */ |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 5074 | bb_signals(0 |
| 5075 | + (1 << SIGTSTP) |
| 5076 | + (1 << SIGTTIN) |
| 5077 | + (1 << SIGTTOU) |
| 5078 | , SIG_IGN); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5079 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 5080 | xmove_fd(channel[1], 1); |
| 5081 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 5082 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5083 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5084 | reset_traps_to_defaults(); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5085 | parse_and_run_string(s); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5086 | _exit(G.last_exitcode); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5087 | #else |
| 5088 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5089 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 5090 | * huge=`cat BIG` # was blocking here forever |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5091 | * echo OK |
| 5092 | */ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5093 | re_execute_shell(&to_free, |
| 5094 | s, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5095 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 5096 | G.global_argv + 1, |
| 5097 | NULL); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5098 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5099 | } |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5100 | |
| 5101 | /* parent */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 5102 | #if ENABLE_HUSH_FAST |
| 5103 | G.count_SIGCHLD++; |
| 5104 | //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); |
| 5105 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5106 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5107 | #if !BB_MMU |
| 5108 | free(to_free); |
| 5109 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5110 | close(channel[1]); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 5111 | pf = fdopen(channel[0], "r"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5112 | return pf; |
| 5113 | } |
| 5114 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 5115 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5116 | static int process_command_subs(o_string *dest, const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5117 | { |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5118 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5119 | struct in_str pipe_str; |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5120 | int ch, eol_cnt; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5121 | |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5122 | pf = generate_stream_from_string(s); |
| 5123 | if (pf == NULL) |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5124 | return 1; |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5125 | close_on_exec_on(fileno(pf)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5126 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5127 | /* Now send results of command back into original context */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5128 | setup_file_in_str(&pipe_str, pf); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5129 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5130 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5131 | if (ch == '\n') { |
| 5132 | eol_cnt++; |
| 5133 | continue; |
| 5134 | } |
| 5135 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5136 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5137 | eol_cnt--; |
| 5138 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 5139 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5140 | } |
| 5141 | |
| 5142 | debug_printf("done reading from pipe, pclose()ing\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5143 | /* Note: we got EOF, and we just close the read end of the pipe. |
| 5144 | * 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] | 5145 | * Try these: |
| 5146 | * echo `echo Hi; exec 1>&-; sleep 2` - bash waits 2 sec |
| 5147 | * `false`; echo $? - bash outputs "1" |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5148 | */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5149 | fclose(pf); |
| 5150 | debug_printf("closed FILE from child. return 0\n"); |
| 5151 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5152 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5153 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5154 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5155 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5156 | struct in_str *input, int ch) |
| 5157 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5158 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5159 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5160 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5161 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5162 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5163 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5164 | |
| 5165 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5166 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5167 | if (ch == '(' && !dest->o_quoted) { |
| 5168 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5169 | if (done_word(dest, ctx)) |
| 5170 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5171 | if (!command->argv) |
| 5172 | goto skip; /* (... */ |
| 5173 | if (command->argv[1]) { /* word word ... (... */ |
| 5174 | syntax_error_unexpected_ch('('); |
| 5175 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5176 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5177 | /* it is "word(..." or "word (..." */ |
| 5178 | do |
| 5179 | ch = i_getch(input); |
| 5180 | while (ch == ' ' || ch == '\t'); |
| 5181 | if (ch != ')') { |
| 5182 | syntax_error_unexpected_ch(ch); |
| 5183 | return 1; |
| 5184 | } |
| 5185 | nommu_addchr(&ctx->as_string, ch); |
| 5186 | do |
| 5187 | ch = i_getch(input); |
| 5188 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 5189 | if (ch != '{') { |
| 5190 | syntax_error_unexpected_ch(ch); |
| 5191 | return 1; |
| 5192 | } |
| 5193 | nommu_addchr(&ctx->as_string, ch); |
| 5194 | command->grp_type = GRP_FUNCTION; |
| 5195 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5196 | } |
| 5197 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5198 | if (command->argv /* word [word]{... */ |
| 5199 | || dest->length /* word{... */ |
| 5200 | || dest->o_quoted /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5201 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5202 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5203 | debug_printf_parse("parse_group return 1: " |
| 5204 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5205 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5206 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5207 | |
| 5208 | #if ENABLE_HUSH_FUNCTIONS |
| 5209 | skip: |
| 5210 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5211 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 5212 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5213 | endch = ')'; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5214 | command->grp_type = GRP_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5215 | } else { |
| 5216 | /* bash does not allow "{echo...", requires whitespace */ |
| 5217 | ch = i_getch(input); |
| 5218 | if (ch != ' ' && ch != '\t' && ch != '\n') { |
| 5219 | syntax_error_unexpected_ch(ch); |
| 5220 | return 1; |
| 5221 | } |
| 5222 | nommu_addchr(&ctx->as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5223 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5224 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5225 | { |
| 5226 | #if !BB_MMU |
| 5227 | char *as_string = NULL; |
| 5228 | #endif |
| 5229 | pipe_list = parse_stream(&as_string, input, endch); |
| 5230 | #if !BB_MMU |
| 5231 | if (as_string) |
| 5232 | o_addstr(&ctx->as_string, as_string); |
| 5233 | #endif |
| 5234 | /* empty ()/{} or parse error? */ |
| 5235 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5236 | /* parse_stream already emitted error msg */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5237 | #if !BB_MMU |
| 5238 | free(as_string); |
| 5239 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5240 | debug_printf_parse("parse_group return 1: " |
| 5241 | "parse_stream returned %p\n", pipe_list); |
| 5242 | return 1; |
| 5243 | } |
| 5244 | command->group = pipe_list; |
| 5245 | #if !BB_MMU |
| 5246 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 5247 | command->group_as_string = as_string; |
| 5248 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 5249 | command->group_as_string); |
| 5250 | #endif |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5251 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5252 | debug_printf_parse("parse_group return 0\n"); |
| 5253 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5254 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5257 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5258 | /* Subroutines for copying $(...) and `...` things */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5259 | static void add_till_backquote(o_string *dest, struct in_str *input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5260 | /* '...' */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5261 | 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] | 5262 | { |
| 5263 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5264 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5265 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5266 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5267 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5268 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5269 | if (ch == '\'') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5270 | return; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5271 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5272 | } |
| 5273 | } |
| 5274 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5275 | 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] | 5276 | { |
| 5277 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5278 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5279 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5280 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5281 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5282 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5283 | if (ch == '"') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5284 | return; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5285 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5286 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5287 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5288 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5289 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5290 | if (ch == '`') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5291 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5292 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5293 | continue; |
| 5294 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 5295 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5296 | } |
| 5297 | } |
| 5298 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 5299 | * \` quoting. |
| 5300 | * "Within the backquoted style of command substitution, backslash |
| 5301 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 5302 | * The search for the matching backquote shall be satisfied by the first |
| 5303 | * backquote found without a preceding backslash; during this search, |
| 5304 | * if a non-escaped backquote is encountered within a shell comment, |
| 5305 | * a here-document, an embedded command substitution of the $(command) |
| 5306 | * form, or a quoted string, undefined results occur. A single-quoted |
| 5307 | * or double-quoted string that begins, but does not end, within the |
| 5308 | * "`...`" sequence produces undefined results." |
| 5309 | * Example Output |
| 5310 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 5311 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5312 | static void add_till_backquote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5313 | { |
| 5314 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5315 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5316 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5317 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5318 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5319 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5320 | if (ch == '`') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5321 | return; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5322 | if (ch == '\\') { |
| 5323 | /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5324 | int ch2 = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5325 | if (ch2 == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5326 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5327 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5328 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5329 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5330 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5331 | ch = ch2; |
| 5332 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5333 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5334 | } |
| 5335 | } |
| 5336 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 5337 | * quoting and nested ()s. |
| 5338 | * "With the $(command) style of command substitution, all characters |
| 5339 | * following the open parenthesis to the matching closing parenthesis |
| 5340 | * constitute the command. Any valid shell script can be used for command, |
| 5341 | * except a script consisting solely of redirections which produces |
| 5342 | * unspecified results." |
| 5343 | * Example Output |
| 5344 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 5345 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 5346 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 5347 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5348 | 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] | 5349 | { |
| 5350 | int count = 0; |
| 5351 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5352 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5353 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5354 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5355 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5356 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5357 | if (ch == '(') |
| 5358 | count++; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5359 | if (ch == ')') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5360 | if (--count < 0) { |
| 5361 | if (!dbl) |
| 5362 | break; |
| 5363 | if (i_peek(input) == ')') { |
| 5364 | i_getch(input); |
| 5365 | break; |
| 5366 | } |
| 5367 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5368 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5369 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5370 | if (ch == '\'') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5371 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5372 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5373 | continue; |
| 5374 | } |
| 5375 | if (ch == '"') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5376 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5377 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5378 | continue; |
| 5379 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5380 | if (ch == '\\') { |
| 5381 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5382 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5383 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5384 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5385 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5386 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5387 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5388 | continue; |
| 5389 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5390 | } |
| 5391 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5392 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5393 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5394 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5395 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5396 | #define handle_dollar(as_string, dest, input) \ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5397 | handle_dollar(dest, input) |
| 5398 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5399 | static int handle_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5400 | o_string *dest, |
| 5401 | struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5402 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5403 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5404 | unsigned char quote_mask = dest->o_escape ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5405 | |
| 5406 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5407 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5408 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5409 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 5410 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5411 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5412 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5413 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5414 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5415 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5416 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5417 | if (!isalnum(ch) && ch != '_') |
| 5418 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5419 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5420 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5421 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5422 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5423 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5424 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5425 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5426 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5427 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5428 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5429 | o_addchr(dest, ch | quote_mask); |
| 5430 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5431 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5432 | case '$': /* pid */ |
| 5433 | case '!': /* last bg pid */ |
| 5434 | case '?': /* last exit code */ |
| 5435 | case '#': /* number of args */ |
| 5436 | case '*': /* args */ |
| 5437 | case '@': /* args */ |
| 5438 | goto make_one_char_var; |
| 5439 | case '{': { |
| 5440 | bool first_char, all_digits; |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 5441 | int expansion; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 5442 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5443 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5444 | nommu_addchr(as_string, ch); |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 5445 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5446 | |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 5447 | /* TODO: maybe someone will try to escape the '}' */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5448 | expansion = 0; |
| 5449 | first_char = true; |
| 5450 | all_digits = false; |
| 5451 | while (1) { |
| 5452 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5453 | nommu_addchr(as_string, ch); |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5454 | if (ch == '}') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5455 | break; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5456 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5457 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5458 | if (first_char) { |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5459 | if (ch == '#') { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5460 | /* ${#var}: length of var contents */ |
| 5461 | goto char_ok; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5462 | } |
| 5463 | if (isdigit(ch)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5464 | all_digits = true; |
| 5465 | goto char_ok; |
| 5466 | } |
Mike Frysinger | dc3bc40 | 2009-06-01 14:09:09 -0400 | [diff] [blame] | 5467 | /* They're being verbose and doing ${?} */ |
| 5468 | if (i_peek(input) == '}' && strchr("$!?#*@_", ch)) |
| 5469 | goto char_ok; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5470 | } |
| 5471 | |
| 5472 | if (expansion < 2 |
| 5473 | && ( (all_digits && !isdigit(ch)) |
| 5474 | || (!all_digits && !isalnum(ch) && ch != '_') |
| 5475 | ) |
| 5476 | ) { |
| 5477 | /* handle parameter expansions |
| 5478 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 5479 | */ |
| 5480 | if (first_char) |
| 5481 | goto case_default; |
| 5482 | switch (ch) { |
| 5483 | case ':': /* null modifier */ |
| 5484 | if (expansion == 0) { |
| 5485 | debug_printf_parse(": null modifier\n"); |
| 5486 | ++expansion; |
| 5487 | break; |
| 5488 | } |
| 5489 | goto case_default; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5490 | case '#': /* remove prefix */ |
| 5491 | case '%': /* remove suffix */ |
| 5492 | if (expansion == 0) { |
| 5493 | debug_printf_parse(": remove suffix/prefix\n"); |
| 5494 | expansion = 2; |
| 5495 | break; |
| 5496 | } |
| 5497 | goto case_default; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5498 | case '-': /* default value */ |
| 5499 | case '=': /* assign default */ |
| 5500 | case '+': /* alternative */ |
| 5501 | case '?': /* error indicate */ |
| 5502 | debug_printf_parse(": parameter expansion\n"); |
| 5503 | expansion = 2; |
| 5504 | break; |
| 5505 | default: |
| 5506 | case_default: |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5507 | syntax_error_unterm_str("${name}"); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5508 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 5509 | return 1; |
| 5510 | } |
| 5511 | } |
| 5512 | char_ok: |
| 5513 | debug_printf_parse(": '%c'\n", ch); |
| 5514 | o_addchr(dest, ch | quote_mask); |
| 5515 | quote_mask = 0; |
| 5516 | first_char = false; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5517 | } /* while (1) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5518 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5519 | break; |
| 5520 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5521 | #if (ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK) |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5522 | case '(': { |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5523 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5524 | int pos; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5525 | # endif |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5526 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5527 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5528 | # if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5529 | if (i_peek(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5530 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5531 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5532 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5533 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5534 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5535 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5536 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5537 | add_till_closing_paren(dest, input, true); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5538 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5539 | if (as_string) { |
| 5540 | o_addstr(as_string, dest->data + pos); |
| 5541 | o_addchr(as_string, ')'); |
| 5542 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5543 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5544 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5545 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5546 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5547 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5548 | # endif |
| 5549 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5550 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5551 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5552 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5553 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5554 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5555 | add_till_closing_paren(dest, input, false); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5556 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5557 | if (as_string) { |
| 5558 | o_addstr(as_string, dest->data + pos); |
| 5559 | o_addchr(as_string, '`'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5560 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5561 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5562 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5563 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5564 | break; |
| 5565 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5566 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5567 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5568 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5569 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5570 | ch = i_peek(input); |
| 5571 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 5572 | ch = '_'; |
| 5573 | goto make_var; |
| 5574 | } |
| 5575 | /* else: it's $_ */ |
| 5576 | /* TODO: */ |
| 5577 | /* $_ Shell or shell script name; or last cmd name */ |
| 5578 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 5579 | default: |
| 5580 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5581 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5582 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5583 | return 0; |
| 5584 | } |
| 5585 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5586 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5587 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5588 | parse_stream_dquoted(dest, input, dquote_end) |
| 5589 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5590 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5591 | o_string *dest, |
| 5592 | struct in_str *input, |
| 5593 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5594 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5595 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5596 | int next; |
| 5597 | |
| 5598 | again: |
| 5599 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5600 | if (ch != EOF) |
| 5601 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5602 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5603 | if (dest->o_assignment == NOT_ASSIGNMENT) |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5604 | dest->o_escape ^= 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5605 | debug_printf_parse("parse_stream_dquoted return 0\n"); |
| 5606 | return 0; |
| 5607 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5608 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5609 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5610 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5611 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5612 | } |
| 5613 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5614 | if (ch != '\n') { |
| 5615 | next = i_peek(input); |
| 5616 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5617 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 5618 | ch, ch, dest->o_escape); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5619 | if (ch == '\\') { |
| 5620 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5621 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5622 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5623 | } |
| 5624 | /* bash: |
| 5625 | * "The backslash retains its special meaning [in "..."] |
| 5626 | * only when followed by one of the following characters: |
| 5627 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 5628 | * within double quotes by preceding it with a backslash." |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5629 | */ |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 5630 | if (strchr("$`\"\\\n", next) != NULL) { |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 5631 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 5632 | if (ch != '\n') { |
| 5633 | o_addqchr(dest, ch); |
| 5634 | nommu_addchr(as_string, ch); |
| 5635 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5636 | } else { |
| 5637 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 5638 | nommu_addchr(as_string, '\\'); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5639 | } |
| 5640 | goto again; |
| 5641 | } |
| 5642 | if (ch == '$') { |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5643 | if (handle_dollar(as_string, dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5644 | debug_printf_parse("parse_stream_dquoted return 1: " |
| 5645 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5646 | return 1; |
| 5647 | } |
| 5648 | goto again; |
| 5649 | } |
| 5650 | #if ENABLE_HUSH_TICK |
| 5651 | if (ch == '`') { |
| 5652 | //int pos = dest->length; |
| 5653 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5654 | o_addchr(dest, 0x80 | '`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5655 | add_till_backquote(dest, input); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5656 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5657 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5658 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5659 | } |
| 5660 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5661 | o_addQchr(dest, ch); |
| 5662 | if (ch == '=' |
| 5663 | && (dest->o_assignment == MAYBE_ASSIGNMENT |
| 5664 | || dest->o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5665 | && is_well_formed_var_name(dest->data, '=') |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5666 | ) { |
| 5667 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 5668 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5669 | goto again; |
| 5670 | } |
| 5671 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5672 | /* |
| 5673 | * Scan input until EOF or end_trigger char. |
| 5674 | * Return a list of pipes to execute, or NULL on EOF |
| 5675 | * or if end_trigger character is met. |
| 5676 | * On syntax error, exit is shell is not interactive, |
| 5677 | * reset parsing machinery and start parsing anew, |
| 5678 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5679 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5680 | static struct pipe *parse_stream(char **pstring, |
| 5681 | struct in_str *input, |
| 5682 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5683 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5684 | struct parse_context ctx; |
| 5685 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5686 | int is_in_dquote; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5687 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5688 | |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5689 | /* Double-quote state is handled in the state variable is_in_dquote. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5690 | * 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] | 5691 | * found. When recursing, quote state is passed in via dest->o_escape. |
| 5692 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5693 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
| 5694 | end_trigger ? : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5695 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5696 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5697 | G.ifs = get_local_var_value("IFS"); |
| 5698 | if (G.ifs == NULL) |
| 5699 | G.ifs = " \t\n"; |
| 5700 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5701 | reset: |
| 5702 | #if ENABLE_HUSH_INTERACTIVE |
| 5703 | input->promptmode = 0; /* PS1 */ |
| 5704 | #endif |
| 5705 | /* dest.o_assignment = MAYBE_ASSIGNMENT; - already is */ |
| 5706 | initialize_context(&ctx); |
| 5707 | is_in_dquote = 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5708 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5709 | while (1) { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5710 | const char *is_ifs; |
| 5711 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5712 | int ch; |
| 5713 | int next; |
| 5714 | int redir_fd; |
| 5715 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5716 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5717 | if (is_in_dquote) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5718 | /* dest.o_quoted = 1; - already is (see below) */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5719 | if (parse_stream_dquoted(&ctx.as_string, &dest, input, '"')) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5720 | goto parse_error; |
| 5721 | } |
| 5722 | /* We reached closing '"' */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5723 | is_in_dquote = 0; |
| 5724 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5725 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5726 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 5727 | ch, ch, dest.o_escape); |
| 5728 | if (ch == EOF) { |
| 5729 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5730 | |
| 5731 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5732 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5733 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5734 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5735 | /* end_trigger == '}' case errors out earlier, |
| 5736 | * checking only ')' */ |
| 5737 | if (end_trigger == ')') { |
| 5738 | syntax_error_unterm_ch('('); /* exits */ |
| 5739 | /* goto parse_error; */ |
| 5740 | } |
| 5741 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5742 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5743 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5744 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5745 | o_free(&dest); |
| 5746 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5747 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5748 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5749 | /* (this makes bare "&" cmd a no-op. |
| 5750 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5751 | if (pi->num_cmds == 0 |
| 5752 | IF_HAS_KEYWORDS( && pi->res_word == RES_NONE) |
| 5753 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5754 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5755 | pi = NULL; |
| 5756 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5757 | #if !BB_MMU |
| 5758 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 5759 | if (pstring) |
| 5760 | *pstring = ctx.as_string.data; |
| 5761 | else |
| 5762 | o_free_unsafe(&ctx.as_string); |
| 5763 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5764 | debug_leave(); |
| 5765 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5766 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5767 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5768 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5769 | is_ifs = strchr(G.ifs, ch); |
| 5770 | is_special = strchr("<>;&|(){}#'" /* special outside of "str" */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 5771 | "\\$\"" IF_HUSH_TICK("`") /* always special */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5772 | , ch); |
| 5773 | |
| 5774 | if (!is_special && !is_ifs) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5775 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5776 | o_addQchr(&dest, ch); |
| 5777 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 5778 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5779 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5780 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5781 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5782 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5783 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5784 | continue; |
| 5785 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5786 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5787 | if (is_ifs) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5788 | if (done_word(&dest, &ctx)) { |
| 5789 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 5790 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5791 | if (ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5792 | #if ENABLE_HUSH_CASE |
| 5793 | /* "case ... in <newline> word) ..." - |
| 5794 | * newlines are ignored (but ';' wouldn't be) */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5795 | if (ctx.command->argv == NULL |
| 5796 | && ctx.ctx_res_w == RES_MATCH |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5797 | ) { |
| 5798 | continue; |
| 5799 | } |
| 5800 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5801 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5802 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5803 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 5804 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5805 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5806 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5807 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5808 | heredoc_cnt = 0; |
| 5809 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5810 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5811 | ch = ';'; |
Denis Vlasenko | 7c98612 | 2009-04-04 12:15:42 +0000 | [diff] [blame] | 5812 | /* note: if (is_ifs) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5813 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5814 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5815 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5816 | |
| 5817 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 5818 | * } is an ordinary char in this case, even inside { cmd; } |
| 5819 | * Pathological example: { ""}; } should exec "}" cmd |
| 5820 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5821 | if (ch == '}') { |
| 5822 | if (!IS_NULL_CMD(ctx.command) /* cmd } */ |
| 5823 | || dest.length != 0 /* word} */ |
| 5824 | || dest.o_quoted /* ""} */ |
| 5825 | ) { |
| 5826 | goto ordinary_char; |
| 5827 | } |
| 5828 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
| 5829 | goto skip_end_trigger; |
| 5830 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5831 | } |
| 5832 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5833 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5834 | && (ch != ';' || heredoc_cnt == 0) |
| 5835 | #if ENABLE_HUSH_CASE |
| 5836 | && (ch != ')' |
| 5837 | || ctx.ctx_res_w != RES_MATCH |
| 5838 | || (!dest.o_quoted && strcmp(dest.data, "esac") == 0) |
| 5839 | ) |
| 5840 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5841 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5842 | if (heredoc_cnt) { |
| 5843 | /* This is technically valid: |
| 5844 | * { cat <<HERE; }; echo Ok |
| 5845 | * heredoc |
| 5846 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5847 | * HERE |
| 5848 | * but we don't support this. |
| 5849 | * We require heredoc to be in enclosing {}/(), |
| 5850 | * if any. |
| 5851 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5852 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5853 | goto parse_error; |
| 5854 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5855 | if (done_word(&dest, &ctx)) { |
| 5856 | goto parse_error; |
| 5857 | } |
| 5858 | done_pipe(&ctx, PIPE_SEQ); |
| 5859 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5860 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5861 | if (!HAS_KEYWORDS |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5862 | 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] | 5863 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5864 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5865 | #if !BB_MMU |
| 5866 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 5867 | if (pstring) |
| 5868 | *pstring = ctx.as_string.data; |
| 5869 | else |
| 5870 | o_free_unsafe(&ctx.as_string); |
| 5871 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5872 | debug_leave(); |
| 5873 | debug_printf_parse("parse_stream return %p: " |
| 5874 | "end_trigger char found\n", |
| 5875 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5876 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5877 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5878 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5879 | skip_end_trigger: |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5880 | if (is_ifs) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5881 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5882 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5883 | next = '\0'; |
| 5884 | if (ch != '\n') { |
| 5885 | next = i_peek(input); |
| 5886 | } |
| 5887 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5888 | /* Catch <, > before deciding whether this word is |
| 5889 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 5890 | switch (ch) { |
| 5891 | case '>': |
| 5892 | redir_fd = redirect_opt_num(&dest); |
| 5893 | if (done_word(&dest, &ctx)) { |
| 5894 | goto parse_error; |
| 5895 | } |
| 5896 | redir_style = REDIRECT_OVERWRITE; |
| 5897 | if (next == '>') { |
| 5898 | redir_style = REDIRECT_APPEND; |
| 5899 | ch = i_getch(input); |
| 5900 | nommu_addchr(&ctx.as_string, ch); |
| 5901 | } |
| 5902 | #if 0 |
| 5903 | else if (next == '(') { |
| 5904 | syntax_error(">(process) not supported"); |
| 5905 | goto parse_error; |
| 5906 | } |
| 5907 | #endif |
| 5908 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5909 | goto parse_error; |
| 5910 | continue; /* back to top of while (1) */ |
| 5911 | case '<': |
| 5912 | redir_fd = redirect_opt_num(&dest); |
| 5913 | if (done_word(&dest, &ctx)) { |
| 5914 | goto parse_error; |
| 5915 | } |
| 5916 | redir_style = REDIRECT_INPUT; |
| 5917 | if (next == '<') { |
| 5918 | redir_style = REDIRECT_HEREDOC; |
| 5919 | heredoc_cnt++; |
| 5920 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 5921 | ch = i_getch(input); |
| 5922 | nommu_addchr(&ctx.as_string, ch); |
| 5923 | } else if (next == '>') { |
| 5924 | redir_style = REDIRECT_IO; |
| 5925 | ch = i_getch(input); |
| 5926 | nommu_addchr(&ctx.as_string, ch); |
| 5927 | } |
| 5928 | #if 0 |
| 5929 | else if (next == '(') { |
| 5930 | syntax_error("<(process) not supported"); |
| 5931 | goto parse_error; |
| 5932 | } |
| 5933 | #endif |
| 5934 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5935 | goto parse_error; |
| 5936 | continue; /* back to top of while (1) */ |
| 5937 | } |
| 5938 | |
| 5939 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 5940 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 5941 | && !ctx.pending_redirect |
| 5942 | ) { |
| 5943 | /* ch is a special char and thus this word |
| 5944 | * cannot be an assignment */ |
| 5945 | dest.o_assignment = NOT_ASSIGNMENT; |
| 5946 | } |
| 5947 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5948 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5949 | case '#': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5950 | if (dest.length == 0) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5951 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5952 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5953 | if (ch == EOF || ch == '\n') |
| 5954 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5955 | i_getch(input); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5956 | /* note: we do not add it to &ctx.as_string */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5957 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5958 | nommu_addchr(&ctx.as_string, '\n'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5959 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5960 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5961 | } |
| 5962 | break; |
| 5963 | case '\\': |
| 5964 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5965 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5966 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5967 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5968 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 5969 | if (ch != '\n') { |
| 5970 | o_addchr(&dest, '\\'); |
| 5971 | nommu_addchr(&ctx.as_string, '\\'); |
| 5972 | o_addchr(&dest, ch); |
| 5973 | nommu_addchr(&ctx.as_string, ch); |
| 5974 | /* Example: echo Hello \2>file |
| 5975 | * we need to know that word 2 is quoted */ |
| 5976 | dest.o_quoted = 1; |
| 5977 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5978 | break; |
| 5979 | case '$': |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5980 | if (handle_dollar(&ctx.as_string, &dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5981 | debug_printf_parse("parse_stream parse error: " |
| 5982 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5983 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5984 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5985 | break; |
| 5986 | case '\'': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5987 | dest.o_quoted = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5988 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5989 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5990 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5991 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5992 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5993 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5994 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5995 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5996 | break; |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 5997 | o_addqchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5998 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5999 | break; |
| 6000 | case '"': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6001 | dest.o_quoted = 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6002 | is_in_dquote ^= 1; /* invert */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6003 | if (dest.o_assignment == NOT_ASSIGNMENT) |
| 6004 | dest.o_escape ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6005 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 6006 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6007 | case '`': { |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6008 | #if !BB_MMU |
| 6009 | int pos; |
| 6010 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6011 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6012 | o_addchr(&dest, '`'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6013 | #if !BB_MMU |
| 6014 | pos = dest.length; |
| 6015 | #endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6016 | add_till_backquote(&dest, input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6017 | #if !BB_MMU |
| 6018 | o_addstr(&ctx.as_string, dest.data + pos); |
| 6019 | o_addchr(&ctx.as_string, '`'); |
| 6020 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6021 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6022 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6023 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6024 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 6025 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6026 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6027 | #if ENABLE_HUSH_CASE |
| 6028 | case_semi: |
| 6029 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6030 | if (done_word(&dest, &ctx)) { |
| 6031 | goto parse_error; |
| 6032 | } |
| 6033 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6034 | #if ENABLE_HUSH_CASE |
| 6035 | /* Eat multiple semicolons, detect |
| 6036 | * whether it means something special */ |
| 6037 | while (1) { |
| 6038 | ch = i_peek(input); |
| 6039 | if (ch != ';') |
| 6040 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6041 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6042 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 6043 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6044 | ctx.ctx_dsemicolon = 1; |
| 6045 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6046 | break; |
| 6047 | } |
| 6048 | } |
| 6049 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6050 | new_cmd: |
| 6051 | /* We just finished a cmd. New one may start |
| 6052 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6053 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6054 | break; |
| 6055 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6056 | if (done_word(&dest, &ctx)) { |
| 6057 | goto parse_error; |
| 6058 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6059 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6060 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6061 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6062 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6063 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6064 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6065 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6066 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6067 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6068 | if (done_word(&dest, &ctx)) { |
| 6069 | goto parse_error; |
| 6070 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 6071 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6072 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6073 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 6074 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6075 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6076 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6077 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6078 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6079 | } else { |
| 6080 | /* we could pick up a file descriptor choice here |
| 6081 | * with redirect_opt_num(), but bash doesn't do it. |
| 6082 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6083 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6084 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6085 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6086 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6087 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6088 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6089 | if (ctx.ctx_res_w == RES_MATCH |
| 6090 | && ctx.command->argv == NULL /* not (word|(... */ |
| 6091 | && dest.length == 0 /* not word(... */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6092 | && dest.o_quoted == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6093 | ) { |
| 6094 | continue; |
| 6095 | } |
| 6096 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6097 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6098 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 6099 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6100 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6101 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6102 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6103 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6104 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6105 | goto case_semi; |
| 6106 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6107 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 6108 | /* proper use of this character is caught by end_trigger: |
| 6109 | * if we see {, we call parse_group(..., end_trigger='}') |
| 6110 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 6111 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6112 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6113 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 6114 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 6115 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6116 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6117 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6118 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6119 | parse_error: |
| 6120 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6121 | struct parse_context *pctx; |
| 6122 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6123 | |
| 6124 | /* Clean up allocated tree. |
| 6125 | * Samples for finding leaks on syntax error recovery path. |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6126 | * Run them from interactive shell, watch pmap `pidof hush`. |
| 6127 | * while if false; then false; fi do break; done |
| 6128 | * (bash accepts it) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6129 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 6130 | * Samples to catch leaks at execution: |
| 6131 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 6132 | * 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] | 6133 | */ |
| 6134 | pctx = &ctx; |
| 6135 | do { |
| 6136 | /* Update pipe/command counts, |
| 6137 | * otherwise freeing may miss some */ |
| 6138 | done_pipe(pctx, PIPE_SEQ); |
| 6139 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 6140 | pctx->list_head, pctx); |
| 6141 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6142 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6143 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6144 | #if !BB_MMU |
| 6145 | o_free_unsafe(&pctx->as_string); |
| 6146 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6147 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6148 | if (pctx != &ctx) { |
| 6149 | free(pctx); |
| 6150 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6151 | IF_HAS_KEYWORDS(pctx = p2;) |
| 6152 | } while (HAS_KEYWORDS && pctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6153 | /* Free text, clear all dest fields */ |
| 6154 | o_free(&dest); |
| 6155 | /* If we are not in top-level parse, we return, |
| 6156 | * our caller will propagate error. |
| 6157 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6158 | if (end_trigger != ';') { |
| 6159 | #if !BB_MMU |
| 6160 | if (pstring) |
| 6161 | *pstring = NULL; |
| 6162 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6163 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6164 | return ERR_PTR; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6165 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6166 | /* Discard cached input, force prompt */ |
| 6167 | input->p = NULL; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6168 | IF_HUSH_INTERACTIVE(input->promptme = 1;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6169 | goto reset; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6170 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6171 | } |
| 6172 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6173 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6174 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 6175 | * end_trigger controls how often we stop parsing |
| 6176 | * NUL: parse all, execute, return |
| 6177 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 6178 | */ |
| 6179 | 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] | 6180 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6181 | while (1) { |
| 6182 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6183 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6184 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6185 | if (!pipe_list) /* EOF */ |
| 6186 | break; |
| 6187 | debug_print_tree(pipe_list, 0); |
| 6188 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 6189 | run_and_free_list(pipe_list); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6190 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6191 | } |
| 6192 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6193 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6194 | { |
| 6195 | struct in_str input; |
| 6196 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6197 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6198 | } |
| 6199 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6200 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6201 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6202 | struct in_str input; |
| 6203 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6204 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6205 | } |
| 6206 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6207 | /* Called a few times only (or even once if "sh -c") */ |
| 6208 | static void block_signals(int second_time) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 6209 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6210 | unsigned sig; |
| 6211 | unsigned mask; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 6212 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6213 | mask = (1 << SIGQUIT); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6214 | if (G_interactive_fd) { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 6215 | mask = (1 << SIGQUIT) | SPECIAL_INTERACTIVE_SIGS; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6216 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6217 | mask |= SPECIAL_JOB_SIGS; |
| 6218 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6219 | G.non_DFL_mask = mask; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 6220 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6221 | if (!second_time) |
| 6222 | sigprocmask(SIG_SETMASK, NULL, &G.blocked_set); |
| 6223 | sig = 0; |
| 6224 | while (mask) { |
| 6225 | if (mask & 1) |
| 6226 | sigaddset(&G.blocked_set, sig); |
| 6227 | mask >>= 1; |
| 6228 | sig++; |
| 6229 | } |
| 6230 | sigdelset(&G.blocked_set, SIGCHLD); |
| 6231 | |
| 6232 | sigprocmask(SIG_SETMASK, &G.blocked_set, |
| 6233 | second_time ? NULL : &G.inherited_set); |
| 6234 | /* POSIX allows shell to re-enable SIGCHLD |
| 6235 | * even if it was SIG_IGN on entry */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 6236 | #if ENABLE_HUSH_FAST |
| 6237 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6238 | if (!second_time) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 6239 | signal(SIGCHLD, SIGCHLD_handler); |
| 6240 | #else |
| 6241 | if (!second_time) |
| 6242 | signal(SIGCHLD, SIG_DFL); |
| 6243 | #endif |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6244 | } |
| 6245 | |
| 6246 | #if ENABLE_HUSH_JOB |
| 6247 | /* helper */ |
| 6248 | static void maybe_set_to_sigexit(int sig) |
| 6249 | { |
| 6250 | void (*handler)(int); |
| 6251 | /* non_DFL_mask'ed signals are, well, masked, |
| 6252 | * no need to set handler for them. |
| 6253 | */ |
| 6254 | if (!((G.non_DFL_mask >> sig) & 1)) { |
| 6255 | handler = signal(sig, sigexit); |
| 6256 | if (handler == SIG_IGN) /* oops... restore back to IGN! */ |
| 6257 | signal(sig, handler); |
| 6258 | } |
| 6259 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6260 | /* Set handlers to restore tty pgrp and exit */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6261 | static void set_fatal_handlers(void) |
| 6262 | { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 6263 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6264 | if (HUSH_DEBUG) { |
| 6265 | maybe_set_to_sigexit(SIGILL ); |
| 6266 | maybe_set_to_sigexit(SIGFPE ); |
| 6267 | maybe_set_to_sigexit(SIGBUS ); |
| 6268 | maybe_set_to_sigexit(SIGSEGV); |
| 6269 | maybe_set_to_sigexit(SIGTRAP); |
| 6270 | } /* else: hush is perfect. what SEGV? */ |
| 6271 | maybe_set_to_sigexit(SIGABRT); |
| 6272 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 6273 | maybe_set_to_sigexit(SIGPIPE); |
| 6274 | maybe_set_to_sigexit(SIGALRM); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 6275 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are masked. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6276 | * if we aren't interactive... but in this case |
| 6277 | * 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] | 6278 | /*maybe_set_to_sigexit(SIGHUP );*/ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6279 | /*maybe_set_to_sigexit(SIGTERM);*/ |
| 6280 | /*maybe_set_to_sigexit(SIGINT );*/ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 6281 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 6282 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 6283 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6284 | static int set_mode(const char cstate, const char mode) |
| 6285 | { |
| 6286 | int state = (cstate == '-' ? 1 : 0); |
| 6287 | switch (mode) { |
| 6288 | case 'n': G.fake_mode = state; break; |
| 6289 | case 'x': /*G.debug_mode = state;*/ break; |
| 6290 | default: return EXIT_FAILURE; |
| 6291 | } |
| 6292 | return EXIT_SUCCESS; |
| 6293 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6294 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 6295 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 6296 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6297 | { |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6298 | static const struct variable const_shell_ver = { |
| 6299 | .next = NULL, |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 6300 | .varstr = (char*)hush_version_str, |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6301 | .max_len = 1, /* 0 can provoke free(name) */ |
| 6302 | .flg_export = 1, |
| 6303 | .flg_read_only = 1, |
| 6304 | }; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6305 | int signal_mask_is_inited = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6306 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6307 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6308 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6309 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 6310 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 6311 | INIT_G(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6312 | if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6313 | G.last_exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6314 | #if !BB_MMU |
| 6315 | G.argv0_for_re_execing = argv[0]; |
| 6316 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6317 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6318 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 6319 | G.top_var = &G.shell_ver; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6320 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6321 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 6322 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6323 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6324 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6325 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6326 | if (e) while (*e) { |
| 6327 | char *value = strchr(*e, '='); |
| 6328 | if (value) { /* paranoia */ |
| 6329 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 6330 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 6331 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6332 | cur_var->max_len = strlen(*e); |
| 6333 | cur_var->flg_export = 1; |
| 6334 | } |
| 6335 | e++; |
| 6336 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6337 | debug_printf_env("putenv '%s'\n", hush_version_str); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 6338 | putenv((char *)hush_version_str); /* reinstate HUSH_VERSION */ |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 6339 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6340 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 6341 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6342 | G.global_argc = argc; |
| 6343 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 6344 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 6345 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 6346 | |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 6347 | if (setjmp(die_jmp)) { |
| 6348 | /* xfunc has failed! die die die */ |
| 6349 | /* no EXIT traps, this is an escape hatch! */ |
| 6350 | G.exiting = 1; |
| 6351 | hush_exit(xfunc_error_retval); |
| 6352 | } |
| 6353 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6354 | /* Shell is non-interactive at first. We need to call |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6355 | * block_signals(0) if we are going to execute "sh <script>", |
| 6356 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6357 | * If we later decide that we are interactive, we run block_signals(0) |
| 6358 | * (or re-run block_signals(1) if we ran block_signals(0) before) |
| 6359 | * in order to intercept (more) signals. |
| 6360 | */ |
| 6361 | |
| 6362 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 6363 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6364 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6365 | while (1) { |
| 6366 | opt = getopt(argc, argv, "c:xins" |
| 6367 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 6368 | "<:$:R:V:" |
| 6369 | # if ENABLE_HUSH_FUNCTIONS |
| 6370 | "F:" |
| 6371 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6372 | #endif |
| 6373 | ); |
| 6374 | if (opt <= 0) |
| 6375 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6376 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6377 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6378 | /* Possibilities: |
| 6379 | * sh ... -c 'script' |
| 6380 | * sh ... -c 'script' ARG0 [ARG1...] |
| 6381 | * On NOMMU, if builtin_argc != 0, |
| 6382 | * sh ... -c 'builtin' [BARGV...] "" ARG0 [ARG1...] |
| 6383 | * "" needs to be replaced with NULL |
| 6384 | * and BARGV vector fed to builtin function. |
| 6385 | * Note: this form never happens: |
| 6386 | * sh ... -c 'builtin' [BARGV...] "" |
| 6387 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6388 | if (!G.root_pid) |
| 6389 | G.root_pid = getpid(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6390 | G.global_argv = argv + optind; |
| 6391 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6392 | if (builtin_argc) { |
| 6393 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 6394 | const struct built_in_command *x; |
| 6395 | |
| 6396 | block_signals(0); /* 0: called 1st time */ |
| 6397 | x = find_builtin(optarg); |
| 6398 | if (x) { /* paranoia */ |
| 6399 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 6400 | G.global_argv += builtin_argc; |
| 6401 | G.global_argv[-1] = NULL; /* replace "" */ |
| 6402 | G.last_exitcode = x->function(argv + optind - 1); |
| 6403 | } |
| 6404 | goto final_return; |
| 6405 | } |
| 6406 | if (!G.global_argv[0]) { |
| 6407 | /* -c 'script' (no params): prevent empty $0 */ |
| 6408 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 6409 | G.global_argv[0] = argv[0]; |
| 6410 | G.global_argc--; |
| 6411 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6412 | block_signals(0); /* 0: called 1st time */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6413 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6414 | goto final_return; |
| 6415 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 6416 | /* Well, we cannot just declare interactiveness, |
| 6417 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6418 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6419 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 6420 | case 's': |
| 6421 | /* "-s" means "read from stdin", but this is how we always |
| 6422 | * operate, so simply do nothing here. */ |
| 6423 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6424 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 6425 | case '<': /* "big heredoc" support */ |
| 6426 | full_write(STDOUT_FILENO, optarg, strlen(optarg)); |
| 6427 | _exit(0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6428 | case '$': |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6429 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 6430 | optarg++; |
| 6431 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 6432 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6433 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6434 | optarg++; |
| 6435 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6436 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6437 | optarg++; |
| 6438 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6439 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6440 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6441 | case 'R': |
| 6442 | case 'V': |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6443 | set_local_var(xstrdup(optarg), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ opt == 'R'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6444 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 6445 | # if ENABLE_HUSH_FUNCTIONS |
| 6446 | case 'F': { |
| 6447 | struct function *funcp = new_function(optarg); |
| 6448 | /* funcp->name is already set to optarg */ |
| 6449 | /* funcp->body is set to NULL. It's a special case. */ |
| 6450 | funcp->body_as_string = argv[optind]; |
| 6451 | optind++; |
| 6452 | break; |
| 6453 | } |
| 6454 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6455 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6456 | case 'n': |
| 6457 | case 'x': |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6458 | if (!set_mode('-', opt)) |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6459 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6460 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6461 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6462 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 6463 | " or: sh -c command [args]...\n\n"); |
| 6464 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6465 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6466 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6467 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6468 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6469 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6470 | |
| 6471 | if (!G.root_pid) |
| 6472 | G.root_pid = getpid(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6473 | |
| 6474 | /* If we are login shell... */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6475 | if (argv[0] && argv[0][0] == '-') { |
| 6476 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6477 | debug_printf("sourcing /etc/profile\n"); |
| 6478 | input = fopen_for_read("/etc/profile"); |
| 6479 | if (input != NULL) { |
| 6480 | close_on_exec_on(fileno(input)); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6481 | block_signals(0); /* 0: called 1st time */ |
| 6482 | signal_mask_is_inited = 1; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6483 | parse_and_run_file(input); |
| 6484 | fclose(input); |
| 6485 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6486 | /* bash: after sourcing /etc/profile, |
| 6487 | * tries to source (in the given order): |
| 6488 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 6489 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6490 | * bash also sources ~/.bash_logout on exit. |
| 6491 | * If called as sh, skips .bash_XXX files. |
| 6492 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6493 | } |
| 6494 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6495 | if (argv[optind]) { |
| 6496 | FILE *input; |
| 6497 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6498 | * "bash <script>" (which is never interactive (unless -i?)) |
| 6499 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6500 | * If called as sh, does the same but with $ENV. |
| 6501 | */ |
| 6502 | debug_printf("running script '%s'\n", argv[optind]); |
| 6503 | G.global_argv = argv + optind; |
| 6504 | G.global_argc = argc - optind; |
| 6505 | input = xfopen_for_read(argv[optind]); |
| 6506 | close_on_exec_on(fileno(input)); |
| 6507 | if (!signal_mask_is_inited) |
| 6508 | block_signals(0); /* 0: called 1st time */ |
| 6509 | parse_and_run_file(input); |
| 6510 | #if ENABLE_FEATURE_CLEAN_UP |
| 6511 | fclose(input); |
| 6512 | #endif |
| 6513 | goto final_return; |
| 6514 | } |
| 6515 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6516 | /* Up to here, shell was non-interactive. Now it may become one. |
| 6517 | * NB: don't forget to (re)run block_signals(0/1) as needed. |
| 6518 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6519 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 6520 | /* A shell is interactive if the '-i' flag was given, |
| 6521 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 6522 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6523 | * no arguments remaining or the -s flag given |
| 6524 | * standard input is a terminal |
| 6525 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6526 | * Refer to Posix.2, the description of the 'sh' utility. |
| 6527 | */ |
| 6528 | #if ENABLE_HUSH_JOB |
| 6529 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6530 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 6531 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 6532 | if (G_saved_tty_pgrp < 0) |
| 6533 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6534 | |
| 6535 | /* try to dup stdin to high fd#, >= 255 */ |
| 6536 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 6537 | if (G_interactive_fd < 0) { |
| 6538 | /* try to dup to any fd */ |
| 6539 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6540 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6541 | /* give up */ |
| 6542 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6543 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 6544 | } |
| 6545 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6546 | // TODO: track & disallow any attempts of user |
| 6547 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6548 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6549 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6550 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6551 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6552 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6553 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6554 | /* If we were run as 'hush &', sleep until we are |
| 6555 | * in the foreground (tty pgrp == our pgrp). |
| 6556 | * If we get started under a job aware app (like bash), |
| 6557 | * make sure we are now in charge so we don't fight over |
| 6558 | * who gets the foreground */ |
| 6559 | while (1) { |
| 6560 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6561 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 6562 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6563 | break; |
| 6564 | /* send TTIN to ourself (should stop us) */ |
| 6565 | kill(- shell_pgrp, SIGTTIN); |
| 6566 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6567 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6568 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6569 | /* Block some signals */ |
| 6570 | block_signals(signal_mask_is_inited); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6571 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6572 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6573 | /* Set other signals to restore saved_tty_pgrp */ |
| 6574 | set_fatal_handlers(); |
| 6575 | /* Put ourselves in our own process group |
| 6576 | * (bash, too, does this only if ctty is available) */ |
| 6577 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 6578 | /* Grab control of the terminal */ |
| 6579 | tcsetpgrp(G_interactive_fd, getpid()); |
| 6580 | } |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 6581 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 6582 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6583 | enable_restore_tty_pgrp_on_exit(); /* sets die_sleep = -1 */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6584 | } else if (!signal_mask_is_inited) { |
| 6585 | block_signals(0); /* 0: called 1st time */ |
| 6586 | } /* else: block_signals(0) was done before */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6587 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6588 | /* No job control compiled in, only prompt/line editing */ |
| 6589 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6590 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 6591 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6592 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6593 | G_interactive_fd = dup(STDIN_FILENO); |
| 6594 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6595 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6596 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6597 | } |
| 6598 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6599 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6600 | close_on_exec_on(G_interactive_fd); |
| 6601 | block_signals(signal_mask_is_inited); |
| 6602 | } else if (!signal_mask_is_inited) { |
| 6603 | block_signals(0); |
| 6604 | } |
| 6605 | #else |
| 6606 | /* We have interactiveness code disabled */ |
| 6607 | if (!signal_mask_is_inited) { |
| 6608 | block_signals(0); |
| 6609 | } |
| 6610 | #endif |
| 6611 | /* bash: |
| 6612 | * if interactive but not a login shell, sources ~/.bashrc |
| 6613 | * (--norc turns this off, --rcfile <file> overrides) |
| 6614 | */ |
| 6615 | |
| 6616 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Mike Frysinger | 258275d | 2009-04-05 21:19:43 +0000 | [diff] [blame] | 6617 | printf("\n\n%s hush - the humble shell\n", bb_banner); |
Mike Frysinger | 26cf283 | 2009-04-24 06:40:30 +0000 | [diff] [blame] | 6618 | if (ENABLE_HUSH_HELP) |
| 6619 | puts("Enter 'help' for a list of built-in commands."); |
| 6620 | puts(""); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 6621 | } |
| 6622 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6623 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6624 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6625 | final_return: |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 6626 | #if ENABLE_FEATURE_CLEAN_UP |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6627 | if (G.cwd != bb_msg_unknown) |
| 6628 | free((char*)G.cwd); |
| 6629 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6630 | while (cur_var) { |
| 6631 | struct variable *tmp = cur_var; |
| 6632 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 6633 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6634 | cur_var = cur_var->next; |
| 6635 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 6636 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6637 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6638 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6639 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 6640 | |
| 6641 | |
| 6642 | #if ENABLE_LASH |
| 6643 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 6644 | int lash_main(int argc, char **argv) |
| 6645 | { |
| 6646 | //bb_error_msg("lash is deprecated, please use hush instead"); |
| 6647 | return hush_main(argc, argv); |
| 6648 | } |
| 6649 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6650 | |
| 6651 | |
| 6652 | /* |
| 6653 | * Built-ins |
| 6654 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6655 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6656 | { |
| 6657 | return 0; |
| 6658 | } |
| 6659 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6660 | static int FAST_FUNC builtin_test(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6661 | { |
| 6662 | int argc = 0; |
| 6663 | while (*argv) { |
| 6664 | argc++; |
| 6665 | argv++; |
| 6666 | } |
| 6667 | return test_main(argc, argv - argc); |
| 6668 | } |
| 6669 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6670 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6671 | { |
| 6672 | int argc = 0; |
| 6673 | while (*argv) { |
| 6674 | argc++; |
| 6675 | argv++; |
| 6676 | } |
| 6677 | return echo_main(argc, argv - argc); |
| 6678 | } |
| 6679 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6680 | static int FAST_FUNC builtin_eval(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6681 | { |
| 6682 | int rcode = EXIT_SUCCESS; |
| 6683 | |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6684 | if (*++argv) { |
| 6685 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6686 | /* bash: |
| 6687 | * eval "echo Hi; done" ("done" is syntax error): |
| 6688 | * "echo Hi" will not execute too. |
| 6689 | */ |
| 6690 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6691 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6692 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6693 | } |
| 6694 | return rcode; |
| 6695 | } |
| 6696 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6697 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6698 | { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6699 | const char *newdir = argv[1]; |
| 6700 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6701 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6702 | * bash says "bash: cd: HOME not set" and does nothing |
| 6703 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6704 | */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 6705 | newdir = get_local_var_value("HOME") ? : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6706 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6707 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6708 | /* Mimic bash message exactly */ |
| 6709 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6710 | return EXIT_FAILURE; |
| 6711 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6712 | get_cwd(1); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6713 | return EXIT_SUCCESS; |
| 6714 | } |
| 6715 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6716 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6717 | { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6718 | if (*++argv == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6719 | return EXIT_SUCCESS; /* bash does this */ |
| 6720 | { |
| 6721 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6722 | nommu_save_t dummy; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6723 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 6724 | /* TODO: if exec fails, bash does NOT exit! We do... */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6725 | pseudo_exec_argv(&dummy, argv, 0, NULL); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6726 | /* never returns */ |
| 6727 | } |
| 6728 | } |
| 6729 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6730 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6731 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 6732 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 6733 | |
| 6734 | /* interactive bash: |
| 6735 | * # trap "echo EEE" EXIT |
| 6736 | * # exit |
| 6737 | * exit |
| 6738 | * There are stopped jobs. |
| 6739 | * (if there are _stopped_ jobs, running ones don't count) |
| 6740 | * # exit |
| 6741 | * exit |
| 6742 | # EEE (then bash exits) |
| 6743 | * |
| 6744 | * we can use G.exiting = -1 as indicator "last cmd was exit" |
| 6745 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 6746 | |
| 6747 | /* note: EXIT trap is run by hush_exit */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6748 | if (*++argv == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6749 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6750 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 6751 | xfunc_error_retval = 255; |
| 6752 | /* bash: exit -2 == exit 254, no error msg */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6753 | hush_exit(xatoi(*argv) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6754 | } |
| 6755 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6756 | static void print_escaped(const char *s) |
| 6757 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6758 | if (*s == '\'') |
| 6759 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6760 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 6761 | const char *p = strchrnul(s, '\''); |
| 6762 | /* print 'xxxx', possibly just '' */ |
| 6763 | printf("'%.*s'", (int)(p - s), s); |
| 6764 | if (*p == '\0') |
| 6765 | break; |
| 6766 | s = p; |
| 6767 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6768 | /* s points to '; print "'''...'''" */ |
| 6769 | putchar('"'); |
| 6770 | do putchar('\''); while (*++s == '\''); |
| 6771 | putchar('"'); |
| 6772 | } while (*s); |
| 6773 | } |
| 6774 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6775 | #if !ENABLE_HUSH_LOCAL |
| 6776 | #define helper_export_local(argv, exp, lvl) \ |
| 6777 | helper_export_local(argv, exp) |
| 6778 | #endif |
| 6779 | static void helper_export_local(char **argv, int exp, int lvl) |
| 6780 | { |
| 6781 | do { |
| 6782 | char *name = *argv; |
| 6783 | |
| 6784 | /* So far we do not check that name is valid (TODO?) */ |
| 6785 | |
| 6786 | if (strchr(name, '=') == NULL) { |
| 6787 | struct variable *var; |
| 6788 | |
| 6789 | var = get_local_var(name); |
| 6790 | if (exp == -1) { /* unexporting? */ |
| 6791 | /* export -n NAME (without =VALUE) */ |
| 6792 | if (var) { |
| 6793 | var->flg_export = 0; |
| 6794 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 6795 | unsetenv(name); |
| 6796 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 6797 | continue; |
| 6798 | } |
| 6799 | if (exp == 1) { /* exporting? */ |
| 6800 | /* export NAME (without =VALUE) */ |
| 6801 | if (var) { |
| 6802 | var->flg_export = 1; |
| 6803 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 6804 | putenv(var->varstr); |
| 6805 | continue; |
| 6806 | } |
| 6807 | } |
| 6808 | /* Exporting non-existing variable. |
| 6809 | * bash does not put it in environment, |
| 6810 | * but remembers that it is exported, |
| 6811 | * and does put it in env when it is set later. |
| 6812 | * We just set it to "" and export. */ |
| 6813 | /* Or, it's "local NAME" (without =VALUE). |
| 6814 | * bash sets the value to "". */ |
| 6815 | name = xasprintf("%s=", name); |
| 6816 | } else { |
| 6817 | /* (Un)exporting/making local NAME=VALUE */ |
| 6818 | name = xstrdup(name); |
| 6819 | } |
| 6820 | set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0); |
| 6821 | } while (*++argv); |
| 6822 | } |
| 6823 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6824 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6825 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 6826 | unsigned opt_unexport; |
| 6827 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame^] | 6828 | #if ENABLE_HUSH_EXPORT_N |
| 6829 | /* "!": do not abort on errors */ |
| 6830 | opt_unexport = getopt32(argv, "!n"); |
| 6831 | if (opt_unexport == (uint32_t)-1) |
| 6832 | return EXIT_FAILURE; |
| 6833 | argv += optind; |
| 6834 | #else |
| 6835 | opt_unexport = 0; |
| 6836 | argv++; |
| 6837 | #endif |
| 6838 | |
| 6839 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6840 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6841 | if (e) { |
| 6842 | while (*e) { |
| 6843 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6844 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6845 | #else |
| 6846 | /* ash emits: export VAR='VAL' |
| 6847 | * bash: declare -x VAR="VAL" |
| 6848 | * we follow ash example */ |
| 6849 | const char *s = *e++; |
| 6850 | const char *p = strchr(s, '='); |
| 6851 | |
| 6852 | if (!p) /* wtf? take next variable */ |
| 6853 | continue; |
| 6854 | /* export var= */ |
| 6855 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6856 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6857 | putchar('\n'); |
| 6858 | #endif |
| 6859 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6860 | /*fflush(stdout); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6861 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6862 | return EXIT_SUCCESS; |
| 6863 | } |
| 6864 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6865 | helper_export_local(argv, (opt_unexport ? -1 : 1), 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6866 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6867 | return EXIT_SUCCESS; |
| 6868 | } |
| 6869 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6870 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6871 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6872 | { |
| 6873 | if (G.func_nest_level == 0) { |
| 6874 | bb_error_msg("%s: not in a function", argv[0]); |
| 6875 | return EXIT_FAILURE; /* bash compat */ |
| 6876 | } |
| 6877 | helper_export_local(argv, 0, G.func_nest_level); |
| 6878 | return EXIT_SUCCESS; |
| 6879 | } |
| 6880 | #endif |
| 6881 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6882 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6883 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6884 | int sig; |
| 6885 | char *new_cmd; |
| 6886 | |
| 6887 | if (!G.traps) |
| 6888 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 6889 | |
| 6890 | argv++; |
| 6891 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 6892 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6893 | /* No args: print all trapped */ |
| 6894 | for (i = 0; i < NSIG; ++i) { |
| 6895 | if (G.traps[i]) { |
| 6896 | printf("trap -- "); |
| 6897 | print_escaped(G.traps[i]); |
| 6898 | printf(" %s\n", get_signame(i)); |
| 6899 | } |
| 6900 | } |
| 6901 | /*fflush(stdout); - done after each builtin anyway */ |
| 6902 | return EXIT_SUCCESS; |
| 6903 | } |
| 6904 | |
| 6905 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6906 | /* If first arg is a number: reset all specified signals */ |
| 6907 | sig = bb_strtou(*argv, NULL, 10); |
| 6908 | if (errno == 0) { |
| 6909 | int ret; |
| 6910 | process_sig_list: |
| 6911 | ret = EXIT_SUCCESS; |
| 6912 | while (*argv) { |
| 6913 | sig = get_signum(*argv++); |
| 6914 | if (sig < 0 || sig >= NSIG) { |
| 6915 | ret = EXIT_FAILURE; |
| 6916 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 6917 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6918 | continue; |
| 6919 | } |
| 6920 | |
| 6921 | free(G.traps[sig]); |
| 6922 | G.traps[sig] = xstrdup(new_cmd); |
| 6923 | |
| 6924 | debug_printf("trap: setting SIG%s (%i) to '%s'", |
| 6925 | get_signame(sig), sig, G.traps[sig]); |
| 6926 | |
| 6927 | /* There is no signal for 0 (EXIT) */ |
| 6928 | if (sig == 0) |
| 6929 | continue; |
| 6930 | |
| 6931 | if (new_cmd) { |
| 6932 | sigaddset(&G.blocked_set, sig); |
| 6933 | } else { |
| 6934 | /* There was a trap handler, we are removing it |
| 6935 | * (if sig has non-DFL handling, |
| 6936 | * we don't need to do anything) */ |
| 6937 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 6938 | continue; |
| 6939 | sigdelset(&G.blocked_set, sig); |
| 6940 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6941 | } |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 6942 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6943 | return ret; |
| 6944 | } |
| 6945 | |
| 6946 | if (!argv[1]) { /* no second arg */ |
| 6947 | bb_error_msg("trap: invalid arguments"); |
| 6948 | return EXIT_FAILURE; |
| 6949 | } |
| 6950 | |
| 6951 | /* First arg is "-": reset all specified to default */ |
| 6952 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 6953 | /* Everything else: set arg as signal handler |
| 6954 | * (includes "" case, which ignores signal) */ |
| 6955 | if (argv[0][0] == '-') { |
| 6956 | if (argv[0][1] == '\0') { /* "-" */ |
| 6957 | /* new_cmd remains NULL: "reset these sigs" */ |
| 6958 | goto reset_traps; |
| 6959 | } |
| 6960 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 6961 | argv++; |
| 6962 | } |
| 6963 | /* else: "-something", no special meaning */ |
| 6964 | } |
| 6965 | new_cmd = *argv; |
| 6966 | reset_traps: |
| 6967 | argv++; |
| 6968 | goto process_sig_list; |
| 6969 | } |
| 6970 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6971 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6972 | static int FAST_FUNC builtin_type(char **argv) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6973 | { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 6974 | int ret = EXIT_SUCCESS; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6975 | |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 6976 | while (*++argv) { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6977 | const char *type; |
Denys Vlasenko | 171932d | 2009-05-28 17:07:22 +0200 | [diff] [blame] | 6978 | char *path = NULL; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6979 | |
| 6980 | if (0) {} /* make conditional compile easier below */ |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 6981 | /*else if (find_alias(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6982 | type = "an alias";*/ |
| 6983 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 6984 | else if (find_function(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6985 | type = "a function"; |
| 6986 | #endif |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 6987 | else if (find_builtin(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6988 | type = "a shell builtin"; |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 6989 | else if ((path = find_in_path(*argv)) != NULL) |
| 6990 | type = path; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 6991 | else { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 6992 | bb_error_msg("type: %s: not found", *argv); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6993 | ret = EXIT_FAILURE; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 6994 | continue; |
| 6995 | } |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6996 | |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 6997 | printf("%s is %s\n", *argv, type); |
| 6998 | free(path); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6999 | } |
| 7000 | |
| 7001 | return ret; |
| 7002 | } |
| 7003 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7004 | #if ENABLE_HUSH_JOB |
| 7005 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7006 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7007 | { |
| 7008 | int i, jobnum; |
| 7009 | struct pipe *pi; |
| 7010 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7011 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7012 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7013 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7014 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 7015 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7016 | for (pi = G.job_list; pi; pi = pi->next) { |
| 7017 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7018 | goto found; |
| 7019 | } |
| 7020 | } |
| 7021 | bb_error_msg("%s: no current job", argv[0]); |
| 7022 | return EXIT_FAILURE; |
| 7023 | } |
| 7024 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 7025 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 7026 | return EXIT_FAILURE; |
| 7027 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7028 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7029 | if (pi->jobid == jobnum) { |
| 7030 | goto found; |
| 7031 | } |
| 7032 | } |
| 7033 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 7034 | return EXIT_FAILURE; |
| 7035 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 7036 | /* TODO: bash prints a string representation |
| 7037 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7038 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7039 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7040 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7041 | } |
| 7042 | |
| 7043 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7044 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 7045 | for (i = 0; i < pi->num_cmds; i++) { |
| 7046 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 7047 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7048 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7049 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7050 | |
| 7051 | i = kill(- pi->pgrp, SIGCONT); |
| 7052 | if (i < 0) { |
| 7053 | if (errno == ESRCH) { |
| 7054 | delete_finished_bg_job(pi); |
| 7055 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7056 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7057 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7058 | } |
| 7059 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7060 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7061 | remove_bg_job(pi); |
| 7062 | return checkjobs_and_fg_shell(pi); |
| 7063 | } |
| 7064 | return EXIT_SUCCESS; |
| 7065 | } |
| 7066 | #endif |
| 7067 | |
| 7068 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7069 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7070 | { |
| 7071 | const struct built_in_command *x; |
| 7072 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7073 | printf( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7074 | "Built-in commands:\n" |
| 7075 | "------------------\n"); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7076 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
| 7077 | if (x->descr) |
| 7078 | printf("%s\t%s\n", x->cmd, x->descr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7079 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7080 | bb_putchar('\n'); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7081 | return EXIT_SUCCESS; |
| 7082 | } |
| 7083 | #endif |
| 7084 | |
| 7085 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7086 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7087 | { |
| 7088 | struct pipe *job; |
| 7089 | const char *status_string; |
| 7090 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7091 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7092 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7093 | status_string = "Stopped"; |
| 7094 | else |
| 7095 | status_string = "Running"; |
| 7096 | |
| 7097 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 7098 | } |
| 7099 | return EXIT_SUCCESS; |
| 7100 | } |
| 7101 | #endif |
| 7102 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7103 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7104 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7105 | { |
| 7106 | void *p; |
| 7107 | unsigned long l; |
| 7108 | |
| 7109 | /* Crude attempt to find where "free memory" starts, |
| 7110 | * sans fragmentation. */ |
| 7111 | p = malloc(240); |
| 7112 | l = (unsigned long)p; |
| 7113 | free(p); |
| 7114 | p = malloc(3400); |
| 7115 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 7116 | free(p); |
| 7117 | |
| 7118 | if (!G.memleak_value) |
| 7119 | G.memleak_value = l; |
| 7120 | |
| 7121 | l -= G.memleak_value; |
| 7122 | if ((long)l < 0) |
| 7123 | l = 0; |
| 7124 | l /= 1024; |
| 7125 | if (l > 127) |
| 7126 | l = 127; |
| 7127 | |
| 7128 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 7129 | return l; |
| 7130 | } |
| 7131 | #endif |
| 7132 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7133 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7134 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7135 | puts(get_cwd(0)); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7136 | return EXIT_SUCCESS; |
| 7137 | } |
| 7138 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7139 | static int FAST_FUNC builtin_read(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7140 | { |
| 7141 | char *string; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 7142 | const char *name = "REPLY"; |
| 7143 | |
| 7144 | if (argv[1]) { |
| 7145 | name = argv[1]; |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 7146 | /* bash (3.2.33(1)) bug: "read 0abcd" will execute, |
| 7147 | * and _after_ that_ it will complain */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 7148 | if (!is_well_formed_var_name(name, '\0')) { |
| 7149 | /* Mimic bash message */ |
| 7150 | bb_error_msg("read: '%s': not a valid identifier", name); |
| 7151 | return 1; |
| 7152 | } |
| 7153 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7154 | |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 7155 | //TODO: bash unbackslashes input, splits words and puts them in argv[i] |
| 7156 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7157 | string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7158 | return set_local_var(string, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7159 | } |
| 7160 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7161 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 7162 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7163 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7164 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 7165 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7166 | * set -- [argument...] |
| 7167 | * set -o |
| 7168 | * set +o |
| 7169 | * Implementations shall support the options in both their hyphen and |
| 7170 | * plus-sign forms. These options can also be specified as options to sh. |
| 7171 | * Examples: |
| 7172 | * Write out all variables and their values: set |
| 7173 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 7174 | * Turn on the -x and -v options: set -xv |
| 7175 | * Unset all positional parameters: set -- |
| 7176 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 7177 | * Set the positional parameters to the expansion of x, even if x expands |
| 7178 | * with a leading '-' or '+': set -- $x |
| 7179 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7180 | * 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] | 7181 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7182 | static int FAST_FUNC builtin_set(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7183 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7184 | int n; |
| 7185 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7186 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7187 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7188 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7189 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7190 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7191 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7192 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7193 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7194 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7195 | do { |
| 7196 | if (!strcmp(arg, "--")) { |
| 7197 | ++argv; |
| 7198 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7199 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 7200 | if (arg[0] != '+' && arg[0] != '-') |
| 7201 | break; |
| 7202 | for (n = 1; arg[n]; ++n) |
| 7203 | if (set_mode(arg[0], arg[n])) |
| 7204 | goto error; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7205 | } while ((arg = *++argv) != NULL); |
| 7206 | /* Now argv[0] is 1st argument */ |
| 7207 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7208 | if (arg == NULL) |
| 7209 | return EXIT_SUCCESS; |
| 7210 | set_argv: |
| 7211 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7212 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 7213 | g_argv = G.global_argv; |
| 7214 | if (G.global_args_malloced) { |
| 7215 | pp = g_argv; |
| 7216 | while (*++pp) |
| 7217 | free(*pp); |
| 7218 | g_argv[1] = NULL; |
| 7219 | } else { |
| 7220 | G.global_args_malloced = 1; |
| 7221 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 7222 | pp[0] = g_argv[0]; /* retain $0 */ |
| 7223 | g_argv = pp; |
| 7224 | } |
| 7225 | /* This realloc's G.global_argv */ |
| 7226 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 7227 | |
| 7228 | n = 1; |
| 7229 | while (*++pp) |
| 7230 | n++; |
| 7231 | G.global_argc = n; |
| 7232 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7233 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7234 | |
| 7235 | /* Nothing known, so abort */ |
| 7236 | error: |
| 7237 | bb_error_msg("set: %s: invalid option", arg); |
| 7238 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7239 | } |
| 7240 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7241 | static int FAST_FUNC builtin_shift(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7242 | { |
| 7243 | int n = 1; |
| 7244 | if (argv[1]) { |
| 7245 | n = atoi(argv[1]); |
| 7246 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7247 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 7248 | if (G.global_args_malloced) { |
| 7249 | int m = 1; |
| 7250 | while (m <= n) |
| 7251 | free(G.global_argv[m++]); |
| 7252 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7253 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 7254 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 7255 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7256 | return EXIT_SUCCESS; |
| 7257 | } |
| 7258 | return EXIT_FAILURE; |
| 7259 | } |
| 7260 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7261 | static int FAST_FUNC builtin_source(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7262 | { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7263 | char *arg_path; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7264 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7265 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7266 | #if ENABLE_HUSH_FUNCTIONS |
| 7267 | smallint sv_flg; |
| 7268 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7269 | |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7270 | if (*++argv == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7271 | return EXIT_FAILURE; |
| 7272 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7273 | if (strchr(*argv, '/') == NULL && (arg_path = find_in_path(*argv)) != NULL) { |
| 7274 | input = fopen_for_read(arg_path); |
| 7275 | free(arg_path); |
| 7276 | } else |
| 7277 | input = fopen_or_warn(*argv, "r"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7278 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7279 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7280 | return EXIT_FAILURE; |
| 7281 | } |
| 7282 | close_on_exec_on(fileno(input)); |
| 7283 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7284 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7285 | sv_flg = G.flag_return_in_progress; |
| 7286 | /* "we are inside sourced file, ok to use return" */ |
| 7287 | G.flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7288 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7289 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7290 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7291 | parse_and_run_file(input); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7292 | fclose(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7293 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7294 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7295 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7296 | G.flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7297 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7298 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7299 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7300 | } |
| 7301 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7302 | static int FAST_FUNC builtin_umask(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7303 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7304 | int rc; |
| 7305 | mode_t mask; |
| 7306 | |
| 7307 | mask = umask(0); |
| 7308 | if (argv[1]) { |
| 7309 | mode_t old_mask = mask; |
| 7310 | |
| 7311 | mask ^= 0777; |
| 7312 | rc = bb_parse_mode(argv[1], &mask); |
| 7313 | mask ^= 0777; |
| 7314 | if (rc == 0) { |
| 7315 | mask = old_mask; |
| 7316 | /* bash messages: |
| 7317 | * bash: umask: 'q': invalid symbolic mode operator |
| 7318 | * bash: umask: 999: octal number out of range |
| 7319 | */ |
| 7320 | bb_error_msg("%s: '%s' invalid mode", argv[0], argv[1]); |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7321 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7322 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7323 | rc = 1; |
| 7324 | /* Mimic bash */ |
| 7325 | printf("%04o\n", (unsigned) mask); |
| 7326 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7327 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7328 | umask(mask); |
| 7329 | |
| 7330 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7331 | } |
| 7332 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7333 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7334 | static int FAST_FUNC builtin_unset(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7335 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7336 | int ret; |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7337 | unsigned opts; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7338 | |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7339 | /* "!": do not abort on errors */ |
| 7340 | /* "+": stop at 1st non-option */ |
| 7341 | opts = getopt32(argv, "!+vf"); |
| 7342 | if (opts == (unsigned)-1) |
| 7343 | return EXIT_FAILURE; |
| 7344 | if (opts == 3) { |
| 7345 | bb_error_msg("unset: -v and -f are exclusive"); |
| 7346 | return EXIT_FAILURE; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7347 | } |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7348 | argv += optind; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7349 | |
| 7350 | ret = EXIT_SUCCESS; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7351 | while (*argv) { |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7352 | if (!(opts & 2)) { /* not -f */ |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7353 | if (unset_local_var(*argv)) { |
| 7354 | /* unset <nonexistent_var> doesn't fail. |
| 7355 | * Error is when one tries to unset RO var. |
| 7356 | * Message was printed by unset_local_var. */ |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7357 | ret = EXIT_FAILURE; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7358 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7359 | } |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 7360 | #if ENABLE_HUSH_FUNCTIONS |
| 7361 | else { |
| 7362 | unset_func(*argv); |
| 7363 | } |
| 7364 | #endif |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7365 | argv++; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7366 | } |
| 7367 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7368 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7369 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7370 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7371 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7372 | { |
| 7373 | int ret = EXIT_SUCCESS; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7374 | int status, sig; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7375 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7376 | if (*++argv == NULL) { |
| 7377 | /* Don't care about wait results */ |
| 7378 | /* Note 1: must wait until there are no more children */ |
| 7379 | /* Note 2: must be interruptible */ |
| 7380 | /* Examples: |
| 7381 | * $ sleep 3 & sleep 6 & wait |
| 7382 | * [1] 30934 sleep 3 |
| 7383 | * [2] 30935 sleep 6 |
| 7384 | * [1] Done sleep 3 |
| 7385 | * [2] Done sleep 6 |
| 7386 | * $ sleep 3 & sleep 6 & wait |
| 7387 | * [1] 30936 sleep 3 |
| 7388 | * [2] 30937 sleep 6 |
| 7389 | * [1] Done sleep 3 |
| 7390 | * ^C <-- after ~4 sec from keyboard |
| 7391 | * $ |
| 7392 | */ |
| 7393 | sigaddset(&G.blocked_set, SIGCHLD); |
| 7394 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7395 | while (1) { |
| 7396 | checkjobs(NULL); |
| 7397 | if (errno == ECHILD) |
| 7398 | break; |
| 7399 | /* Wait for SIGCHLD or any other signal of interest */ |
| 7400 | /* sigtimedwait with infinite timeout: */ |
| 7401 | sig = sigwaitinfo(&G.blocked_set, NULL); |
| 7402 | if (sig > 0) { |
| 7403 | sig = check_and_run_traps(sig); |
| 7404 | if (sig && sig != SIGCHLD) { /* see note 2 */ |
| 7405 | ret = 128 + sig; |
| 7406 | break; |
| 7407 | } |
| 7408 | } |
| 7409 | } |
| 7410 | sigdelset(&G.blocked_set, SIGCHLD); |
| 7411 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7412 | return ret; |
| 7413 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7414 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7415 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7416 | while (*argv) { |
| 7417 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 7418 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7419 | /* mimic bash message */ |
| 7420 | 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] | 7421 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7422 | } |
| 7423 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7424 | if (WIFSIGNALED(status)) |
| 7425 | ret = 128 + WTERMSIG(status); |
| 7426 | else if (WIFEXITED(status)) |
| 7427 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7428 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7429 | ret = EXIT_FAILURE; |
| 7430 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7431 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7432 | ret = 127; |
| 7433 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7434 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7435 | } |
| 7436 | |
| 7437 | return ret; |
| 7438 | } |
| 7439 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7440 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 7441 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 7442 | { |
| 7443 | if (argv[1]) { |
| 7444 | def = bb_strtou(argv[1], NULL, 10); |
| 7445 | if (errno || def < def_min || argv[2]) { |
| 7446 | bb_error_msg("%s: bad arguments", argv[0]); |
| 7447 | def = UINT_MAX; |
| 7448 | } |
| 7449 | } |
| 7450 | return def; |
| 7451 | } |
| 7452 | #endif |
| 7453 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 7454 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7455 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7456 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7457 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7458 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 7459 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 7460 | return EXIT_SUCCESS; /* bash compat */ |
| 7461 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7462 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7463 | |
| 7464 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 7465 | if (depth == UINT_MAX) |
| 7466 | G.flag_break_continue = BC_BREAK; |
| 7467 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7468 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7469 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7470 | return EXIT_SUCCESS; |
| 7471 | } |
| 7472 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7473 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7474 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 7475 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 7476 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7477 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 7478 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7479 | |
| 7480 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7481 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7482 | { |
| 7483 | int rc; |
| 7484 | |
| 7485 | if (G.flag_return_in_progress != -1) { |
| 7486 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 7487 | return EXIT_FAILURE; /* bash compat */ |
| 7488 | } |
| 7489 | |
| 7490 | G.flag_return_in_progress = 1; |
| 7491 | |
| 7492 | /* bash: |
| 7493 | * out of range: wraps around at 256, does not error out |
| 7494 | * non-numeric param: |
| 7495 | * f() { false; return qwe; }; f; echo $? |
| 7496 | * bash: return: qwe: numeric argument required <== we do this |
| 7497 | * 255 <== we also do this |
| 7498 | */ |
| 7499 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 7500 | return rc; |
| 7501 | } |
| 7502 | #endif |