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 |
| 47 | * Brace expansion |
| 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) |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 53 | * change { and } from special chars to reserved words |
Denis Vlasenko | fa4ca78 | 2009-04-16 12:00:15 +0000 | [diff] [blame] | 54 | * $var refs in function do not pick up values set by "var=val func" |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 55 | * builtins: return, ulimit |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 56 | * follow IFS rules more precisely, including update semantics |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 57 | * figure out what to do with backslash-newline |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 58 | * continuation lines, both explicit and implicit - done? |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 59 | * SIGHUP handling |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 60 | * separate job control from interactiveness |
| 61 | * (testcase: booting with init=/bin/hush does not show prompt (2009-04)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 62 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 63 | * 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] | 64 | */ |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 65 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 66 | #include <glob.h> |
| 67 | /* #include <dmalloc.h> */ |
| 68 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 69 | # include <fnmatch.h> |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 70 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 71 | #include "math.h" |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 72 | #include "match.h" |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 73 | #ifndef PIPE_BUF |
| 74 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
| 75 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 76 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 77 | |
| 78 | /* Debug build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 79 | #define LEAK_HUNTING 0 |
| 80 | #define BUILD_AS_NOMMU 0 |
| 81 | /* Enable/disable sanity checks. Ok to enable in production, |
| 82 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 83 | * Keeping 1 for now even in released versions. |
| 84 | */ |
| 85 | #define HUSH_DEBUG 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 86 | |
| 87 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 88 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 89 | # undef BB_MMU |
| 90 | # undef USE_FOR_NOMMU |
| 91 | # undef USE_FOR_MMU |
| 92 | # define BB_MMU 0 |
| 93 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 94 | # define USE_FOR_MMU(...) |
| 95 | #endif |
| 96 | |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 97 | #if defined SINGLE_APPLET_MAIN |
| 98 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 99 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 100 | # undef ENABLE_FEATURE_SH_STANDALONE |
| 101 | # undef USE_FEATURE_SH_STANDALONE |
| 102 | # define SKIP_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
| 103 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
| 104 | # define USE_FEATURE_SH_STANDALONE(...) |
| 105 | # define SKIP_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 106 | #endif |
| 107 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 108 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 109 | # undef ENABLE_FEATURE_EDITING |
| 110 | # define ENABLE_FEATURE_EDITING 0 |
| 111 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 112 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 113 | #endif |
| 114 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 115 | /* Do we support ANY keywords? */ |
| 116 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 117 | # define HAS_KEYWORDS 1 |
| 118 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 119 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 120 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 121 | # define HAS_KEYWORDS 0 |
| 122 | # define IF_HAS_KEYWORDS(...) |
| 123 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 124 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 125 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 126 | /* If you comment out one of these below, it will be #defined later |
| 127 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 128 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 129 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 130 | #define debug_printf_parse(...) do {} while (0) |
| 131 | #define debug_print_tree(a, b) do {} while (0) |
| 132 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 133 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 134 | #define debug_printf_jobs(...) do {} while (0) |
| 135 | #define debug_printf_expand(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 136 | #define debug_printf_glob(...) do {} while (0) |
| 137 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 138 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 139 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 140 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 141 | #define ERR_PTR ((void*)(long)1) |
| 142 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 143 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 144 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 145 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 146 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 147 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 148 | |
| 149 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 150 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 151 | */ |
| 152 | #if !BB_MMU |
| 153 | typedef struct nommu_save_t { |
| 154 | char **new_env; |
| 155 | char **old_env; |
| 156 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 157 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 158 | } nommu_save_t; |
| 159 | #endif |
| 160 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 161 | /* The descrip member of this structure is only used to make |
| 162 | * debugging output pretty */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 163 | static const struct { |
| 164 | int mode; |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 165 | signed char default_fd; |
| 166 | char descrip[3]; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 167 | } redir_table[] = { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 168 | { 0, 0, "??" }, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 169 | { O_RDONLY, 0, "<" }, |
| 170 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 171 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 172 | { O_RDONLY, 0, "<<" }, |
| 173 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 174 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 175 | /* { O_RDONLY, 77, "<<" }, */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 178 | typedef enum reserved_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 179 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 180 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 181 | RES_IF , |
| 182 | RES_THEN , |
| 183 | RES_ELIF , |
| 184 | RES_ELSE , |
| 185 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 186 | #endif |
| 187 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 188 | RES_FOR , |
| 189 | RES_WHILE , |
| 190 | RES_UNTIL , |
| 191 | RES_DO , |
| 192 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 193 | #endif |
| 194 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 195 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 196 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 197 | #if ENABLE_HUSH_CASE |
| 198 | RES_CASE , |
| 199 | /* two pseudo-keywords support contrived "case" syntax: */ |
| 200 | RES_MATCH , /* "word)" */ |
| 201 | RES_CASEI , /* "this command is inside CASE" */ |
| 202 | RES_ESAC , |
| 203 | #endif |
| 204 | RES_XXXX , |
| 205 | RES_SNTX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 206 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 207 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 208 | typedef struct o_string { |
| 209 | char *data; |
| 210 | int length; /* position where data is appended */ |
| 211 | int maxlen; |
| 212 | /* Protect newly added chars against globbing |
| 213 | * (by prepending \ to *, ?, [, \) */ |
| 214 | smallint o_escape; |
| 215 | smallint o_glob; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 216 | /* At least some part of the string was inside '' or "", |
| 217 | * possibly empty one: word"", wo''rd etc. */ |
| 218 | smallint o_quoted; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 219 | smallint has_empty_slot; |
| 220 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 221 | } o_string; |
| 222 | enum { |
| 223 | MAYBE_ASSIGNMENT = 0, |
| 224 | DEFINITELY_ASSIGNMENT = 1, |
| 225 | NOT_ASSIGNMENT = 2, |
| 226 | WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */ |
| 227 | }; |
| 228 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 229 | #define NULL_O_STRING { NULL } |
| 230 | |
| 231 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
| 232 | * available? Where is it documented? */ |
| 233 | typedef struct in_str { |
| 234 | const char *p; |
| 235 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 236 | char eof_flag; /* meaningless if ->p == NULL */ |
| 237 | char peek_buf[2]; |
| 238 | #if ENABLE_HUSH_INTERACTIVE |
| 239 | smallint promptme; |
| 240 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 241 | #endif |
| 242 | FILE *file; |
| 243 | int (*get) (struct in_str *); |
| 244 | int (*peek) (struct in_str *); |
| 245 | } in_str; |
| 246 | #define i_getch(input) ((input)->get(input)) |
| 247 | #define i_peek(input) ((input)->peek(input)) |
| 248 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 249 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 250 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 251 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 252 | int rd_fd; /* fd to redirect */ |
| 253 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 254 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 255 | smallint rd_type; /* (enum redir_type) */ |
| 256 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 257 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
| 258 | * 1: do we need to trim leading tabs? |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 259 | * 2: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 260 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 261 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 262 | typedef enum redir_type { |
| 263 | REDIRECT_INVALID = 0, |
| 264 | REDIRECT_INPUT = 1, |
| 265 | REDIRECT_OVERWRITE = 2, |
| 266 | REDIRECT_APPEND = 3, |
| 267 | REDIRECT_HEREDOC = 4, |
| 268 | REDIRECT_IO = 5, |
| 269 | REDIRECT_HEREDOC2 = 6, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 270 | |
| 271 | REDIRFD_CLOSE = -3, |
| 272 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 273 | REDIRFD_TO_FILE = -1, |
| 274 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 275 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 276 | HEREDOC_SKIPTABS = 1, |
| 277 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 278 | } redir_type; |
| 279 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 280 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 281 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 282 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 283 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 284 | smallint is_stopped; /* is the command currently running? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 285 | smallint grp_type; /* GRP_xxx */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 286 | #define GRP_NORMAL 0 |
| 287 | #define GRP_SUBSHELL 1 |
| 288 | #if ENABLE_HUSH_FUNCTIONS |
| 289 | # define GRP_FUNCTION 2 |
| 290 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 291 | struct pipe *group; /* if non-NULL, this "command" is { list }, |
| 292 | * ( list ), or a compound statement */ |
| 293 | #if !BB_MMU |
| 294 | char *group_as_string; |
| 295 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 296 | #if ENABLE_HUSH_FUNCTIONS |
| 297 | struct function *child_func; |
| 298 | /* This field is used to prevent a bug here: |
| 299 | * while...do f1() {a;}; f1; f1 {b;}; f1; done |
| 300 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 301 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
| 302 | * when we execute "f1 {b;}", we notice that f1 exists, |
| 303 | * and that it's "parent cmd" struct is still "alive", |
| 304 | * we put those fields back into cmd->xxx |
| 305 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 306 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 307 | * Without this trick, loop would execute a;b;b;b;... |
| 308 | * instead of correct sequence a;b;a;b;... |
| 309 | * When command is freed, it severs the link |
| 310 | * (sets ->child_func->parent_cmd to NULL). |
| 311 | */ |
| 312 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 313 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 314 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 315 | * and on execution these are substituted with their values. |
| 316 | * Substitution can make _several_ words out of one argv[n]! |
| 317 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 318 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 319 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 320 | struct redir_struct *redirects; /* I/O redirections */ |
| 321 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 322 | /* Is there anything in this command at all? */ |
| 323 | #define IS_NULL_CMD(cmd) \ |
| 324 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 325 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 326 | |
| 327 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 328 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 329 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 330 | int alive_cmds; /* number of commands running (not exited) */ |
| 331 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 332 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 333 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 334 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 335 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 336 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 337 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 338 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 339 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 340 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 341 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 342 | typedef enum pipe_style { |
| 343 | PIPE_SEQ = 1, |
| 344 | PIPE_AND = 2, |
| 345 | PIPE_OR = 3, |
| 346 | PIPE_BG = 4, |
| 347 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 348 | /* Is there anything in this pipe at all? */ |
| 349 | #define IS_NULL_PIPE(pi) \ |
| 350 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 351 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 352 | /* This holds pointers to the various results of parsing */ |
| 353 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 354 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 355 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 356 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 357 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 358 | /* last command in pipe (being constructed right now) */ |
| 359 | struct command *command; |
| 360 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 361 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 362 | #if !BB_MMU |
| 363 | o_string as_string; |
| 364 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 365 | #if HAS_KEYWORDS |
| 366 | smallint ctx_res_w; |
| 367 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 368 | #if ENABLE_HUSH_CASE |
| 369 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 370 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 371 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 372 | int old_flag; |
| 373 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 374 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 375 | * when we see "if" or "then", we malloc and copy current context, |
| 376 | * and make ->stack point to it. then we parse pipeN. |
| 377 | * when closing "then" / fi" / whatever is found, |
| 378 | * we move list_head into ->stack->command->group, |
| 379 | * copy ->stack into current context, and delete ->stack. |
| 380 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 381 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 382 | struct parse_context *stack; |
| 383 | #endif |
| 384 | }; |
| 385 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 386 | /* On program start, environ points to initial environment. |
| 387 | * putenv adds new pointers into it, unsetenv removes them. |
| 388 | * Neither of these (de)allocates the strings. |
| 389 | * setenv allocates new strings in malloc space and does putenv, |
| 390 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 391 | #define setenv(...) setenv_is_leaky_dont_use() |
| 392 | struct variable { |
| 393 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 394 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 395 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 396 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 397 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 398 | }; |
| 399 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 400 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 401 | BC_BREAK = 1, |
| 402 | BC_CONTINUE = 2, |
| 403 | }; |
| 404 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 405 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 406 | struct function { |
| 407 | struct function *next; |
| 408 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 409 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 410 | struct pipe *body; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 411 | #if !BB_MMU |
| 412 | char *body_as_string; |
| 413 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 414 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 415 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 416 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 417 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 418 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 419 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 420 | struct globals { |
| 421 | #if ENABLE_HUSH_INTERACTIVE |
| 422 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 423 | * _AND_ if we decided to act interactively */ |
| 424 | int interactive_fd; |
| 425 | const char *PS1; |
| 426 | const char *PS2; |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 427 | #define G_interactive_fd (G.interactive_fd) |
| 428 | #else |
| 429 | #define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 430 | #endif |
| 431 | #if ENABLE_FEATURE_EDITING |
| 432 | line_input_t *line_input_state; |
| 433 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 434 | pid_t root_pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 435 | pid_t last_bg_pid; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 436 | #if ENABLE_HUSH_JOB |
| 437 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 438 | pid_t saved_tty_pgrp; |
| 439 | int last_jobid; |
| 440 | struct pipe *job_list; |
| 441 | struct pipe *toplevel_list; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 442 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 443 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 444 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 445 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 446 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 447 | #if ENABLE_HUSH_FUNCTIONS |
| 448 | /* 0: outside of a function (or sourced file) |
| 449 | * -1: inside of a function, ok to use return builtin |
| 450 | * 1: return is invoked, skip all till end of func. |
| 451 | */ |
| 452 | smallint flag_return_in_progress; |
| 453 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 454 | smallint fake_mode; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 455 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 456 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 457 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 458 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 459 | smalluint global_args_malloced; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 460 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 461 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 462 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 463 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 464 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 465 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 466 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 467 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 468 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 469 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 470 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 471 | const char *cwd; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 472 | struct variable *top_var; /* = &G.shell_ver (set in main()) */ |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 473 | struct variable shell_ver; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 474 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 475 | struct function *top_func; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 476 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 477 | /* Signal and trap handling */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 478 | // unsigned count_SIGCHLD; |
| 479 | // unsigned handled_SIGCHLD; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 480 | /* which signals have non-DFL handler (even with no traps set)? */ |
| 481 | unsigned non_DFL_mask; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 482 | char **traps; /* char *traps[NSIG] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 483 | sigset_t blocked_set; |
| 484 | sigset_t inherited_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 485 | #if HUSH_DEBUG |
| 486 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 487 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 488 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 489 | char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2]; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 490 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 491 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 492 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 493 | * (too many defines). Also, I actually prefer to see when a variable |
| 494 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 495 | #define INIT_G() do { \ |
| 496 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 497 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 498 | |
| 499 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 500 | /* Function prototypes for builtins */ |
| 501 | static int builtin_cd(char **argv); |
| 502 | static int builtin_echo(char **argv); |
| 503 | static int builtin_eval(char **argv); |
| 504 | static int builtin_exec(char **argv); |
| 505 | static int builtin_exit(char **argv); |
| 506 | static int builtin_export(char **argv); |
| 507 | #if ENABLE_HUSH_JOB |
| 508 | static int builtin_fg_bg(char **argv); |
| 509 | static int builtin_jobs(char **argv); |
| 510 | #endif |
| 511 | #if ENABLE_HUSH_HELP |
| 512 | static int builtin_help(char **argv); |
| 513 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 514 | #if HUSH_DEBUG |
| 515 | static int builtin_memleak(char **argv); |
| 516 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 517 | static int builtin_pwd(char **argv); |
| 518 | static int builtin_read(char **argv); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 519 | static int builtin_set(char **argv); |
| 520 | static int builtin_shift(char **argv); |
| 521 | static int builtin_source(char **argv); |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 522 | static int builtin_test(char **argv); |
| 523 | static int builtin_trap(char **argv); |
| 524 | static int builtin_true(char **argv); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 525 | static int builtin_umask(char **argv); |
| 526 | static int builtin_unset(char **argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 527 | static int builtin_wait(char **argv); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 528 | #if ENABLE_HUSH_LOOPS |
| 529 | static int builtin_break(char **argv); |
| 530 | static int builtin_continue(char **argv); |
| 531 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 532 | #if ENABLE_HUSH_FUNCTIONS |
| 533 | static int builtin_return(char **argv); |
| 534 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 535 | |
| 536 | /* Table of built-in functions. They can be forked or not, depending on |
| 537 | * context: within pipes, they fork. As simple commands, they do not. |
| 538 | * When used in non-forking context, they can change global variables |
| 539 | * in the parent shell process. If forked, of course they cannot. |
| 540 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 541 | * still be set at the end. */ |
| 542 | struct built_in_command { |
| 543 | const char *cmd; |
| 544 | int (*function)(char **argv); |
| 545 | #if ENABLE_HUSH_HELP |
| 546 | const char *descr; |
| 547 | #define BLTIN(cmd, func, help) { cmd, func, help } |
| 548 | #else |
| 549 | #define BLTIN(cmd, func, help) { cmd, func } |
| 550 | #endif |
| 551 | }; |
| 552 | |
| 553 | /* For now, echo and test are unconditionally enabled. |
| 554 | * Maybe make it configurable? */ |
| 555 | static const struct built_in_command bltins[] = { |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 556 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 557 | BLTIN(":" , builtin_true , "No-op"), |
| 558 | BLTIN("[" , builtin_test , "Test condition"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 559 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 560 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 561 | #endif |
| 562 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 563 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 564 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 565 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 566 | #if ENABLE_HUSH_LOOPS |
| 567 | BLTIN("continue", builtin_continue, "Start new loop iteration"), |
| 568 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 569 | BLTIN("echo" , builtin_echo , "Write to stdout"), |
| 570 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 571 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 572 | BLTIN("exit" , builtin_exit , "Exit"), |
| 573 | BLTIN("export" , builtin_export , "Set environment variable"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 574 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 575 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 576 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 577 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 578 | BLTIN("help" , builtin_help , "List shell built-in commands"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 579 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 580 | #if ENABLE_HUSH_JOB |
| 581 | BLTIN("jobs" , builtin_jobs , "List active jobs"), |
| 582 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 583 | #if HUSH_DEBUG |
| 584 | BLTIN("memleak" , builtin_memleak , "Debug tool"), |
| 585 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 586 | BLTIN("pwd" , builtin_pwd , "Print current directory"), |
| 587 | BLTIN("read" , builtin_read , "Input environment variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 588 | #if ENABLE_HUSH_FUNCTIONS |
| 589 | BLTIN("return" , builtin_return , "Return from a function"), |
| 590 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 591 | BLTIN("set" , builtin_set , "Set/unset shell local variables"), |
| 592 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
| 593 | BLTIN("test" , builtin_test , "Test condition"), |
| 594 | BLTIN("trap" , builtin_trap , "Trap signals"), |
| 595 | // BLTIN("ulimit" , builtin_return , "Control resource limits"), |
| 596 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 597 | BLTIN("unset" , builtin_unset , "Unset environment variable"), |
| 598 | BLTIN("wait" , builtin_wait , "Wait for process"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 599 | }; |
| 600 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 601 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 602 | /* Debug printouts. |
| 603 | */ |
| 604 | #if HUSH_DEBUG |
| 605 | /* prevent disasters with G.debug_indent < 0 */ |
| 606 | # define indent() fprintf(stderr, "%*s", (G.debug_indent * 2) & 0xff, "") |
| 607 | # define debug_enter() (G.debug_indent++) |
| 608 | # define debug_leave() (G.debug_indent--) |
| 609 | #else |
| 610 | # define indent() ((void)0) |
| 611 | # define debug_enter() ((void)0) |
| 612 | # define debug_leave() ((void)0) |
| 613 | #endif |
| 614 | |
| 615 | #ifndef debug_printf |
| 616 | # define debug_printf(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 617 | #endif |
| 618 | |
| 619 | #ifndef debug_printf_parse |
| 620 | # define debug_printf_parse(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 621 | #endif |
| 622 | |
| 623 | #ifndef debug_printf_exec |
| 624 | #define debug_printf_exec(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 625 | #endif |
| 626 | |
| 627 | #ifndef debug_printf_env |
| 628 | # define debug_printf_env(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 629 | #endif |
| 630 | |
| 631 | #ifndef debug_printf_jobs |
| 632 | # define debug_printf_jobs(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 633 | # define DEBUG_JOBS 1 |
| 634 | #else |
| 635 | # define DEBUG_JOBS 0 |
| 636 | #endif |
| 637 | |
| 638 | #ifndef debug_printf_expand |
| 639 | # define debug_printf_expand(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 640 | # define DEBUG_EXPAND 1 |
| 641 | #else |
| 642 | # define DEBUG_EXPAND 0 |
| 643 | #endif |
| 644 | |
| 645 | #ifndef debug_printf_glob |
| 646 | # define debug_printf_glob(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 647 | # define DEBUG_GLOB 1 |
| 648 | #else |
| 649 | # define DEBUG_GLOB 0 |
| 650 | #endif |
| 651 | |
| 652 | #ifndef debug_printf_list |
| 653 | # define debug_printf_list(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 654 | #endif |
| 655 | |
| 656 | #ifndef debug_printf_subst |
| 657 | # define debug_printf_subst(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 658 | #endif |
| 659 | |
| 660 | #ifndef debug_printf_clean |
| 661 | # define debug_printf_clean(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 662 | # define DEBUG_CLEAN 1 |
| 663 | #else |
| 664 | # define DEBUG_CLEAN 0 |
| 665 | #endif |
| 666 | |
| 667 | #if DEBUG_EXPAND |
| 668 | static void debug_print_strings(const char *prefix, char **vv) |
| 669 | { |
| 670 | indent(); |
| 671 | fprintf(stderr, "%s:\n", prefix); |
| 672 | while (*vv) |
| 673 | fprintf(stderr, " '%s'\n", *vv++); |
| 674 | } |
| 675 | #else |
| 676 | #define debug_print_strings(prefix, vv) ((void)0) |
| 677 | #endif |
| 678 | |
| 679 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 680 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 681 | */ |
| 682 | #if LEAK_HUNTING |
| 683 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 684 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 685 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 686 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 687 | return ptr; |
| 688 | } |
| 689 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 690 | { |
| 691 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 692 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 693 | return ptr; |
| 694 | } |
| 695 | static char *xxstrdup(int lineno, const char *str) |
| 696 | { |
| 697 | char *ptr = xstrdup(str); |
| 698 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 699 | return ptr; |
| 700 | } |
| 701 | static void xxfree(void *ptr) |
| 702 | { |
| 703 | fdprintf(2, "free %p\n", ptr); |
| 704 | free(ptr); |
| 705 | } |
| 706 | #define xmalloc(s) xxmalloc(__LINE__, s) |
| 707 | #define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 708 | #define xstrdup(s) xxstrdup(__LINE__, s) |
| 709 | #define free(p) xxfree(p) |
| 710 | #endif |
| 711 | |
| 712 | |
| 713 | /* Syntax and runtime errors. They always abort scripts. |
| 714 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 715 | * and return to the prompt. |
| 716 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 717 | */ |
| 718 | #if HUSH_DEBUG < 2 |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 719 | # define die_if_script(lineno, fmt...) die_if_script(fmt) |
| 720 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 721 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 722 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 723 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 724 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 725 | #endif |
| 726 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 727 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 728 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 729 | va_list p; |
| 730 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 731 | #if HUSH_DEBUG >= 2 |
| 732 | bb_error_msg("hush.c:%u", lineno); |
| 733 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 734 | va_start(p, fmt); |
| 735 | bb_verror_msg(fmt, p, NULL); |
| 736 | va_end(p); |
| 737 | if (!G_interactive_fd) |
| 738 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 739 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 740 | |
| 741 | static void syntax_error(unsigned lineno, const char *msg) |
| 742 | { |
| 743 | if (msg) |
| 744 | die_if_script(lineno, "syntax error: %s", msg); |
| 745 | else |
| 746 | die_if_script(lineno, "syntax error", NULL); |
| 747 | } |
| 748 | |
| 749 | static void syntax_error_at(unsigned lineno, const char *msg) |
| 750 | { |
| 751 | die_if_script(lineno, "syntax error at '%s'", msg); |
| 752 | } |
| 753 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 754 | /* It so happens that all such cases are totally fatal |
| 755 | * even if shell is interactive: EOF while looking for closing |
| 756 | * delimiter. There is nowhere to read stuff from after that, |
| 757 | * it's EOF! The only choice is to terminate. |
| 758 | */ |
| 759 | static void syntax_error_unterm_ch(unsigned lineno, char ch) NORETURN; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 760 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 761 | { |
| 762 | char msg[2]; |
| 763 | msg[0] = ch; |
| 764 | msg[1] = '\0'; |
| 765 | die_if_script(lineno, "syntax error: unterminated %s", msg); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 766 | xfunc_die(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 769 | static void syntax_error_unterm_str(unsigned lineno, const char *s) |
| 770 | { |
| 771 | die_if_script(lineno, "syntax error: unterminated %s", s); |
| 772 | } |
| 773 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 774 | static void syntax_error_unexpected_ch(unsigned lineno, char ch) |
| 775 | { |
| 776 | char msg[2]; |
| 777 | msg[0] = ch; |
| 778 | msg[1] = '\0'; |
| 779 | die_if_script(lineno, "syntax error: unexpected %s", msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 782 | #if HUSH_DEBUG < 2 |
| 783 | # undef die_if_script |
| 784 | # undef syntax_error |
| 785 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 786 | # undef syntax_error_unterm_ch |
| 787 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 788 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 789 | #else |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 790 | # define die_if_script(fmt...) die_if_script(__LINE__, fmt) |
| 791 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 792 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 793 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 794 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 795 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 796 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 797 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 798 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 799 | /* Utility functions |
| 800 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 801 | static int glob_needed(const char *s) |
| 802 | { |
| 803 | while (*s) { |
| 804 | if (*s == '\\') |
| 805 | s++; |
| 806 | if (*s == '*' || *s == '[' || *s == '?') |
| 807 | return 1; |
| 808 | s++; |
| 809 | } |
| 810 | return 0; |
| 811 | } |
| 812 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 813 | static int is_well_formed_var_name(const char *s, char terminator) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 814 | { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 815 | if (!s || !(isalpha(*s) || *s == '_')) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 816 | return 0; |
| 817 | s++; |
| 818 | while (isalnum(*s) || *s == '_') |
| 819 | s++; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 820 | return *s == terminator; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 823 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 824 | static char *unbackslash(char *src) |
| 825 | { |
| 826 | char *dst = src; |
| 827 | while (1) { |
| 828 | if (*src == '\\') |
| 829 | src++; |
| 830 | if ((*dst++ = *src++) == '\0') |
| 831 | break; |
| 832 | } |
| 833 | return dst; |
| 834 | } |
| 835 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 836 | 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] | 837 | { |
| 838 | int i; |
| 839 | unsigned count1; |
| 840 | unsigned count2; |
| 841 | char **v; |
| 842 | |
| 843 | v = strings; |
| 844 | count1 = 0; |
| 845 | if (v) { |
| 846 | while (*v) { |
| 847 | count1++; |
| 848 | v++; |
| 849 | } |
| 850 | } |
| 851 | count2 = 0; |
| 852 | v = add; |
| 853 | while (*v) { |
| 854 | count2++; |
| 855 | v++; |
| 856 | } |
| 857 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 858 | v[count1 + count2] = NULL; |
| 859 | i = count2; |
| 860 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 861 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 862 | return v; |
| 863 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 864 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 865 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 866 | { |
| 867 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 868 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 869 | return ptr; |
| 870 | } |
| 871 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 872 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 873 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 874 | |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 875 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 876 | { |
| 877 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 878 | v[0] = add; |
| 879 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 880 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 881 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 882 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 883 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 884 | { |
| 885 | char **ptr = add_string_to_strings(strings, add); |
| 886 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 887 | return ptr; |
| 888 | } |
| 889 | #define add_string_to_strings(strings, add) \ |
| 890 | xx_add_string_to_strings(__LINE__, strings, add) |
| 891 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 892 | |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 893 | static void putenv_all(char **strings) |
| 894 | { |
| 895 | if (!strings) |
| 896 | return; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 897 | while (*strings) { |
| 898 | debug_printf_env("putenv '%s'\n", *strings); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 899 | putenv(*strings++); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 900 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | static char **putenv_all_and_save_old(char **strings) |
| 904 | { |
| 905 | char **old = NULL; |
| 906 | char **s = strings; |
| 907 | |
| 908 | if (!strings) |
| 909 | return old; |
| 910 | while (*strings) { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 911 | char *v, *eq; |
| 912 | |
| 913 | eq = strchr(*strings, '='); |
| 914 | if (eq) { |
| 915 | *eq = '\0'; |
| 916 | v = getenv(*strings); |
| 917 | *eq = '='; |
| 918 | if (v) { |
| 919 | /* v points to VAL in VAR=VAL, go back to VAR */ |
| 920 | v -= (eq - *strings) + 1; |
| 921 | old = add_string_to_strings(old, v); |
| 922 | } |
| 923 | } |
| 924 | strings++; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 925 | } |
| 926 | putenv_all(s); |
| 927 | return old; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 930 | static void free_strings_and_unsetenv(char **strings, int unset) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 931 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 932 | char **v; |
| 933 | |
| 934 | if (!strings) |
| 935 | return; |
| 936 | |
| 937 | v = strings; |
| 938 | while (*v) { |
| 939 | if (unset) { |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 940 | debug_printf_env("unsetenv '%s'\n", *v); |
| 941 | bb_unsetenv(*v); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 942 | } |
| 943 | free(*v++); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 944 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 945 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 948 | static void free_strings(char **strings) |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 949 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 950 | free_strings_and_unsetenv(strings, 0); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 951 | } |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 952 | |
| 953 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 954 | /* Helpers for setting new $n and restoring them back |
| 955 | */ |
| 956 | typedef struct save_arg_t { |
| 957 | char *sv_argv0; |
| 958 | char **sv_g_argv; |
| 959 | int sv_g_argc; |
| 960 | smallint sv_g_malloced; |
| 961 | } save_arg_t; |
| 962 | |
| 963 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 964 | { |
| 965 | int n; |
| 966 | |
| 967 | sv->sv_argv0 = argv[0]; |
| 968 | sv->sv_g_argv = G.global_argv; |
| 969 | sv->sv_g_argc = G.global_argc; |
| 970 | sv->sv_g_malloced = G.global_args_malloced; |
| 971 | |
| 972 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 973 | G.global_argv = argv; |
| 974 | G.global_args_malloced = 0; |
| 975 | |
| 976 | n = 1; |
| 977 | while (*++argv) |
| 978 | n++; |
| 979 | G.global_argc = n; |
| 980 | } |
| 981 | |
| 982 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 983 | { |
| 984 | char **pp; |
| 985 | |
| 986 | if (G.global_args_malloced) { |
| 987 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 988 | pp = G.global_argv; |
| 989 | while (*++pp) /* note: does not free $0 */ |
| 990 | free(*pp); |
| 991 | free(G.global_argv); |
| 992 | } |
| 993 | argv[0] = sv->sv_argv0; |
| 994 | G.global_argv = sv->sv_g_argv; |
| 995 | G.global_argc = sv->sv_g_argc; |
| 996 | G.global_args_malloced = sv->sv_g_malloced; |
| 997 | } |
| 998 | |
| 999 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1000 | /* Basic theory of signal handling in shell |
| 1001 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1002 | * This does not describe what hush does, rather, it is current understanding |
| 1003 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1004 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1005 | * |
| 1006 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1007 | * is finished or backgrounded. It is the same in interactive and |
| 1008 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1009 | * 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] | 1010 | * ^C or ^Z from keyboard seem to execute "at once" because it usually |
| 1011 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1012 | * pipe. |
| 1013 | * |
| 1014 | * Wait builtin in interruptible by signals for which user trap is set |
| 1015 | * or by SIGINT in interactive shell. |
| 1016 | * |
| 1017 | * Trap handlers will execute even within trap handlers. (right?) |
| 1018 | * |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1019 | * User trap handlers are forgotten when subshell ("(cmd)") is entered. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1020 | * |
| 1021 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1022 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1023 | * |
| 1024 | * Commands run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1025 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1026 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1027 | * Ordinary commands have signals set to SIG_IGN/DFL set as inherited |
| 1028 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1029 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1030 | * Siganls which differ from SIG_DFL action |
| 1031 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1032 | * |
| 1033 | * SIGQUIT: ignore |
| 1034 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1035 | * SIGHUP (interactive): |
| 1036 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1037 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1038 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1039 | * that all pipe members are stopped. Try this in bash: |
| 1040 | * while :; do :; done - ^Z does not background it |
| 1041 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1042 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1043 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1044 | * to interactive shell while shell is waiting for a pipe, |
| 1045 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1046 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1047 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1048 | * Example 2: this does not wait and does not execute ls: |
| 1049 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1050 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1051 | * "sleep 5; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1052 | * |
| 1053 | * (What happens to signals which are IGN on shell start?) |
| 1054 | * (What happens with signal mask on shell start?) |
| 1055 | * |
| 1056 | * Implementation in hush |
| 1057 | * ====================== |
| 1058 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1059 | * We block all signals which we don't want to take action immediately, |
| 1060 | * i.e. we block all signals which need to have special handling as described |
| 1061 | * above, and all signals which have traps set. |
| 1062 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1063 | * and act on them. |
| 1064 | * |
| 1065 | * unsigned non_DFL_mask: a mask of such "special" signals |
| 1066 | * sigset_t blocked_set: current blocked signal set |
| 1067 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1068 | * "trap - SIGxxx": |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1069 | * 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] | 1070 | * "trap 'cmd' SIGxxx": |
| 1071 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1072 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1073 | * nothing for {} child shell (say, "true | { true; true; } | true") |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1074 | * unset all traps if () shell. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1075 | * after [v]fork, if we plan to exec: |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1076 | * POSIX says pending signal mask is cleared in child - no need to clear it. |
| 1077 | * 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] | 1078 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1079 | * Note: as a result, we do not use signal handlers much. The only uses |
| 1080 | * are to count SIGCHLDs [disabled - bug somewhere, + bloat] |
| 1081 | * and to restore tty pgrp on signal-induced exit. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1082 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1083 | enum { |
| 1084 | SPECIAL_INTERACTIVE_SIGS = 0 |
| 1085 | #if ENABLE_HUSH_JOB |
| 1086 | | (1 << SIGTTIN) | (1 << SIGTTOU) | (1 << SIGTSTP) |
| 1087 | #endif |
| 1088 | | (1 << SIGTERM) |
| 1089 | //TODO | (1 << SIGHUP) |
| 1090 | | (1 << SIGINT) |
| 1091 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1092 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1093 | //static void SIGCHLD_handler(int sig UNUSED_PARAM) |
| 1094 | //{ |
| 1095 | // G.count_SIGCHLD++; |
| 1096 | //} |
| 1097 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1098 | static int check_and_run_traps(int sig) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1099 | { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1100 | static const struct timespec zero_timespec = { 0, 0 }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1101 | smalluint save_rcode; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1102 | int last_sig = 0; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1103 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1104 | if (sig) |
| 1105 | goto jump_in; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1106 | while (1) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1107 | sig = sigtimedwait(&G.blocked_set, NULL, &zero_timespec); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1108 | if (sig <= 0) |
| 1109 | break; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1110 | jump_in: |
| 1111 | last_sig = sig; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1112 | if (G.traps && G.traps[sig]) { |
| 1113 | if (G.traps[sig][0]) { |
| 1114 | /* We have user-defined handler */ |
| 1115 | char *argv[] = { NULL, xstrdup(G.traps[sig]), NULL }; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 1116 | save_rcode = G.last_exitcode; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1117 | builtin_eval(argv); |
| 1118 | free(argv[1]); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 1119 | G.last_exitcode = save_rcode; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1120 | } /* else: "" trap, ignoring signal */ |
| 1121 | continue; |
| 1122 | } |
| 1123 | /* not a trap: special action */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1124 | switch (sig) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1125 | // case SIGCHLD: |
| 1126 | // G.count_SIGCHLD++; |
| 1127 | // break; |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1128 | case SIGINT: |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1129 | //TODO: add putchar('\n') also when we detect that child was killed (sleep 5 + ^C) |
| 1130 | /* Builtin was ^C'ed, make it look prettier: */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1131 | bb_putchar('\n'); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1132 | G.flag_SIGINT = 1; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1133 | break; |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1134 | //TODO |
| 1135 | // case SIGHUP: ... |
| 1136 | // break; |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 1137 | default: /* ignored: */ |
| 1138 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1139 | break; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1140 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1141 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1142 | return last_sig; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1145 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1146 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1147 | /* After [v]fork, in child: do not restore tty pgrp on xfunc death */ |
| 1148 | #define disable_restore_tty_pgrp_on_exit() (die_sleep = 0) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1149 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1150 | #define enable_restore_tty_pgrp_on_exit() (die_sleep = -1) |
| 1151 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1152 | /* Restores tty foreground process group, and exits. |
| 1153 | * May be called as signal handler for fatal signal |
| 1154 | * (will faithfully resend signal to itself, producing correct exit state) |
| 1155 | * or called directly with -EXITCODE. |
| 1156 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1157 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1158 | static void sigexit(int sig) |
| 1159 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1160 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 1161 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1162 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1163 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1164 | * tty pgrp then, only top-level shell process does that */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 1165 | if (G_interactive_fd && getpid() == G.root_pid) |
| 1166 | tcsetpgrp(G_interactive_fd, G.saved_tty_pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1167 | |
| 1168 | /* Not a signal, just exit */ |
| 1169 | if (sig <= 0) |
| 1170 | _exit(- sig); |
| 1171 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1172 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1173 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1174 | #else |
| 1175 | |
| 1176 | #define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1177 | #define enable_restore_tty_pgrp_on_exit() ((void)0) |
| 1178 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1179 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1180 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1181 | /* Restores tty foreground process group, and exits. */ |
| 1182 | static void hush_exit(int exitcode) NORETURN; |
| 1183 | static void hush_exit(int exitcode) |
| 1184 | { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1185 | if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) { |
| 1186 | /* Prevent recursion: |
| 1187 | * trap "echo Hi; exit" EXIT; exit |
| 1188 | */ |
| 1189 | char *argv[] = { NULL, G.traps[0], NULL }; |
| 1190 | G.traps[0] = NULL; |
| 1191 | G.exiting = 1; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1192 | builtin_eval(argv); |
| 1193 | free(argv[1]); |
| 1194 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1195 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1196 | #if ENABLE_HUSH_JOB |
| 1197 | fflush(NULL); /* flush all streams */ |
| 1198 | sigexit(- (exitcode & 0xff)); |
| 1199 | #else |
| 1200 | exit(exitcode); |
| 1201 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1204 | |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1205 | static const char *set_cwd(void) |
| 1206 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1207 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1208 | * we must not try to free(bb_msg_unknown) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1209 | if (G.cwd == bb_msg_unknown) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1210 | G.cwd = NULL; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1211 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1212 | if (!G.cwd) |
| 1213 | G.cwd = bb_msg_unknown; |
| 1214 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1217 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1218 | /* Get/check local shell variables */ |
| 1219 | static struct variable *get_local_var(const char *name) |
| 1220 | { |
| 1221 | struct variable *cur; |
| 1222 | int len; |
| 1223 | |
| 1224 | if (!name) |
| 1225 | return NULL; |
| 1226 | len = strlen(name); |
| 1227 | for (cur = G.top_var; cur; cur = cur->next) { |
| 1228 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
| 1229 | return cur; |
| 1230 | } |
| 1231 | return NULL; |
| 1232 | } |
| 1233 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 1234 | static const char *get_local_var_value(const char *src) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1235 | { |
| 1236 | struct variable *var = get_local_var(src); |
| 1237 | if (var) |
| 1238 | return strchr(var->varstr, '=') + 1; |
| 1239 | return NULL; |
| 1240 | } |
| 1241 | |
| 1242 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1243 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1244 | * flg_export: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1245 | * 0: do not export |
| 1246 | * 1: export |
| 1247 | * -1: if NAME is set, leave export status alone |
| 1248 | * if NAME is not set, do not export |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1249 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1250 | */ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1251 | #if BB_MMU |
| 1252 | #define set_local_var(str, flg_export, flg_read_only) \ |
| 1253 | set_local_var(str, flg_export) |
| 1254 | #endif |
| 1255 | static int set_local_var(char *str, int flg_export, int flg_read_only) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1256 | { |
| 1257 | struct variable *cur; |
| 1258 | char *value; |
| 1259 | int name_len; |
| 1260 | |
| 1261 | value = strchr(str, '='); |
| 1262 | if (!value) { /* not expected to ever happen? */ |
| 1263 | free(str); |
| 1264 | return -1; |
| 1265 | } |
| 1266 | |
| 1267 | name_len = value - str + 1; /* including '=' */ |
| 1268 | cur = G.top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */ |
| 1269 | while (1) { |
| 1270 | if (strncmp(cur->varstr, str, name_len) != 0) { |
| 1271 | if (!cur->next) { |
| 1272 | /* Bail out. Note that now cur points |
| 1273 | * to last var in linked list */ |
| 1274 | break; |
| 1275 | } |
| 1276 | cur = cur->next; |
| 1277 | continue; |
| 1278 | } |
| 1279 | /* We found an existing var with this name */ |
| 1280 | *value = '\0'; |
| 1281 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1282 | #if !BB_MMU |
| 1283 | if (!flg_read_only) |
| 1284 | #endif |
| 1285 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1286 | free(str); |
| 1287 | return -1; |
| 1288 | } |
| 1289 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1290 | unsetenv(str); /* just in case */ |
| 1291 | *value = '='; |
| 1292 | if (strcmp(cur->varstr, str) == 0) { |
| 1293 | free_and_exp: |
| 1294 | free(str); |
| 1295 | goto exp; |
| 1296 | } |
| 1297 | if (cur->max_len >= strlen(str)) { |
| 1298 | /* This one is from startup env, reuse space */ |
| 1299 | strcpy(cur->varstr, str); |
| 1300 | goto free_and_exp; |
| 1301 | } |
| 1302 | /* max_len == 0 signifies "malloced" var, which we can |
| 1303 | * (and has to) free */ |
| 1304 | if (!cur->max_len) |
| 1305 | free(cur->varstr); |
| 1306 | cur->max_len = 0; |
| 1307 | goto set_str_and_exp; |
| 1308 | } |
| 1309 | |
| 1310 | /* Not found - create next variable struct */ |
| 1311 | cur->next = xzalloc(sizeof(*cur)); |
| 1312 | cur = cur->next; |
| 1313 | |
| 1314 | set_str_and_exp: |
| 1315 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1316 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1317 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1318 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1319 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1320 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1321 | cur->flg_export = 1; |
| 1322 | if (cur->flg_export) { |
| 1323 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1324 | return putenv(cur->varstr); |
| 1325 | } |
| 1326 | return 0; |
| 1327 | } |
| 1328 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1329 | static int unset_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1330 | { |
| 1331 | struct variable *cur; |
| 1332 | struct variable *prev = prev; /* for gcc */ |
| 1333 | int name_len; |
| 1334 | |
| 1335 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1336 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1337 | name_len = strlen(name); |
| 1338 | cur = G.top_var; |
| 1339 | while (cur) { |
| 1340 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1341 | if (cur->flg_read_only) { |
| 1342 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1343 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1344 | } |
| 1345 | /* prev is ok to use here because 1st variable, HUSH_VERSION, |
| 1346 | * is ro, and we cannot reach this code on the 1st pass */ |
| 1347 | prev->next = cur->next; |
| 1348 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1349 | bb_unsetenv(cur->varstr); |
| 1350 | if (!cur->max_len) |
| 1351 | free(cur->varstr); |
| 1352 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1353 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1354 | } |
| 1355 | prev = cur; |
| 1356 | cur = cur->next; |
| 1357 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1358 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1361 | #if ENABLE_SH_MATH_SUPPORT |
| 1362 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1363 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 1364 | static char *endofname(const char *name) |
| 1365 | { |
| 1366 | char *p; |
| 1367 | |
| 1368 | p = (char *) name; |
| 1369 | if (!is_name(*p)) |
| 1370 | return p; |
| 1371 | while (*++p) { |
| 1372 | if (!is_in_name(*p)) |
| 1373 | break; |
| 1374 | } |
| 1375 | return p; |
| 1376 | } |
| 1377 | |
| 1378 | static void arith_set_local_var(const char *name, const char *val, int flags) |
| 1379 | { |
| 1380 | /* arith code doesnt malloc space, so do it for it */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1381 | char *var = xasprintf("%s=%s", name, val); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1382 | set_local_var(var, flags, 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1383 | } |
| 1384 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1385 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1386 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1387 | /* |
| 1388 | * in_str support |
| 1389 | */ |
| 1390 | static int static_get(struct in_str *i) |
| 1391 | { |
| 1392 | int ch = *i->p++; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1393 | if (ch != '\0') |
| 1394 | return ch; |
| 1395 | i->p--; |
| 1396 | return EOF; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | static int static_peek(struct in_str *i) |
| 1400 | { |
| 1401 | return *i->p; |
| 1402 | } |
| 1403 | |
| 1404 | #if ENABLE_HUSH_INTERACTIVE |
| 1405 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1406 | static void cmdedit_set_initial_prompt(void) |
| 1407 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1408 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1409 | G.PS1 = getenv("PS1"); |
| 1410 | if (G.PS1 == NULL) |
| 1411 | G.PS1 = "\\w \\$ "; |
| 1412 | } else |
| 1413 | G.PS1 = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1414 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1415 | |
| 1416 | static const char* setup_prompt_string(int promptmode) |
| 1417 | { |
| 1418 | const char *prompt_str; |
| 1419 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1420 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1421 | /* Set up the prompt */ |
| 1422 | if (promptmode == 0) { /* PS1 */ |
| 1423 | free((char*)G.PS1); |
| 1424 | G.PS1 = xasprintf("%s %c ", G.cwd, (geteuid() != 0) ? '$' : '#'); |
| 1425 | prompt_str = G.PS1; |
| 1426 | } else |
| 1427 | prompt_str = G.PS2; |
| 1428 | } else |
| 1429 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1430 | debug_printf("result '%s'\n", prompt_str); |
| 1431 | return prompt_str; |
| 1432 | } |
| 1433 | |
| 1434 | static void get_user_input(struct in_str *i) |
| 1435 | { |
| 1436 | int r; |
| 1437 | const char *prompt_str; |
| 1438 | |
| 1439 | prompt_str = setup_prompt_string(i->promptmode); |
| 1440 | #if ENABLE_FEATURE_EDITING |
| 1441 | /* Enable command line editing only while a command line |
| 1442 | * is actually being read */ |
| 1443 | do { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1444 | G.flag_SIGINT = 0; |
| 1445 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 1446 | * only after <Enter>. (^C will work) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1447 | 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] | 1448 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1449 | check_and_run_traps(0); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1450 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1451 | i->eof_flag = (r < 0); |
| 1452 | if (i->eof_flag) { /* EOF/error detected */ |
| 1453 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1454 | G.user_input_buf[1] = '\0'; |
| 1455 | } |
| 1456 | #else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1457 | do { |
| 1458 | G.flag_SIGINT = 0; |
| 1459 | fputs(prompt_str, stdout); |
| 1460 | fflush(stdout); |
| 1461 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1462 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1463 | //do we need check_and_run_traps(0)? (maybe only if stdin) |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1464 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1465 | i->eof_flag = (r == EOF); |
| 1466 | #endif |
| 1467 | i->p = G.user_input_buf; |
| 1468 | } |
| 1469 | |
| 1470 | #endif /* INTERACTIVE */ |
| 1471 | |
| 1472 | /* This is the magic location that prints prompts |
| 1473 | * and gets data back from the user */ |
| 1474 | static int file_get(struct in_str *i) |
| 1475 | { |
| 1476 | int ch; |
| 1477 | |
| 1478 | /* If there is data waiting, eat it up */ |
| 1479 | if (i->p && *i->p) { |
| 1480 | #if ENABLE_HUSH_INTERACTIVE |
| 1481 | take_cached: |
| 1482 | #endif |
| 1483 | ch = *i->p++; |
| 1484 | if (i->eof_flag && !*i->p) |
| 1485 | ch = EOF; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1486 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1487 | } else { |
| 1488 | /* need to double check i->file because we might be doing something |
| 1489 | * more complicated by now, like sourcing or substituting. */ |
| 1490 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 1491 | if (G_interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1492 | do { |
| 1493 | get_user_input(i); |
| 1494 | } while (!*i->p); /* need non-empty line */ |
| 1495 | i->promptmode = 1; /* PS2 */ |
| 1496 | i->promptme = 0; |
| 1497 | goto take_cached; |
| 1498 | } |
| 1499 | #endif |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1500 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1501 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1502 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1503 | #if ENABLE_HUSH_INTERACTIVE |
| 1504 | if (ch == '\n') |
| 1505 | i->promptme = 1; |
| 1506 | #endif |
| 1507 | return ch; |
| 1508 | } |
| 1509 | |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1510 | /* All callers guarantee this routine will never |
| 1511 | * be used right after a newline, so prompting is not needed. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1512 | */ |
| 1513 | static int file_peek(struct in_str *i) |
| 1514 | { |
| 1515 | int ch; |
| 1516 | if (i->p && *i->p) { |
| 1517 | if (i->eof_flag && !i->p[1]) |
| 1518 | return EOF; |
| 1519 | return *i->p; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1520 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1521 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1522 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1523 | i->eof_flag = (ch == EOF); |
| 1524 | i->peek_buf[0] = ch; |
| 1525 | i->peek_buf[1] = '\0'; |
| 1526 | i->p = i->peek_buf; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1527 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1528 | return ch; |
| 1529 | } |
| 1530 | |
| 1531 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1532 | { |
| 1533 | i->peek = file_peek; |
| 1534 | i->get = file_get; |
| 1535 | #if ENABLE_HUSH_INTERACTIVE |
| 1536 | i->promptme = 1; |
| 1537 | i->promptmode = 0; /* PS1 */ |
| 1538 | #endif |
| 1539 | i->file = f; |
| 1540 | i->p = NULL; |
| 1541 | } |
| 1542 | |
| 1543 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1544 | { |
| 1545 | i->peek = static_peek; |
| 1546 | i->get = static_get; |
| 1547 | #if ENABLE_HUSH_INTERACTIVE |
| 1548 | i->promptme = 1; |
| 1549 | i->promptmode = 0; /* PS1 */ |
| 1550 | #endif |
| 1551 | i->p = s; |
| 1552 | i->eof_flag = 0; |
| 1553 | } |
| 1554 | |
| 1555 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1556 | /* |
| 1557 | * o_string support |
| 1558 | */ |
| 1559 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1560 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1561 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1562 | { |
| 1563 | o->length = 0; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 1564 | o->o_quoted = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1565 | if (o->data) |
| 1566 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1569 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1570 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1571 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1572 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1575 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 1576 | { |
| 1577 | free(o->data); |
| 1578 | } |
| 1579 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1580 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1581 | { |
| 1582 | if (o->length + len > o->maxlen) { |
| 1583 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 1584 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 1585 | } |
| 1586 | } |
| 1587 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1588 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1589 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1590 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 1591 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1592 | o->data[o->length] = ch; |
| 1593 | o->length++; |
| 1594 | o->data[o->length] = '\0'; |
| 1595 | } |
| 1596 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1597 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1598 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1599 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1600 | memcpy(&o->data[o->length], str, len); |
| 1601 | o->length += len; |
| 1602 | o->data[o->length] = '\0'; |
| 1603 | } |
| 1604 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1605 | #if !BB_MMU |
| 1606 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1607 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1608 | o_addblock(o, str, strlen(str)); |
| 1609 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1610 | static void nommu_addchr(o_string *o, int ch) |
| 1611 | { |
| 1612 | if (o) |
| 1613 | o_addchr(o, ch); |
| 1614 | } |
| 1615 | #else |
| 1616 | #define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1617 | #endif |
| 1618 | |
| 1619 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 1620 | { |
| 1621 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1624 | 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] | 1625 | { |
| 1626 | while (len) { |
| 1627 | o_addchr(o, *str); |
| 1628 | if (*str++ == '\\' |
| 1629 | && (*str != '*' && *str != '?' && *str != '[') |
| 1630 | ) { |
| 1631 | o_addchr(o, '\\'); |
| 1632 | } |
| 1633 | len--; |
| 1634 | } |
| 1635 | } |
| 1636 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1637 | /* My analysis of quoting semantics tells me that state information |
| 1638 | * is associated with a destination, not a source. |
| 1639 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1640 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1641 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1642 | int sz = 1; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1643 | char *found = strchr("*?[\\", ch); |
| 1644 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1645 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1646 | o_grow_by(o, sz); |
| 1647 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1648 | o->data[o->length] = '\\'; |
| 1649 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1650 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1651 | o->data[o->length] = ch; |
| 1652 | o->length++; |
| 1653 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1656 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1657 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1658 | int sz = 1; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1659 | if (o->o_escape && strchr("*?[\\", ch)) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1660 | sz++; |
| 1661 | o->data[o->length] = '\\'; |
| 1662 | o->length++; |
| 1663 | } |
| 1664 | o_grow_by(o, sz); |
| 1665 | o->data[o->length] = ch; |
| 1666 | o->length++; |
| 1667 | o->data[o->length] = '\0'; |
| 1668 | } |
| 1669 | |
| 1670 | static void o_addQstr(o_string *o, const char *str, int len) |
| 1671 | { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1672 | if (!o->o_escape) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1673 | o_addblock(o, str, len); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1674 | return; |
| 1675 | } |
| 1676 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1677 | char ch; |
| 1678 | int sz; |
| 1679 | int ordinary_cnt = strcspn(str, "*?[\\"); |
| 1680 | if (ordinary_cnt > len) /* paranoia */ |
| 1681 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1682 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1683 | if (ordinary_cnt == len) |
| 1684 | return; |
| 1685 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1686 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1687 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1688 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1689 | sz = 1; |
| 1690 | if (ch) { /* it is necessarily one of "*?[\\" */ |
| 1691 | sz++; |
| 1692 | o->data[o->length] = '\\'; |
| 1693 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1694 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1695 | o_grow_by(o, sz); |
| 1696 | o->data[o->length] = ch; |
| 1697 | o->length++; |
| 1698 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1699 | } |
| 1700 | } |
| 1701 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1702 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1703 | * 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] | 1704 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1705 | * list[i] contains an INDEX (int!) into this string data. |
| 1706 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1707 | * but list[i]'s need not be modified. |
| 1708 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1709 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1710 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1711 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1712 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1713 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1714 | { |
| 1715 | char **list = (char**)o->data; |
| 1716 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1717 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1718 | |
| 1719 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1720 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1721 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1722 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1723 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1724 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1725 | o->data + (int)list[i] + string_start, |
| 1726 | o->data + (int)list[i] + string_start); |
| 1727 | i++; |
| 1728 | } |
| 1729 | if (n) { |
| 1730 | const char *p = o->data + (int)list[n - 1] + string_start; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1731 | indent(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1732 | 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] | 1733 | } |
| 1734 | } |
| 1735 | #else |
| 1736 | #define debug_print_list(prefix, o, n) ((void)0) |
| 1737 | #endif |
| 1738 | |
| 1739 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1740 | * in list[n] so that it points past last stored byte so far. |
| 1741 | * It returns n+1. */ |
| 1742 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1743 | { |
| 1744 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1745 | int string_start; |
| 1746 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1747 | |
| 1748 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1749 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1750 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1751 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1752 | 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] | 1753 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1754 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1755 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 1756 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1757 | memmove(list + n + 0x10, list + n, string_len); |
| 1758 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1759 | } else { |
| 1760 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 1761 | n, string_len, string_start); |
| 1762 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1763 | } else { |
| 1764 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1765 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1766 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1767 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 1768 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1769 | o->has_empty_slot = 0; |
| 1770 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1771 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1772 | return n + 1; |
| 1773 | } |
| 1774 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1775 | /* "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] | 1776 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1777 | { |
| 1778 | char **list = (char**)o->data; |
| 1779 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1780 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1781 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1784 | /* o_glob performs globbing on last list[], saving each result |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1785 | * as a new list[]. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1786 | static int o_glob(o_string *o, int n) |
| 1787 | { |
| 1788 | glob_t globdata; |
| 1789 | int gr; |
| 1790 | char *pattern; |
| 1791 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1792 | 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] | 1793 | if (!o->data) |
| 1794 | return o_save_ptr_helper(o, n); |
| 1795 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1796 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1797 | if (!glob_needed(pattern)) { |
| 1798 | literal: |
| 1799 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1800 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1801 | return o_save_ptr_helper(o, n); |
| 1802 | } |
| 1803 | |
| 1804 | memset(&globdata, 0, sizeof(globdata)); |
| 1805 | gr = glob(pattern, 0, NULL, &globdata); |
| 1806 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 1807 | if (gr == GLOB_NOSPACE) |
| 1808 | bb_error_msg_and_die("out of memory during glob"); |
| 1809 | if (gr == GLOB_NOMATCH) { |
| 1810 | globfree(&globdata); |
| 1811 | goto literal; |
| 1812 | } |
| 1813 | if (gr != 0) { /* GLOB_ABORTED ? */ |
| 1814 | //TODO: testcase for bad glob pattern behavior |
| 1815 | bb_error_msg("glob(3) error %d on '%s'", gr, pattern); |
| 1816 | } |
| 1817 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 1818 | char **argv = globdata.gl_pathv; |
| 1819 | o->length = pattern - o->data; /* "forget" pattern */ |
| 1820 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1821 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1822 | n = o_save_ptr_helper(o, n); |
| 1823 | argv++; |
| 1824 | if (!*argv) |
| 1825 | break; |
| 1826 | } |
| 1827 | } |
| 1828 | globfree(&globdata); |
| 1829 | if (DEBUG_GLOB) |
| 1830 | debug_print_list("o_glob returning", o, n); |
| 1831 | return n; |
| 1832 | } |
| 1833 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1834 | /* If o->o_glob == 1, glob the string so far remembered. |
| 1835 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1836 | static int o_save_ptr(o_string *o, int n) |
| 1837 | { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 1838 | if (o->o_glob) { /* if globbing is requested */ |
| 1839 | /* If o->has_empty_slot, list[n] was already globbed |
| 1840 | * (if it was requested back then when it was filled) |
| 1841 | * so don't do that again! */ |
| 1842 | if (!o->has_empty_slot) |
| 1843 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 1844 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1845 | return o_save_ptr_helper(o, n); |
| 1846 | } |
| 1847 | |
| 1848 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1849 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1850 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1851 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1852 | int string_start; |
| 1853 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1854 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 1855 | if (DEBUG_EXPAND) |
| 1856 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1857 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1858 | list = (char**)o->data; |
| 1859 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1860 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1861 | while (n) { |
| 1862 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1863 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1864 | } |
| 1865 | return list; |
| 1866 | } |
| 1867 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1868 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1869 | /* Expansion can recurse */ |
| 1870 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 1871 | static int process_command_subs(o_string *dest, const char *s); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1872 | #endif |
| 1873 | static char *expand_string_to_string(const char *str); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1874 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 1875 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1876 | parse_stream_dquoted(dest, input, dquote_end) |
| 1877 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 1878 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1879 | o_string *dest, |
| 1880 | struct in_str *input, |
| 1881 | int dquote_end); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1882 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1883 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 1884 | * all variable references within and returns a pointer to |
| 1885 | * a list of expanded strings, possibly with larger number |
| 1886 | * of strings. (Think VAR="a b"; echo $VAR). |
| 1887 | * This new list is allocated as a single malloc block. |
| 1888 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 1889 | * followed by strings themself. |
| 1890 | * Caller can deallocate entire list by single free(list). */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1891 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1892 | /* Store given string, finalizing the word and starting new one whenever |
| 1893 | * we encounter IFS char(s). This is used for expanding variable values. |
| 1894 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 1895 | 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] | 1896 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1897 | while (1) { |
| 1898 | int word_len = strcspn(str, G.ifs); |
| 1899 | if (word_len) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1900 | if (output->o_escape || !output->o_glob) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1901 | o_addQstr(output, str, word_len); |
| 1902 | else /* protect backslashes against globbing up :) */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1903 | o_addblock_duplicate_backslash(output, str, word_len); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1904 | str += word_len; |
| 1905 | } |
| 1906 | if (!*str) /* EOL - do not finalize word */ |
| 1907 | break; |
| 1908 | o_addchr(output, '\0'); |
| 1909 | debug_print_list("expand_on_ifs", output, n); |
| 1910 | n = o_save_ptr(output, n); |
| 1911 | str += strspn(str, G.ifs); /* skip ifs chars */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1912 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1913 | debug_print_list("expand_on_ifs[1]", output, n); |
| 1914 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 1917 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 1918 | * they are in double quotes, with the exception that they are not :). |
| 1919 | * Just the rules are similar: "expand only $var and `cmd`" |
| 1920 | * |
| 1921 | * Returns malloced string. |
| 1922 | * As an optimization, we return NULL if expansion is not needed. |
| 1923 | */ |
| 1924 | static char *expand_pseudo_dquoted(const char *str) |
| 1925 | { |
| 1926 | char *exp_str; |
| 1927 | struct in_str input; |
| 1928 | o_string dest = NULL_O_STRING; |
| 1929 | |
| 1930 | if (strchr(str, '$') == NULL |
| 1931 | #if ENABLE_HUSH_TICK |
| 1932 | && strchr(str, '`') == NULL |
| 1933 | #endif |
| 1934 | ) { |
| 1935 | return NULL; |
| 1936 | } |
| 1937 | |
| 1938 | /* We need to expand. Example: |
| 1939 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 1940 | */ |
| 1941 | setup_string_in_str(&input, str); |
| 1942 | parse_stream_dquoted(NULL, &dest, &input, EOF); |
| 1943 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
| 1944 | exp_str = expand_string_to_string(dest.data); |
| 1945 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 1946 | o_free_unsafe(&dest); |
| 1947 | return exp_str; |
| 1948 | } |
| 1949 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1950 | /* Expand all variable references in given string, adding words to list[] |
| 1951 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 1952 | * to be filled). This routine is extremely tricky: has to deal with |
| 1953 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 1954 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
| 1955 | 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] | 1956 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1957 | /* or_mask is either 0 (normal case) or 0x80 |
| 1958 | * (expansion of right-hand side of assignment == 1-element expand. |
| 1959 | * It will also do no globbing, and thus we must not backslash-quote!) */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1960 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1961 | char first_ch, ored_ch; |
| 1962 | int i; |
| 1963 | const char *val; |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 1964 | char *dyn_val, *p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1965 | |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 1966 | dyn_val = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1967 | ored_ch = 0; |
| 1968 | |
| 1969 | debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg); |
| 1970 | debug_print_list("expand_vars_to_list", output, n); |
| 1971 | n = o_save_ptr(output, n); |
| 1972 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 1973 | |
| 1974 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 1975 | #if ENABLE_HUSH_TICK |
| 1976 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1977 | #endif |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1978 | #if ENABLE_SH_MATH_SUPPORT |
| 1979 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 1980 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1981 | o_addblock(output, arg, p - arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1982 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 1983 | arg = ++p; |
| 1984 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 1985 | |
| 1986 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
| 1987 | /* "$@" is special. Even if quoted, it can still |
| 1988 | * expand to nothing (not even an empty string) */ |
| 1989 | if ((first_ch & 0x7f) != '@') |
| 1990 | ored_ch |= first_ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1991 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1992 | val = NULL; |
| 1993 | switch (first_ch & 0x7f) { |
| 1994 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 1995 | case '$': /* pid */ |
| 1996 | val = utoa(G.root_pid); |
| 1997 | break; |
| 1998 | case '!': /* bg pid */ |
| 1999 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
| 2000 | break; |
| 2001 | case '?': /* exitcode */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 2002 | val = utoa(G.last_exitcode); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2003 | break; |
| 2004 | case '#': /* argc */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2005 | if (arg[1] != SPECIAL_VAR_SYMBOL) |
| 2006 | /* actually, it's a ${#var} */ |
| 2007 | goto case_default; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2008 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 2009 | break; |
| 2010 | case '*': |
| 2011 | case '@': |
| 2012 | i = 1; |
| 2013 | if (!G.global_argv[i]) |
| 2014 | break; |
| 2015 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
| 2016 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2017 | smallint sv = output->o_escape; |
| 2018 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2019 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2020 | while (G.global_argv[i]) { |
| 2021 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 2022 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 2023 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 2024 | /* this argv[] is not empty and not last: |
| 2025 | * put terminating NUL, start new word */ |
| 2026 | o_addchr(output, '\0'); |
| 2027 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 2028 | n = o_save_ptr(output, n); |
| 2029 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 2030 | } |
| 2031 | } |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2032 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2033 | } else |
| 2034 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
| 2035 | * and in this case should treat it like '$*' - see 'else...' below */ |
| 2036 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
| 2037 | while (1) { |
| 2038 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2039 | if (++i >= G.global_argc) |
| 2040 | break; |
| 2041 | o_addchr(output, '\0'); |
| 2042 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 2043 | n = o_save_ptr(output, n); |
| 2044 | } |
| 2045 | } else { /* quoted $*: add as one word */ |
| 2046 | while (1) { |
| 2047 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2048 | if (!G.global_argv[++i]) |
| 2049 | break; |
| 2050 | if (G.ifs[0]) |
| 2051 | o_addchr(output, G.ifs[0]); |
| 2052 | } |
| 2053 | } |
| 2054 | break; |
| 2055 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 2056 | /* "Empty variable", used to make "" etc to not disappear */ |
| 2057 | arg++; |
| 2058 | ored_ch = 0x80; |
| 2059 | break; |
| 2060 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2061 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2062 | *p = '\0'; |
| 2063 | arg++; |
| 2064 | //TODO: can we just stuff it into "output" directly? |
| 2065 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2066 | process_command_subs(&subst_result, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2067 | debug_printf_subst("SUBST RES '%s'\n", subst_result.data); |
| 2068 | val = subst_result.data; |
| 2069 | goto store_val; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2070 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2071 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2072 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2073 | arith_eval_hooks_t hooks; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2074 | arith_t res; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2075 | int errcode; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2076 | char *exp_str; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2077 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2078 | arg++; /* skip '+' */ |
| 2079 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2080 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2081 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2082 | exp_str = expand_pseudo_dquoted(arg); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 2083 | hooks.lookupvar = get_local_var_value; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2084 | hooks.setvar = arith_set_local_var; |
| 2085 | hooks.endofname = endofname; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2086 | res = arith(exp_str ? exp_str : arg, &errcode, &hooks); |
| 2087 | free(exp_str); |
| 2088 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2089 | if (errcode < 0) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2090 | const char *msg = "error in arithmetic"; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2091 | switch (errcode) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2092 | case -3: |
| 2093 | msg = "exponent less than 0"; |
| 2094 | break; |
| 2095 | case -2: |
| 2096 | msg = "divide by 0"; |
| 2097 | break; |
| 2098 | case -5: |
| 2099 | msg = "expression recursion loop detected"; |
| 2100 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2101 | } |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2102 | die_if_script(msg); |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2103 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2104 | debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2105 | sprintf(arith_buf, arith_t_fmt, res); |
| 2106 | val = arith_buf; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2107 | break; |
| 2108 | } |
| 2109 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2110 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2111 | case_default: { |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2112 | bool exp_len = false; |
| 2113 | bool exp_null = false; |
| 2114 | char *var = arg; |
| 2115 | char exp_save = exp_save; /* for compiler */ |
| 2116 | char exp_op = exp_op; /* for compiler */ |
| 2117 | char *exp_word = exp_word; /* for compiler */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2118 | size_t exp_off = 0; |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2119 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2120 | *p = '\0'; |
| 2121 | arg[0] = first_ch & 0x7f; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2122 | |
| 2123 | /* prepare for expansions */ |
| 2124 | if (var[0] == '#') { |
| 2125 | /* handle length expansion ${#var} */ |
| 2126 | exp_len = true; |
| 2127 | ++var; |
| 2128 | } else { |
| 2129 | /* maybe handle parameter expansion */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2130 | exp_off = strcspn(var, ":-=+?%#"); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2131 | if (!var[exp_off]) |
| 2132 | exp_off = 0; |
| 2133 | if (exp_off) { |
| 2134 | exp_save = var[exp_off]; |
| 2135 | exp_null = exp_save == ':'; |
| 2136 | exp_word = var + exp_off; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2137 | if (exp_null) |
| 2138 | ++exp_word; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2139 | exp_op = *exp_word++; |
| 2140 | var[exp_off] = '\0'; |
| 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | /* lookup the variable in question */ |
| 2145 | if (isdigit(var[0])) { |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 2146 | /* handle_dollar() should have vetted var for us */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2147 | i = xatoi_u(var); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2148 | if (i < G.global_argc) |
| 2149 | val = G.global_argv[i]; |
| 2150 | /* else val remains NULL: $N with too big N */ |
| 2151 | } else |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 2152 | val = get_local_var_value(var); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2153 | |
| 2154 | /* handle any expansions */ |
| 2155 | if (exp_len) { |
| 2156 | debug_printf_expand("expand: length of '%s' = ", val); |
| 2157 | val = utoa(val ? strlen(val) : 0); |
| 2158 | debug_printf_expand("%s\n", val); |
| 2159 | } else if (exp_off) { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2160 | if (exp_op == '%' || exp_op == '#') { |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2161 | if (val) { |
| 2162 | /* we need to do a pattern match */ |
| 2163 | bool zero; |
| 2164 | char *loc; |
| 2165 | scan_t scan = pick_scan(exp_op, *exp_word, &zero); |
| 2166 | if (exp_op == *exp_word) /* ## or %% */ |
| 2167 | ++exp_word; |
| 2168 | val = dyn_val = xstrdup(val); |
| 2169 | loc = scan(dyn_val, exp_word, zero); |
| 2170 | if (zero) |
| 2171 | val = loc; |
| 2172 | else |
| 2173 | *loc = '\0'; |
| 2174 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2175 | } else { |
| 2176 | /* we need to do an expansion */ |
| 2177 | int exp_test = (!val || (exp_null && !val[0])); |
| 2178 | if (exp_op == '+') |
| 2179 | exp_test = !exp_test; |
| 2180 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 2181 | exp_null ? "true" : "false", exp_test); |
| 2182 | if (exp_test) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2183 | if (exp_op == '?') { |
| 2184 | //TODO: how interactive bash aborts expansion mid-command? |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2185 | /* ${var?[error_msg_if_unset]} */ |
| 2186 | /* ${var:?[error_msg_if_unset_or_null]} */ |
| 2187 | /* mimic bash message */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2188 | die_if_script("%s: %s", |
| 2189 | var, |
| 2190 | exp_word[0] ? exp_word : "parameter null or not set" |
| 2191 | ); |
| 2192 | } else { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2193 | val = exp_word; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2194 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2195 | |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2196 | if (exp_op == '=') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2197 | /* ${var=[word]} or ${var:=[word]} */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2198 | if (isdigit(var[0]) || var[0] == '#') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2199 | /* mimic bash message */ |
| 2200 | die_if_script("$%s: cannot assign in this way", var); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2201 | val = NULL; |
| 2202 | } else { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2203 | char *new_var = xasprintf("%s=%s", var, val); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2204 | set_local_var(new_var, -1, 0); |
| 2205 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2206 | } |
| 2207 | } |
| 2208 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2209 | |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2210 | var[exp_off] = exp_save; |
| 2211 | } |
| 2212 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2213 | arg[0] = first_ch; |
| 2214 | #if ENABLE_HUSH_TICK |
| 2215 | store_val: |
| 2216 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2217 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2218 | 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] | 2219 | if (val) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2220 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2221 | smallint sv = output->o_escape; |
| 2222 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2223 | n = expand_on_ifs(output, n, val); |
| 2224 | val = NULL; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2225 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2226 | } |
| 2227 | } else { /* quoted $VAR, val will be appended below */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2228 | 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] | 2229 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2230 | } /* default: */ |
| 2231 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2232 | if (val) { |
| 2233 | o_addQstr(output, val, strlen(val)); |
| 2234 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2235 | free(dyn_val); |
| 2236 | dyn_val = NULL; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2237 | /* Do the check to avoid writing to a const string */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2238 | if (*p != SPECIAL_VAR_SYMBOL) |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2239 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2240 | |
| 2241 | #if ENABLE_HUSH_TICK |
| 2242 | o_free(&subst_result); |
| 2243 | #endif |
| 2244 | arg = ++p; |
| 2245 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 2246 | |
| 2247 | if (arg[0]) { |
| 2248 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 2249 | /* this part is literal, and it was already pre-quoted |
| 2250 | * if needed (much earlier), do not use o_addQstr here! */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2251 | o_addstr_with_NUL(output, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2252 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 2253 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 2254 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 2255 | ) { |
| 2256 | n--; |
| 2257 | /* allow to reuse list[n] later without re-growth */ |
| 2258 | output->has_empty_slot = 1; |
| 2259 | } else { |
| 2260 | o_addchr(output, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2261 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2262 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2265 | static char **expand_variables(char **argv, int or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2266 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2267 | int n; |
| 2268 | char **list; |
| 2269 | char **v; |
| 2270 | o_string output = NULL_O_STRING; |
| 2271 | |
| 2272 | if (or_mask & 0x100) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2273 | output.o_escape = 1; /* protect against globbing for "$var" */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2274 | /* (unquoted $var will temporarily switch it off) */ |
| 2275 | output.o_glob = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2276 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2277 | |
| 2278 | n = 0; |
| 2279 | v = argv; |
| 2280 | while (*v) { |
| 2281 | n = expand_vars_to_list(&output, n, *v, (char)or_mask); |
| 2282 | v++; |
| 2283 | } |
| 2284 | debug_print_list("expand_variables", &output, n); |
| 2285 | |
| 2286 | /* output.data (malloced in one block) gets returned in "list" */ |
| 2287 | list = o_finalize_list(&output, n); |
| 2288 | debug_print_strings("expand_variables[1]", list); |
| 2289 | return list; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2292 | static char **expand_strvec_to_strvec(char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2293 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2294 | return expand_variables(argv, 0x100); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2297 | /* Used for expansion of right hand of assignments */ |
| 2298 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 2299 | * "v=/bin/c*" */ |
| 2300 | static char *expand_string_to_string(const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2301 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2302 | char *argv[2], **list; |
| 2303 | |
| 2304 | argv[0] = (char*)str; |
| 2305 | argv[1] = NULL; |
| 2306 | list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */ |
| 2307 | if (HUSH_DEBUG) |
| 2308 | if (!list[0] || list[1]) |
| 2309 | bb_error_msg_and_die("BUG in varexp2"); |
| 2310 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 2311 | overlapping_strcpy((char*)list, list[0]); |
| 2312 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 2313 | return (char*)list; |
| 2314 | } |
| 2315 | |
| 2316 | /* Used for "eval" builtin */ |
| 2317 | static char* expand_strvec_to_string(char **argv) |
| 2318 | { |
| 2319 | char **list; |
| 2320 | |
| 2321 | list = expand_variables(argv, 0x80); |
| 2322 | /* Convert all NULs to spaces */ |
| 2323 | if (list[0]) { |
| 2324 | int n = 1; |
| 2325 | while (list[n]) { |
| 2326 | if (HUSH_DEBUG) |
| 2327 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 2328 | bb_error_msg_and_die("BUG in varexp3"); |
| 2329 | list[n][-1] = ' '; /* TODO: or to G.ifs[0]? */ |
| 2330 | n++; |
| 2331 | } |
| 2332 | } |
| 2333 | overlapping_strcpy((char*)list, list[0]); |
| 2334 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 2335 | return (char*)list; |
| 2336 | } |
| 2337 | |
| 2338 | static char **expand_assignments(char **argv, int count) |
| 2339 | { |
| 2340 | int i; |
| 2341 | char **p = NULL; |
| 2342 | /* Expand assignments into one string each */ |
| 2343 | for (i = 0; i < count; i++) { |
| 2344 | p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
| 2345 | } |
| 2346 | return p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2349 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2350 | #if BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2351 | /* never called */ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2352 | void re_execute_shell(char ***to_free, const char *s, char *argv0, char **argv); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2353 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2354 | static void reset_traps_to_defaults(void) |
| 2355 | { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2356 | /* This function is always called in a child shell |
| 2357 | * after fork (not vfork, NOMMU doesn't use this function). |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2358 | * Child shells are not interactive. |
| 2359 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 2360 | * Testcase: (while :; do :; done) + ^Z should background. |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2361 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2362 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2363 | unsigned sig; |
| 2364 | unsigned mask; |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2365 | |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2366 | if (!G.traps && !(G.non_DFL_mask & SPECIAL_INTERACTIVE_SIGS)) |
| 2367 | return; |
| 2368 | |
| 2369 | /* Stupid. It can be done with *single* &= op, but we can't use |
| 2370 | * the fact that G.blocked_set is implemented as a bitmask... */ |
| 2371 | mask = (SPECIAL_INTERACTIVE_SIGS >> 1); |
| 2372 | sig = 1; |
| 2373 | while (1) { |
| 2374 | if (mask & 1) |
| 2375 | sigdelset(&G.blocked_set, sig); |
| 2376 | mask >>= 1; |
| 2377 | if (!mask) |
| 2378 | break; |
| 2379 | sig++; |
| 2380 | } |
| 2381 | |
| 2382 | G.non_DFL_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
| 2383 | mask = G.non_DFL_mask; |
| 2384 | if (G.traps) for (sig = 0; sig < NSIG; sig++, mask >>= 1) { |
| 2385 | if (!G.traps[sig]) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2386 | continue; |
| 2387 | free(G.traps[sig]); |
| 2388 | G.traps[sig] = NULL; |
| 2389 | /* There is no signal for 0 (EXIT) */ |
| 2390 | if (sig == 0) |
| 2391 | continue; |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2392 | /* There was a trap handler, we are removing it. |
| 2393 | * But if sig still has non-DFL handling, |
| 2394 | * we should not unblock it. */ |
| 2395 | if (mask & 1) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2396 | continue; |
| 2397 | sigdelset(&G.blocked_set, sig); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2398 | } |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2399 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | #else /* !BB_MMU */ |
| 2403 | |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2404 | static void re_execute_shell(char ***to_free, const char *s, char *g_argv0, char **g_argv) NORETURN; |
| 2405 | static void re_execute_shell(char ***to_free, const char *s, char *g_argv0, char **g_argv) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2406 | { |
| 2407 | char param_buf[sizeof("-$%x:%x:%x:%x") + sizeof(unsigned) * 4]; |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2408 | char *heredoc_argv[4]; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2409 | struct variable *cur; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2410 | #if ENABLE_HUSH_FUNCTIONS |
| 2411 | struct function *funcp; |
| 2412 | #endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2413 | char **argv, **pp; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2414 | unsigned cnt; |
| 2415 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2416 | if (!g_argv0) { /* heredoc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2417 | argv = heredoc_argv; |
| 2418 | argv[0] = (char *) G.argv0_for_re_execing; |
| 2419 | argv[1] = (char *) "-<"; |
| 2420 | argv[2] = (char *) s; |
| 2421 | argv[3] = NULL; |
Denis Vlasenko | f50caac | 2009-04-09 01:40:15 +0000 | [diff] [blame] | 2422 | pp = &argv[3]; /* used as pointer to empty environment */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2423 | goto do_exec; |
| 2424 | } |
| 2425 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2426 | sprintf(param_buf, "-$%x:%x:%x" USE_HUSH_LOOPS(":%x") |
| 2427 | , (unsigned) G.root_pid |
| 2428 | , (unsigned) G.last_bg_pid |
| 2429 | , (unsigned) G.last_exitcode |
| 2430 | USE_HUSH_LOOPS(, G.depth_of_loop) |
| 2431 | ); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2432 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<depth> <vars...> <funcs...> |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2433 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2434 | */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2435 | cnt = 6; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2436 | for (cur = G.top_var; cur; cur = cur->next) { |
| 2437 | if (!cur->flg_export || cur->flg_read_only) |
| 2438 | cnt += 2; |
| 2439 | } |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2440 | #if ENABLE_HUSH_FUNCTIONS |
| 2441 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 2442 | cnt += 3; |
| 2443 | #endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2444 | pp = g_argv; |
| 2445 | while (*pp++) |
| 2446 | cnt++; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2447 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2448 | *pp++ = (char *) G.argv0_for_re_execing; |
| 2449 | *pp++ = param_buf; |
| 2450 | for (cur = G.top_var; cur; cur = cur->next) { |
| 2451 | if (cur->varstr == hush_version_str) |
| 2452 | continue; |
| 2453 | if (cur->flg_read_only) { |
| 2454 | *pp++ = (char *) "-R"; |
| 2455 | *pp++ = cur->varstr; |
| 2456 | } else if (!cur->flg_export) { |
| 2457 | *pp++ = (char *) "-V"; |
| 2458 | *pp++ = cur->varstr; |
| 2459 | } |
| 2460 | } |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2461 | #if ENABLE_HUSH_FUNCTIONS |
| 2462 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 2463 | *pp++ = (char *) "-F"; |
| 2464 | *pp++ = funcp->name; |
| 2465 | *pp++ = funcp->body_as_string; |
| 2466 | } |
| 2467 | #endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2468 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 2469 | * |
| 2470 | * However, POSIX says that subshells reset signals with traps |
| 2471 | * to SIG_DFL. |
| 2472 | * I tested bash-3.2 and it not only does that with true subshells |
| 2473 | * of the form ( list ), but with any forked children shells. |
| 2474 | * I set trap "echo W" WINCH; and then tried: |
| 2475 | * |
| 2476 | * { echo 1; sleep 20; echo 2; } & |
| 2477 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 2478 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 2479 | * |
| 2480 | * In all these cases sending SIGWINCH to the child shell |
| 2481 | * did not run the trap. If I add trap "echo V" WINCH; |
| 2482 | * _inside_ group (just before echo 1), it works. |
| 2483 | * |
| 2484 | * I conclude it means we don't need to pass active traps here. |
| 2485 | * exec syscall below resets them to SIG_DFL for us. |
| 2486 | */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2487 | *pp++ = (char *) "-c"; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2488 | *pp++ = (char *) s; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2489 | *pp++ = g_argv0; |
| 2490 | while (*g_argv) |
| 2491 | *pp++ = *g_argv++; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2492 | /* *pp = NULL; - is already there */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2493 | pp = environ; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2494 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2495 | do_exec: |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2496 | debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s); |
| 2497 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2498 | execve(bb_busybox_exec_path, argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2499 | /* Fallback. Useful for init=/bin/hush usage etc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2500 | if (argv[0][0] == '/') |
| 2501 | execve(argv[0], argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2502 | xfunc_error_retval = 127; |
| 2503 | bb_error_msg_and_die("can't re-execute the shell"); |
| 2504 | } |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2505 | #endif /* !BB_MMU */ |
| 2506 | |
| 2507 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2508 | static void setup_heredoc(struct redir_struct *redir) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2509 | { |
| 2510 | struct fd_pair pair; |
| 2511 | pid_t pid; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2512 | int len, written; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2513 | /* the _body_ of heredoc (misleading field name) */ |
| 2514 | const char *heredoc = redir->rd_filename; |
| 2515 | char *expanded; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2516 | #if !BB_MMU |
| 2517 | char **to_free; |
| 2518 | #endif |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2519 | |
| 2520 | expanded = NULL; |
| 2521 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
| 2522 | expanded = expand_pseudo_dquoted(heredoc); |
| 2523 | if (expanded) |
| 2524 | heredoc = expanded; |
| 2525 | } |
| 2526 | len = strlen(heredoc); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2527 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2528 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2529 | xpiped_pair(pair); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2530 | xmove_fd(pair.rd, redir->rd_fd); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2531 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2532 | /* Try writing without forking. Newer kernels have |
| 2533 | * dynamically growing pipes. Must use non-blocking write! */ |
| 2534 | ndelay_on(pair.wr); |
| 2535 | while (1) { |
| 2536 | written = write(pair.wr, heredoc, len); |
| 2537 | if (written <= 0) |
| 2538 | break; |
| 2539 | len -= written; |
| 2540 | if (len == 0) { |
| 2541 | close(pair.wr); |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 2542 | free(expanded); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2543 | return; |
| 2544 | } |
| 2545 | heredoc += written; |
| 2546 | } |
| 2547 | ndelay_off(pair.wr); |
| 2548 | |
| 2549 | /* Okay, pipe buffer was not big enough */ |
| 2550 | /* Note: we must not create a stray child (bastard? :) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2551 | * for the unsuspecting parent process. Child creates a grandchild |
| 2552 | * and exits before parent execs the process which consumes heredoc |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2553 | * (that exec happens after we return from this function) */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 2554 | #if !BB_MMU |
| 2555 | to_free = NULL; |
| 2556 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2557 | pid = vfork(); |
| 2558 | if (pid < 0) |
| 2559 | bb_perror_msg_and_die("vfork"); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2560 | if (pid == 0) { |
| 2561 | /* child */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 2562 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2563 | pid = BB_MMU ? fork() : vfork(); |
| 2564 | if (pid < 0) |
| 2565 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
| 2566 | if (pid != 0) |
| 2567 | _exit(0); |
| 2568 | /* grandchild */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2569 | close(redir->rd_fd); /* read side of the pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2570 | #if BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2571 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2572 | _exit(0); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2573 | #else |
| 2574 | /* Delegate blocking writes to another process */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2575 | xmove_fd(pair.wr, STDOUT_FILENO); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2576 | re_execute_shell(&to_free, heredoc, NULL, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2577 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2578 | } |
| 2579 | /* parent */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2580 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2581 | #if !BB_MMU |
| 2582 | free(to_free); |
| 2583 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2584 | close(pair.wr); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2585 | free(expanded); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2586 | wait(NULL); /* wait till child has died */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2589 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 2590 | * and stderr if they are redirected. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2591 | static int setup_redirects(struct command *prog, int squirrel[]) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2592 | { |
| 2593 | int openfd, mode; |
| 2594 | struct redir_struct *redir; |
| 2595 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2596 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2597 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2598 | /* rd_fd<<HERE case */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2599 | if (squirrel && redir->rd_fd < 3) { |
| 2600 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2601 | } |
| 2602 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 2603 | * of the heredoc */ |
| 2604 | debug_printf_parse("set heredoc '%s'\n", |
| 2605 | redir->rd_filename); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2606 | setup_heredoc(redir); |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2607 | continue; |
| 2608 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2609 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2610 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
| 2611 | /* rd_fd<*>file case (<*> is <,>,>>,<>) */ |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2612 | char *p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2613 | if (redir->rd_filename == NULL) { |
| 2614 | /* Something went wrong in the parse. |
| 2615 | * Pretend it didn't happen */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2616 | bb_error_msg("bug in redirect parse"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2617 | continue; |
| 2618 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2619 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2620 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2621 | openfd = open_or_warn(p, mode); |
| 2622 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2623 | if (openfd < 0) { |
| 2624 | /* this could get lost if stderr has been redirected, but |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2625 | * bash and ash both lose it as well (though zsh doesn't!) */ |
| 2626 | //what the above comment tries to say? |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2627 | return 1; |
| 2628 | } |
| 2629 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2630 | /* rd_fd<*>rd_dup or rd_fd<*>- cases */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2631 | openfd = redir->rd_dup; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2634 | if (openfd != redir->rd_fd) { |
| 2635 | if (squirrel && redir->rd_fd < 3) { |
| 2636 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2637 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2638 | if (openfd == REDIRFD_CLOSE) { |
| 2639 | /* "n>-" means "close me" */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2640 | close(redir->rd_fd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2641 | } else { |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2642 | xdup2(openfd, redir->rd_fd); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2643 | if (redir->rd_dup == REDIRFD_TO_FILE) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2644 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2645 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2646 | } |
| 2647 | } |
| 2648 | return 0; |
| 2649 | } |
| 2650 | |
| 2651 | static void restore_redirects(int squirrel[]) |
| 2652 | { |
| 2653 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2654 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2655 | fd = squirrel[i]; |
| 2656 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2657 | /* We simply die on error */ |
| 2658 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2659 | } |
| 2660 | } |
| 2661 | } |
| 2662 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2663 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2664 | static void free_pipe_list(struct pipe *head); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2665 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2666 | /* Return code is the exit status of the pipe */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2667 | static void free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2668 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2669 | char **p; |
| 2670 | struct command *command; |
| 2671 | struct redir_struct *r, *rnext; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2672 | int a, i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2673 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2674 | if (pi->stopped_cmds > 0) /* why? */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2675 | return; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2676 | debug_printf_clean("run pipe: (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2677 | for (i = 0; i < pi->num_cmds; i++) { |
| 2678 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2679 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2680 | if (command->argv) { |
| 2681 | for (a = 0, p = command->argv; *p; a++, p++) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2682 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2683 | } |
| 2684 | free_strings(command->argv); |
| 2685 | command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2686 | } |
| 2687 | /* not "else if": on syntax error, we may have both! */ |
| 2688 | if (command->group) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2689 | debug_printf_clean(" begin group (grp_type:%d)\n", |
| 2690 | command->grp_type); |
| 2691 | free_pipe_list(command->group); |
| 2692 | debug_printf_clean(" end group\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2693 | command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2694 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 2695 | /* else is crucial here. |
| 2696 | * If group != NULL, child_func is meaningless */ |
| 2697 | #if ENABLE_HUSH_FUNCTIONS |
| 2698 | else if (command->child_func) { |
| 2699 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 2700 | command->child_func->parent_cmd = NULL; |
| 2701 | } |
| 2702 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2703 | #if !BB_MMU |
| 2704 | free(command->group_as_string); |
| 2705 | command->group_as_string = NULL; |
| 2706 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2707 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2708 | debug_printf_clean(" redirect %d%s", |
| 2709 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2710 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 2711 | if (r->rd_filename) { |
| 2712 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 2713 | free(r->rd_filename); |
| 2714 | r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2715 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2716 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2717 | rnext = r->next; |
| 2718 | free(r); |
| 2719 | } |
| 2720 | command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2721 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2722 | free(pi->cmds); /* children are an array, they get freed all at once */ |
| 2723 | pi->cmds = NULL; |
| 2724 | #if ENABLE_HUSH_JOB |
| 2725 | free(pi->cmdtext); |
| 2726 | pi->cmdtext = NULL; |
| 2727 | #endif |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2728 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2729 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2730 | static void free_pipe_list(struct pipe *head) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2731 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2732 | struct pipe *pi, *next; |
| 2733 | |
| 2734 | for (pi = head; pi; pi = next) { |
| 2735 | #if HAS_KEYWORDS |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2736 | debug_printf_clean(" pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2737 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2738 | free_pipe(pi); |
| 2739 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2740 | next = pi->next; |
| 2741 | /*pi->next = NULL;*/ |
| 2742 | free(pi); |
| 2743 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2747 | static int run_list(struct pipe *pi); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2748 | #if BB_MMU |
| 2749 | #define parse_stream(pstring, input, end_trigger) \ |
| 2750 | parse_stream(input, end_trigger) |
| 2751 | #endif |
| 2752 | static struct pipe *parse_stream(char **pstring, |
| 2753 | struct in_str *input, |
| 2754 | int end_trigger); |
| 2755 | static void parse_and_run_string(const char *s); |
| 2756 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2757 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2758 | static const struct built_in_command* find_builtin(const char *name) |
| 2759 | { |
| 2760 | const struct built_in_command *x; |
| 2761 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
| 2762 | if (strcmp(name, x->cmd) != 0) |
| 2763 | continue; |
| 2764 | debug_printf_exec("found builtin '%s'\n", name); |
| 2765 | return x; |
| 2766 | } |
| 2767 | return NULL; |
| 2768 | } |
| 2769 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2770 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2771 | static const struct function *find_function(const char *name) |
| 2772 | { |
| 2773 | const struct function *funcp = G.top_func; |
| 2774 | while (funcp) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2775 | if (strcmp(name, funcp->name) == 0) { |
| 2776 | break; |
| 2777 | } |
| 2778 | funcp = funcp->next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2779 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2780 | debug_printf_exec("found function '%s'\n", name); |
| 2781 | return funcp; |
| 2782 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 2783 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2784 | /* Note: takes ownership on name ptr */ |
| 2785 | static struct function *new_function(char *name) |
| 2786 | { |
| 2787 | struct function *funcp; |
| 2788 | struct function **funcpp = &G.top_func; |
| 2789 | |
| 2790 | while ((funcp = *funcpp) != NULL) { |
| 2791 | struct command *cmd; |
| 2792 | |
| 2793 | if (strcmp(funcp->name, name) != 0) { |
| 2794 | funcpp = &funcp->next; |
| 2795 | continue; |
| 2796 | } |
| 2797 | |
| 2798 | cmd = funcp->parent_cmd; |
| 2799 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 2800 | if (!cmd) { |
| 2801 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 2802 | free(funcp->name); |
| 2803 | /* Note: if !funcp->body, do not free body_as_string! |
| 2804 | * This is a special case of "-F name body" function: |
| 2805 | * body_as_string was not malloced! */ |
| 2806 | if (funcp->body) { |
| 2807 | free_pipe_list(funcp->body); |
| 2808 | #if !BB_MMU |
| 2809 | free(funcp->body_as_string); |
| 2810 | #endif |
| 2811 | } |
| 2812 | } else { |
| 2813 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 2814 | cmd->argv[0] = funcp->name; |
| 2815 | cmd->group = funcp->body; |
| 2816 | #if !BB_MMU |
| 2817 | cmd->group_as_string = funcp->body_as_string; |
| 2818 | #endif |
| 2819 | } |
| 2820 | goto skip; |
| 2821 | } |
| 2822 | debug_printf_exec("remembering new function '%s'\n", command->argv[0]); |
| 2823 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 2824 | /*funcp->next = NULL;*/ |
| 2825 | skip: |
| 2826 | funcp->name = name; |
| 2827 | return funcp; |
| 2828 | } |
| 2829 | |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2830 | #if BB_MMU |
| 2831 | #define exec_function(nommu_save, funcp, argv) \ |
| 2832 | exec_function(funcp, argv) |
| 2833 | #endif |
| 2834 | static void exec_function(nommu_save_t *nommu_save, |
| 2835 | const struct function *funcp, |
| 2836 | char **argv) NORETURN; |
| 2837 | static void exec_function(nommu_save_t *nommu_save, |
| 2838 | const struct function *funcp, |
| 2839 | char **argv) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2840 | { |
| 2841 | # if BB_MMU |
| 2842 | int n = 1; |
| 2843 | |
| 2844 | argv[0] = G.global_argv[0]; |
| 2845 | G.global_argv = argv; |
| 2846 | while (*++argv) |
| 2847 | n++; |
| 2848 | G.global_argc = n; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2849 | /* On MMU, funcp->body is always non-NULL */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2850 | n = run_list(funcp->body); |
| 2851 | fflush(NULL); |
| 2852 | _exit(n); |
| 2853 | # else |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2854 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 2855 | funcp->body_as_string, |
| 2856 | G.global_argv[0], |
| 2857 | argv + 1); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 2858 | # endif |
| 2859 | } |
| 2860 | |
| 2861 | static int run_function(const struct function *funcp, char **argv) |
| 2862 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 2863 | int rc; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 2864 | smallint sv_flg; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 2865 | save_arg_t sv; |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 2866 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 2867 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 2868 | /* "we are in function, ok to use return" */ |
| 2869 | sv_flg = G.flag_return_in_progress; |
| 2870 | G.flag_return_in_progress = -1; |
| 2871 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2872 | /* On MMU, funcp->body is always non-NULL */ |
| 2873 | #if !BB_MMU |
| 2874 | if (!funcp->body) { |
| 2875 | /* Function defined by -F */ |
| 2876 | parse_and_run_string(funcp->body_as_string); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 2877 | rc = G.last_exitcode; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2878 | } else |
| 2879 | #endif |
| 2880 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 2881 | rc = run_list(funcp->body); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2882 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 2883 | |
| 2884 | G.flag_return_in_progress = sv_flg; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 2885 | restore_G_args(&sv, argv); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 2886 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 2887 | return rc; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2888 | } |
| 2889 | #endif |
| 2890 | |
| 2891 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 2892 | #if BB_MMU |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2893 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 2894 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 2895 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 2896 | pseudo_exec(command, argv_expanded) |
| 2897 | #endif |
| 2898 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 2899 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2900 | * Never returns. |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 2901 | * Don't exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2902 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2903 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2904 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 2905 | char **argv, int assignment_cnt, |
| 2906 | char **argv_expanded) NORETURN; |
| 2907 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 2908 | char **argv, int assignment_cnt, |
| 2909 | char **argv_expanded) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2910 | { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2911 | char **new_env; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2912 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2913 | /* Case when we are here: ... | var=val | ... */ |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2914 | if (!argv[assignment_cnt]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2915 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2916 | |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 2917 | new_env = expand_assignments(argv, assignment_cnt); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2918 | #if BB_MMU |
| 2919 | putenv_all(new_env); |
| 2920 | free(new_env); /* optional */ |
| 2921 | #else |
| 2922 | nommu_save->new_env = new_env; |
| 2923 | nommu_save->old_env = putenv_all_and_save_old(new_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2924 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2925 | if (argv_expanded) { |
| 2926 | argv = argv_expanded; |
| 2927 | } else { |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 2928 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2929 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2930 | nommu_save->argv = argv; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2931 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2932 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2933 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2934 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 2935 | if (strchr(argv[0], '/') != NULL) |
| 2936 | goto skip; |
| 2937 | #endif |
| 2938 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2939 | /* On NOMMU, we must never block! |
| 2940 | * Example: { sleep 99999 | read line } & echo Ok |
| 2941 | * read builtin will block on read syscall, leaving parent blocked |
| 2942 | * in vfork. Therefore we can't do this: |
| 2943 | */ |
| 2944 | #if BB_MMU |
| 2945 | /* Check if the command matches any of the builtins. |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2946 | * Depending on context, this might be redundant. But it's |
| 2947 | * easier to waste a few CPU cycles than it is to figure out |
| 2948 | * if this is one of those cases. |
| 2949 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2950 | { |
| 2951 | int rcode; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2952 | const struct built_in_command *x = find_builtin(argv[0]); |
| 2953 | if (x) { |
| 2954 | rcode = x->function(argv); |
| 2955 | fflush(NULL); |
| 2956 | _exit(rcode); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2957 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2958 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2959 | #endif |
| 2960 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2961 | /* Check if the command matches any functions */ |
| 2962 | { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2963 | const struct function *funcp = find_function(argv[0]); |
| 2964 | if (funcp) { |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2965 | exec_function(nommu_save, funcp, argv); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2966 | } |
| 2967 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2968 | #endif |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2969 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 2970 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2971 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2972 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 2973 | int a = find_applet_by_name(argv[0]); |
| 2974 | if (a >= 0) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2975 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 2976 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2977 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 2978 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2979 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2980 | # endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2981 | /* Re-exec ourselves */ |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2982 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2983 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 2984 | execv(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2985 | /* If they called chroot or otherwise made the binary no longer |
| 2986 | * executable, fall through */ |
| 2987 | } |
| 2988 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 2989 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2990 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2991 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2992 | skip: |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2993 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2994 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2995 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2996 | execvp(argv[0], argv); |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 2997 | bb_perror_msg("can't exec '%s'", argv[0]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 2998 | _exit(EXIT_FAILURE); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3001 | /* Called after [v]fork() in run_pipe |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3002 | */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3003 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3004 | struct command *command, |
| 3005 | char **argv_expanded) NORETURN; |
| 3006 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3007 | struct command *command, |
| 3008 | char **argv_expanded) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3009 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3010 | if (command->argv) { |
| 3011 | pseudo_exec_argv(nommu_save, command->argv, |
| 3012 | command->assignment_cnt, argv_expanded); |
| 3013 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3014 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3015 | if (command->group) { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3016 | /* Cases when we are here: |
| 3017 | * ( list ) |
| 3018 | * { list } & |
| 3019 | * ... | ( list ) | ... |
| 3020 | * ... | { list } | ... |
| 3021 | */ |
| 3022 | #if BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 3023 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3024 | debug_printf_exec("pseudo_exec: run_list\n"); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3025 | reset_traps_to_defaults(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3026 | rcode = run_list(command->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 3027 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3028 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3029 | _exit(rcode); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3030 | #else |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3031 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 3032 | command->group_as_string, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3033 | G.global_argv[0], |
| 3034 | G.global_argv + 1); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3035 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3036 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3037 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3038 | /* Case when we are here: ... | >file */ |
| 3039 | debug_printf_exec("pseudo_exec'ed null command\n"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3040 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3041 | } |
| 3042 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3043 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3044 | static const char *get_cmdtext(struct pipe *pi) |
| 3045 | { |
| 3046 | char **argv; |
| 3047 | char *p; |
| 3048 | int len; |
| 3049 | |
| 3050 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 3051 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3052 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3053 | if (pi->cmdtext) |
| 3054 | return pi->cmdtext; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3055 | argv = pi->cmds[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3056 | if (!argv || !argv[0]) { |
| 3057 | pi->cmdtext = xzalloc(1); |
| 3058 | return pi->cmdtext; |
| 3059 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3060 | |
| 3061 | len = 0; |
| 3062 | do len += strlen(*argv) + 1; while (*++argv); |
| 3063 | pi->cmdtext = p = xmalloc(len); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3064 | argv = pi->cmds[0].argv; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3065 | do { |
| 3066 | len = strlen(*argv); |
| 3067 | memcpy(p, *argv, len); |
| 3068 | p += len; |
| 3069 | *p++ = ' '; |
| 3070 | } while (*++argv); |
| 3071 | p[-1] = '\0'; |
| 3072 | return pi->cmdtext; |
| 3073 | } |
| 3074 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3075 | static void insert_bg_job(struct pipe *pi) |
| 3076 | { |
| 3077 | struct pipe *thejob; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3078 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3079 | |
| 3080 | /* Linear search for the ID of the job to use */ |
| 3081 | pi->jobid = 1; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3082 | for (thejob = G.job_list; thejob; thejob = thejob->next) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3083 | if (thejob->jobid >= pi->jobid) |
| 3084 | pi->jobid = thejob->jobid + 1; |
| 3085 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3086 | /* Add thejob to the list of running jobs */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3087 | if (!G.job_list) { |
| 3088 | thejob = G.job_list = xmalloc(sizeof(*thejob)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3089 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3090 | for (thejob = G.job_list; thejob->next; thejob = thejob->next) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3091 | continue; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3092 | thejob->next = xmalloc(sizeof(*thejob)); |
| 3093 | thejob = thejob->next; |
| 3094 | } |
| 3095 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3096 | /* Physically copy the struct job */ |
Eric Andersen | 0fcd447 | 2001-05-02 20:12:03 +0000 | [diff] [blame] | 3097 | memcpy(thejob, pi, sizeof(struct pipe)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3098 | thejob->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 3099 | /* We cannot copy entire pi->cmds[] vector! Double free()s will happen */ |
| 3100 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3101 | // TODO: do we really need to have so many fields which are just dead weight |
| 3102 | // at execution stage? |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3103 | thejob->cmds[i].pid = pi->cmds[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3104 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3105 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3106 | thejob->next = NULL; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3107 | thejob->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3108 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3109 | /* We don't wait for background thejobs to return -- append it |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3110 | to the list of backgrounded thejobs and leave it alone */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3111 | if (G_interactive_fd) |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 3112 | printf("[%d] %d %s\n", thejob->jobid, thejob->cmds[0].pid, thejob->cmdtext); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3113 | G.last_bg_pid = thejob->cmds[0].pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3114 | G.last_jobid = thejob->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3117 | static void remove_bg_job(struct pipe *pi) |
| 3118 | { |
| 3119 | struct pipe *prev_pipe; |
| 3120 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3121 | if (pi == G.job_list) { |
| 3122 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3123 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3124 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3125 | while (prev_pipe->next != pi) |
| 3126 | prev_pipe = prev_pipe->next; |
| 3127 | prev_pipe->next = pi->next; |
| 3128 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3129 | if (G.job_list) |
| 3130 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3131 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3132 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3133 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3134 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3135 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3136 | static void delete_finished_bg_job(struct pipe *pi) |
| 3137 | { |
| 3138 | remove_bg_job(pi); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3139 | pi->stopped_cmds = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3140 | free_pipe(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3141 | free(pi); |
| 3142 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3143 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3144 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3145 | /* Check to see if any processes have exited -- if they |
| 3146 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3147 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3148 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3149 | int attributes; |
| 3150 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3151 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3152 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3153 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3154 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3155 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3156 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3157 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 3158 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3159 | errno = 0; |
| 3160 | // if (G.handled_SIGCHLD == G.count_SIGCHLD) |
| 3161 | // /* avoid doing syscall, nothing there anyway */ |
| 3162 | // return rcode; |
| 3163 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3164 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3165 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3166 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3167 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3168 | /* Do we do this right? |
| 3169 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3170 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3171 | * [3]+ Stopped sleep 20 | false |
| 3172 | * bash-3.00# echo $? |
| 3173 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3174 | * [hush 1.14.0: yes we do it right] |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3175 | */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3176 | wait_more: |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3177 | while (1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3178 | int i; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3179 | int dead; |
| 3180 | |
| 3181 | // i = G.count_SIGCHLD; |
| 3182 | childpid = waitpid(-1, &status, attributes); |
| 3183 | if (childpid <= 0) { |
| 3184 | if (childpid && errno != ECHILD) |
| 3185 | bb_perror_msg("waitpid"); |
| 3186 | // else /* Until next SIGCHLD, waitpid's are useless */ |
| 3187 | // G.handled_SIGCHLD = i; |
| 3188 | break; |
| 3189 | } |
| 3190 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 3191 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3192 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3193 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3194 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3195 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 3196 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3197 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3198 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 3199 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3200 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3201 | childpid, WEXITSTATUS(status)); |
| 3202 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3203 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3204 | if (fg_pipe) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3205 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 3206 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 3207 | if (fg_pipe->cmds[i].pid != childpid) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3208 | continue; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3209 | if (dead) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3210 | fg_pipe->cmds[i].pid = 0; |
| 3211 | fg_pipe->alive_cmds--; |
| 3212 | if (i == fg_pipe->num_cmds - 1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3213 | /* last process gives overall exitstatus */ |
| 3214 | rcode = WEXITSTATUS(status); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3215 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3216 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3217 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3218 | fg_pipe->cmds[i].is_stopped = 1; |
| 3219 | fg_pipe->stopped_cmds++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3220 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3221 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 3222 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 3223 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3224 | /* All processes in fg pipe have exited or stopped */ |
| 3225 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 3226 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 3227 | * and "killall -STOP cat" */ |
| 3228 | if (G_interactive_fd) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3229 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3230 | if (fg_pipe->alive_cmds) |
| 3231 | insert_bg_job(fg_pipe); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3232 | #endif |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3233 | return rcode; |
| 3234 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3235 | } |
| 3236 | /* There are still running processes in the fg pipe */ |
| 3237 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3238 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3239 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3242 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3243 | /* We asked to wait for bg or orphaned children */ |
| 3244 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3245 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3246 | for (i = 0; i < pi->num_cmds; i++) { |
| 3247 | if (pi->cmds[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3248 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3249 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3250 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3251 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 3252 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3253 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 3254 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3255 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3256 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3257 | /* child exited */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3258 | pi->cmds[i].pid = 0; |
| 3259 | pi->alive_cmds--; |
| 3260 | if (!pi->alive_cmds) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3261 | if (G_interactive_fd) |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 3262 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 3263 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3264 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3265 | } |
| 3266 | } else { |
| 3267 | /* child stopped */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3268 | pi->cmds[i].is_stopped = 1; |
| 3269 | pi->stopped_cmds++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3270 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3271 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3272 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3273 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3274 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 3275 | } |
| 3276 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3277 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3278 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 3279 | { |
| 3280 | pid_t p; |
| 3281 | int rcode = checkjobs(fg_pipe); |
| 3282 | /* Job finished, move the shell to the foreground */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3283 | p = getpgid(0); /* pgid of our process */ |
| 3284 | debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3285 | tcsetpgrp(G_interactive_fd, p); |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3286 | return rcode; |
| 3287 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3288 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3289 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3290 | /* Start all the jobs, but don't wait for anything to finish. |
| 3291 | * See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3292 | * |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3293 | * 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] | 3294 | * to finish to determine the exit status of the pipe. If the pipe |
| 3295 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3296 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3297 | * return value. |
| 3298 | * |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3299 | * Returns -1 only if started some children. IOW: we have to |
| 3300 | * mask out retvals of builtins etc with 0xff! |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3301 | * |
| 3302 | * The only case when we do not need to [v]fork is when the pipe |
| 3303 | * is single, non-backgrounded, non-subshell command. Examples: |
| 3304 | * cmd ; ... { list } ; ... |
| 3305 | * cmd && ... { list } && ... |
| 3306 | * cmd || ... { list } || ... |
| 3307 | * If it is, then we can run cmd as a builtin, NOFORK [do we do this?], |
| 3308 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3309 | * 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] | 3310 | * |
| 3311 | * Cases when we must fork: |
| 3312 | * non-single: cmd | cmd |
| 3313 | * backgrounded: cmd & { list } & |
| 3314 | * subshell: ( list ) [&] |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3315 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3316 | static int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3317 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3318 | static const char *const null_ptr = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3319 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3320 | int nextin; |
| 3321 | int pipefds[2]; /* pipefds[0] is for reading */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3322 | struct command *command; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3323 | char **argv_expanded; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3324 | char **argv; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3325 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3326 | /* it is not always needed, but we aim to smaller code */ |
| 3327 | int squirrel[] = { -1, -1, -1 }; |
| 3328 | int rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3329 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3330 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3331 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 3332 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3333 | USE_HUSH_JOB(pi->pgrp = -1;) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3334 | pi->stopped_cmds = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3335 | command = &(pi->cmds[0]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3336 | argv_expanded = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3337 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3338 | if (pi->num_cmds != 1 |
| 3339 | || pi->followup == PIPE_BG |
| 3340 | || command->grp_type == GRP_SUBSHELL |
| 3341 | ) { |
| 3342 | goto must_fork; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3343 | } |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3344 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3345 | pi->alive_cmds = 1; |
| 3346 | |
| 3347 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 3348 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 3349 | |
| 3350 | if (command->group) { |
| 3351 | #if ENABLE_HUSH_FUNCTIONS |
| 3352 | if (command->grp_type == GRP_FUNCTION) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3353 | /* "executing" func () { list } */ |
| 3354 | struct function *funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3355 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3356 | funcp = new_function(command->argv[0]); |
| 3357 | /* funcp->name is already set to argv[0] */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3358 | funcp->body = command->group; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3359 | #if !BB_MMU |
| 3360 | funcp->body_as_string = command->group_as_string; |
| 3361 | command->group_as_string = NULL; |
| 3362 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3363 | command->group = NULL; |
| 3364 | command->argv[0] = NULL; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3365 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 3366 | funcp->parent_cmd = command; |
| 3367 | command->child_func = funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3368 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3369 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 3370 | debug_leave(); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3371 | return EXIT_SUCCESS; |
| 3372 | } |
| 3373 | #endif |
| 3374 | /* { list } */ |
| 3375 | debug_printf("non-subshell group\n"); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3376 | rcode = 1; /* exitcode if redir failed */ |
| 3377 | if (setup_redirects(command, squirrel) == 0) { |
| 3378 | debug_printf_exec(": run_list\n"); |
| 3379 | rcode = run_list(command->group) & 0xff; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3380 | } |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 3381 | restore_redirects(squirrel); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3382 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3383 | debug_leave(); |
| 3384 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3385 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3388 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 3389 | { |
| 3390 | const struct built_in_command *x; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3391 | #if ENABLE_HUSH_FUNCTIONS |
| 3392 | const struct function *funcp; |
| 3393 | #else |
| 3394 | enum { funcp = 0 }; |
| 3395 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3396 | char **new_env = NULL; |
| 3397 | char **old_env = NULL; |
| 3398 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3399 | if (argv[command->assignment_cnt] == NULL) { |
| 3400 | /* Assignments, but no command */ |
| 3401 | /* Ensure redirects take effect. Try "a=t >file" */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3402 | rcode = setup_redirects(command, squirrel); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3403 | restore_redirects(squirrel); |
| 3404 | /* Set shell variables */ |
| 3405 | while (*argv) { |
| 3406 | p = expand_string_to_string(*argv); |
| 3407 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 3408 | *argv, p); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 3409 | set_local_var(p, 0, 0); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3410 | argv++; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3411 | } |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3412 | /* Do we need to flag set_local_var() errors? |
| 3413 | * "assignment to readonly var" and "putenv error" |
| 3414 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3415 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3416 | debug_leave(); |
| 3417 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3418 | return rcode; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3419 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3420 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3421 | /* Expand the rest into (possibly) many strings each */ |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3422 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3423 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3424 | x = find_builtin(argv_expanded[0]); |
| 3425 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3426 | funcp = NULL; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3427 | if (!x) |
| 3428 | funcp = find_function(argv_expanded[0]); |
| 3429 | #endif |
| 3430 | if (x || funcp) { |
| 3431 | if (!funcp) { |
| 3432 | if (x->function == builtin_exec && argv_expanded[1] == NULL) { |
| 3433 | debug_printf("exec with redirects only\n"); |
| 3434 | rcode = setup_redirects(command, NULL); |
| 3435 | goto clean_up_and_ret1; |
| 3436 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3437 | } |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3438 | /* setup_redirects acts on file descriptors, not FILEs. |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3439 | * This is perfect for work that comes after exec(). |
| 3440 | * Is it really safe for inline use? Experimentally, |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3441 | * things seem to work. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3442 | rcode = setup_redirects(command, squirrel); |
| 3443 | if (rcode == 0) { |
| 3444 | new_env = expand_assignments(argv, command->assignment_cnt); |
| 3445 | old_env = putenv_all_and_save_old(new_env); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3446 | if (!funcp) { |
| 3447 | debug_printf_exec(": builtin '%s' '%s'...\n", |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3448 | x->cmd, argv_expanded[1]); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3449 | rcode = x->function(argv_expanded) & 0xff; |
| 3450 | } |
| 3451 | #if ENABLE_HUSH_FUNCTIONS |
| 3452 | else { |
| 3453 | debug_printf_exec(": function '%s' '%s'...\n", |
| 3454 | funcp->name, argv_expanded[1]); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3455 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3456 | } |
| 3457 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3458 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3459 | #if ENABLE_FEATURE_SH_STANDALONE |
| 3460 | clean_up_and_ret: |
| 3461 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3462 | restore_redirects(squirrel); |
| 3463 | free_strings_and_unsetenv(new_env, 1); |
| 3464 | putenv_all(old_env); |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 3465 | /* Free the pointers, but the strings themselves |
| 3466 | * are in environ now, don't use free_strings! */ |
| 3467 | free(old_env); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3468 | clean_up_and_ret1: |
| 3469 | free(argv_expanded); |
| 3470 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3471 | debug_leave(); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3472 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 3473 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3474 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3475 | |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3476 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3477 | i = find_applet_by_name(argv_expanded[0]); |
| 3478 | if (i >= 0 && APPLET_IS_NOFORK(i)) { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3479 | rcode = setup_redirects(command, squirrel); |
| 3480 | if (rcode == 0) { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3481 | new_env = expand_assignments(argv, command->assignment_cnt); |
| 3482 | old_env = putenv_all_and_save_old(new_env); |
| 3483 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3484 | argv_expanded[0], argv_expanded[1]); |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 3485 | rcode = run_nofork_applet(i, argv_expanded); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3486 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3487 | goto clean_up_and_ret; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3488 | } |
| 3489 | #endif |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3490 | /* It is neither builtin nor applet. We must fork. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3493 | must_fork: |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3494 | /* NB: argv_expanded may already be created, and that |
| 3495 | * might include `cmd` runs! Do not rerun it! We *must* |
| 3496 | * use argv_expanded if it's non-NULL */ |
| 3497 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3498 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3499 | pi->alive_cmds = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3500 | nextin = 0; |
| 3501 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3502 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3503 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3504 | volatile nommu_save_t nommu_save; |
| 3505 | nommu_save.new_env = NULL; |
| 3506 | nommu_save.old_env = NULL; |
| 3507 | nommu_save.argv = NULL; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3508 | nommu_save.argv_from_re_execing = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3509 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3510 | command = &(pi->cmds[i]); |
| 3511 | if (command->argv) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3512 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 3513 | command->argv[0], command->argv[1]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3514 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3515 | debug_printf_exec(": pipe member with no argv\n"); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3516 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3517 | |
| 3518 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3519 | pipefds[0] = 0; |
| 3520 | pipefds[1] = 1; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3521 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3522 | xpipe(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3523 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3524 | command->pid = BB_MMU ? fork() : vfork(); |
| 3525 | if (!command->pid) { /* child */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3526 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3527 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3528 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3529 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3530 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3531 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3532 | pid_t pgrp; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3533 | pgrp = pi->pgrp; |
| 3534 | if (pgrp < 0) /* true for 1st process only */ |
| 3535 | pgrp = getpid(); |
| 3536 | if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3537 | /* We do it in *every* child, not just first, |
| 3538 | * to avoid races */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3539 | tcsetpgrp(G_interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3540 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3541 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3542 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3543 | xmove_fd(nextin, 0); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3544 | xmove_fd(pipefds[1], 1); /* write end */ |
| 3545 | if (pipefds[0] > 1) |
| 3546 | close(pipefds[0]); /* read end */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3547 | /* Like bash, explicit redirects override pipes, |
| 3548 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3549 | if (setup_redirects(command, NULL)) |
| 3550 | _exit(1); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3551 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3552 | /* Restore default handlers just prior to exec */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 3553 | /*signal(SIGCHLD, SIG_DFL); - so far we don't have any handlers */ |
| 3554 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3555 | /* Stores to nommu_save list of env vars putenv'ed |
| 3556 | * (NOMMU, on MMU we don't need that) */ |
| 3557 | /* cast away volatility... */ |
| 3558 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3559 | /* pseudo_exec() does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3560 | } |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 3561 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 3562 | /* parent or error */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3563 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3564 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3565 | /* Clean up after vforked child */ |
| 3566 | free(nommu_save.argv); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3567 | free(nommu_save.argv_from_re_execing); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3568 | free_strings_and_unsetenv(nommu_save.new_env, 1); |
| 3569 | putenv_all(nommu_save.old_env); |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 3570 | /* Free the pointers, but the strings themselves |
| 3571 | * are in environ now, don't use free_strings! */ |
| 3572 | free(nommu_save.old_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3573 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3574 | free(argv_expanded); |
| 3575 | argv_expanded = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3576 | if (command->pid < 0) { /* [v]fork failed */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3577 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 3578 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3579 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3580 | pi->alive_cmds++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3581 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3582 | /* Second and next children need to know pid of first one */ |
| 3583 | if (pi->pgrp < 0) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3584 | pi->pgrp = command->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3585 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3586 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3587 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3588 | if (i) |
| 3589 | close(nextin); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3590 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3591 | close(pipefds[1]); /* write end */ |
| 3592 | /* Pass read (output) pipe end to next iteration */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3593 | nextin = pipefds[0]; |
| 3594 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3595 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3596 | if (!pi->alive_cmds) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3597 | debug_leave(); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3598 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 3599 | return 1; |
| 3600 | } |
| 3601 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3602 | debug_leave(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3603 | 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] | 3604 | return -1; |
| 3605 | } |
| 3606 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 3607 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3608 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3609 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3610 | static const char *const PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3611 | [PIPE_SEQ] = "SEQ", |
| 3612 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3613 | [PIPE_OR ] = "OR" , |
| 3614 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3615 | }; |
| 3616 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3617 | [RES_NONE ] = "NONE" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3618 | #if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3619 | [RES_IF ] = "IF" , |
| 3620 | [RES_THEN ] = "THEN" , |
| 3621 | [RES_ELIF ] = "ELIF" , |
| 3622 | [RES_ELSE ] = "ELSE" , |
| 3623 | [RES_FI ] = "FI" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3624 | #endif |
| 3625 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3626 | [RES_FOR ] = "FOR" , |
| 3627 | [RES_WHILE] = "WHILE", |
| 3628 | [RES_UNTIL] = "UNTIL", |
| 3629 | [RES_DO ] = "DO" , |
| 3630 | [RES_DONE ] = "DONE" , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 3631 | #endif |
| 3632 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3633 | [RES_IN ] = "IN" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3634 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3635 | #if ENABLE_HUSH_CASE |
| 3636 | [RES_CASE ] = "CASE" , |
| 3637 | [RES_MATCH] = "MATCH", |
| 3638 | [RES_CASEI] = "CASEI", |
| 3639 | [RES_ESAC ] = "ESAC" , |
| 3640 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3641 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3642 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3643 | }; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3644 | static const char *const GRPTYPE[] = { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3645 | "{}", |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 3646 | "()", |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3647 | #if ENABLE_HUSH_FUNCTIONS |
| 3648 | "func()", |
| 3649 | #endif |
| 3650 | }; |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3651 | |
| 3652 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3653 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3654 | pin = 0; |
| 3655 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3656 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 3657 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3658 | prn = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3659 | while (prn < pi->num_cmds) { |
| 3660 | struct command *command = &pi->cmds[prn]; |
| 3661 | char **argv = command->argv; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3662 | |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3663 | fprintf(stderr, "%*s cmd %d assignment_cnt:%d", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3664 | lvl*2, "", prn, |
| 3665 | command->assignment_cnt); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3666 | if (command->group) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3667 | fprintf(stderr, " group %s: (argv=%p)\n", |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3668 | GRPTYPE[command->grp_type], |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3669 | argv); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3670 | debug_print_tree(command->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3671 | prn++; |
| 3672 | continue; |
| 3673 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3674 | if (argv) while (*argv) { |
| 3675 | fprintf(stderr, " '%s'", *argv); |
| 3676 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 3677 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3678 | fprintf(stderr, "\n"); |
| 3679 | prn++; |
| 3680 | } |
| 3681 | pi = pi->next; |
| 3682 | pin++; |
| 3683 | } |
| 3684 | } |
| 3685 | #endif |
| 3686 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3687 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 3688 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3689 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3690 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3691 | #if ENABLE_HUSH_CASE |
| 3692 | char *case_word = NULL; |
| 3693 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3694 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3695 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3696 | char *for_varname = NULL; |
| 3697 | char **for_lcur = NULL; |
| 3698 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3699 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3700 | smallint last_followup; |
| 3701 | smalluint rcode; |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 3702 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3703 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3704 | #else |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3705 | enum { cond_code = 0 }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3706 | #endif |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 3707 | #if HAS_KEYWORDS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3708 | smallint rword; /* enum reserved_style */ |
| 3709 | smallint last_rword; /* ditto */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 3710 | #endif |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 3711 | |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 3712 | 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] | 3713 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 3714 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3715 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3716 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 3717 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 3718 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3719 | continue; |
| 3720 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 3721 | if (cpipe->next == NULL) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3722 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3723 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3724 | 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] | 3725 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3726 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3727 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 3728 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3729 | continue; |
| 3730 | /* 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] | 3731 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 3732 | || 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] | 3733 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3734 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3735 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3736 | 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] | 3737 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3738 | } |
| 3739 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3740 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3741 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 3742 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 3743 | * in order to return, no direct "return" statements please. |
| 3744 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 3745 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3746 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 3747 | G.run_list_level++; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3748 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3749 | |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 3750 | #if HAS_KEYWORDS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3751 | rword = RES_NONE; |
| 3752 | last_rword = RES_XXXX; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 3753 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3754 | last_followup = PIPE_SEQ; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 3755 | rcode = G.last_exitcode; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3756 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3757 | /* Go through list of pipes, (maybe) executing them. */ |
| 3758 | for (; pi; pi = USE_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 3759 | if (G.flag_SIGINT) |
| 3760 | break; |
| 3761 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3762 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3763 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 3764 | rword, cond_code, last_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3765 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 3766 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3767 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 3768 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3769 | /* start of a loop: remember where loop starts */ |
| 3770 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3771 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3772 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3773 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3774 | /* Still in the same "if...", "then..." or "do..." branch? */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 3775 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3776 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 3777 | || (rcode != 0 && last_followup == PIPE_AND) |
| 3778 | ) { |
| 3779 | /* It is "<true> || CMD" or "<false> && CMD" |
| 3780 | * and we should not execute CMD */ |
| 3781 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 3782 | last_followup = pi->followup; |
| 3783 | continue; |
| 3784 | } |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3785 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3786 | last_followup = pi->followup; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 3787 | IF_HAS_KEYWORDS(last_rword = rword;) |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3788 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3789 | if (cond_code) { |
| 3790 | if (rword == RES_THEN) { |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 3791 | /* if false; then ... fi has exitcode 0! */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 3792 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3793 | /* "if <false> THEN cmd": skip cmd */ |
| 3794 | continue; |
| 3795 | } |
| 3796 | } else { |
| 3797 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 3798 | /* "if <true> then ... ELSE/ELIF cmd": |
| 3799 | * skip cmd and all following ones */ |
| 3800 | break; |
| 3801 | } |
| 3802 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3803 | #endif |
| 3804 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3805 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3806 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 3807 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3808 | |
| 3809 | static const char encoded_dollar_at[] ALIGN1 = { |
| 3810 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 3811 | }; /* encoded representation of "$@" */ |
| 3812 | static const char *const encoded_dollar_at_argv[] = { |
| 3813 | encoded_dollar_at, NULL |
| 3814 | }; /* argv list with one element: "$@" */ |
| 3815 | char **vals; |
| 3816 | |
| 3817 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 3818 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3819 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3820 | if (!pi->next->cmds[0].argv) { |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 3821 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3822 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3823 | break; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3824 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3825 | vals = pi->next->cmds[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3826 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3827 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3828 | debug_print_strings("for_list made from", vals); |
| 3829 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3830 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3831 | debug_print_strings("for_list", for_list); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3832 | for_varname = pi->cmds[0].argv[0]; |
| 3833 | pi->cmds[0].argv[0] = NULL; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3834 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3835 | free(pi->cmds[0].argv[0]); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3836 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 3837 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3838 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 3839 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3840 | for_lcur = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3841 | pi->cmds[0].argv[0] = for_varname; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3842 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3843 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3844 | /* Insert next value from for_lcur */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3845 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3846 | pi->cmds[0].argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++); |
| 3847 | pi->cmds[0].assignment_cnt = 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3848 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3849 | if (rword == RES_IN) { |
| 3850 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 3851 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3852 | if (rword == RES_DONE) { |
| 3853 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3854 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3855 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3856 | #if ENABLE_HUSH_CASE |
| 3857 | if (rword == RES_CASE) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3858 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3859 | continue; |
| 3860 | } |
| 3861 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 3862 | char **argv; |
| 3863 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3864 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 3865 | break; |
| 3866 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3867 | argv = pi->cmds->argv; |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 3868 | while (*argv) { |
| 3869 | char *pattern = expand_string_to_string(*argv); |
| 3870 | /* TODO: which FNM_xxx flags to use? */ |
| 3871 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 3872 | free(pattern); |
| 3873 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 3874 | free(case_word); /* make future "word)" stop */ |
| 3875 | case_word = NULL; |
| 3876 | break; |
| 3877 | } |
| 3878 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3879 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3880 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3881 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3882 | if (rword == RES_CASEI) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3883 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3884 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3885 | } |
| 3886 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3887 | /* Just pressing <enter> in shell should check for jobs. |
| 3888 | * OTOH, in non-interactive shell this is useless |
| 3889 | * and only leads to extra job checks */ |
| 3890 | if (pi->num_cmds == 0) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3891 | if (G_interactive_fd) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3892 | goto check_jobs_and_continue; |
| 3893 | continue; |
| 3894 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3895 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3896 | /* After analyzing all keywords and conditions, we decided |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3897 | * to execute this pipe. NB: have to do checkjobs(NULL) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3898 | * after run_pipe to collect any background children, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3899 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3900 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3901 | { |
| 3902 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 3903 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3904 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 3905 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3906 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 3907 | if (r != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 3908 | /* We ran a builtin, function, or group. |
| 3909 | * rcode is already known |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3910 | * and we don't need to wait for anything. */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 3911 | G.last_exitcode = rcode; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3912 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3913 | check_and_run_traps(0); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 3914 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3915 | /* Was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3916 | if (G.flag_break_continue) { |
| 3917 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3918 | /* We might fall into outer *loop*, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3919 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3920 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3921 | G.depth_break_continue--; |
| 3922 | if (G.depth_break_continue == 0) |
| 3923 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 3924 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 3925 | } /* 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] | 3926 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 3927 | goto check_jobs_and_break; |
| 3928 | /* "continue": simulate end of loop */ |
| 3929 | rword = RES_DONE; |
| 3930 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3931 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 3932 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 3933 | #if ENABLE_HUSH_FUNCTIONS |
| 3934 | if (G.flag_return_in_progress == 1) |
| 3935 | goto check_jobs_and_break; |
| 3936 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3937 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3938 | /* What does bash do with attempts to background builtins? */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3939 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3940 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 3941 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3942 | check_and_run_traps(0); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3943 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3944 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3945 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3946 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 3947 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3948 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3949 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3950 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3951 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3952 | /* Waits for completion, then fg's main shell */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3953 | rcode = checkjobs_and_fg_shell(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3954 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3955 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3956 | } else |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3957 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3958 | { /* This one just waits for completion */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3959 | rcode = checkjobs(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3960 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3961 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3962 | } |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 3963 | G.last_exitcode = rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3964 | } |
| 3965 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3966 | |
| 3967 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3968 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3969 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3970 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3971 | #endif |
| 3972 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3973 | /* Beware of "while false; true; do ..."! */ |
| 3974 | if (pi->next && pi->next->res_word == RES_DO) { |
| 3975 | if (rword == RES_WHILE) { |
| 3976 | if (rcode) { |
| 3977 | /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 3978 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3979 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
| 3980 | goto check_jobs_and_break; |
| 3981 | } |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 3982 | } |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3983 | if (rword == RES_UNTIL) { |
| 3984 | if (!rcode) { |
| 3985 | debug_printf_exec(": until expr is true: breaking\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3986 | check_jobs_and_break: |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3987 | checkjobs(NULL); |
| 3988 | break; |
| 3989 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3990 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3991 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3992 | #endif |
Mike Frysinger | 8ec1c9d | 2009-03-29 00:45:26 +0000 | [diff] [blame] | 3993 | |
| 3994 | check_jobs_and_continue: |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3995 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3996 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3997 | |
| 3998 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3999 | G.run_list_level--; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4000 | #endif |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4001 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4002 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4003 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4004 | free(for_list); |
| 4005 | #endif |
| 4006 | #if ENABLE_HUSH_CASE |
| 4007 | free(case_word); |
| 4008 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4009 | debug_leave(); |
| 4010 | 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] | 4011 | return rcode; |
| 4012 | } |
| 4013 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4014 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4015 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4016 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4017 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4018 | debug_printf_exec("run_and_free_list entered\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4019 | if (!G.fake_mode) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4020 | 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] | 4021 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4022 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4023 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4024 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4025 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4026 | free_pipe_list(pi); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 4027 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4028 | return rcode; |
| 4029 | } |
| 4030 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4031 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 4032 | static struct pipe *new_pipe(void) |
| 4033 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4034 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4035 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4036 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4037 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4038 | return pi; |
| 4039 | } |
| 4040 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4041 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 4042 | * if ctx->command is NULL. |
| 4043 | * No errors possible here. |
| 4044 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4045 | static int done_command(struct parse_context *ctx) |
| 4046 | { |
| 4047 | /* The command is really already in the pipe structure, so |
| 4048 | * advance the pipe counter and make a new, null command. */ |
| 4049 | struct pipe *pi = ctx->pipe; |
| 4050 | struct command *command = ctx->command; |
| 4051 | |
| 4052 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4053 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4054 | 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] | 4055 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4056 | } |
| 4057 | pi->num_cmds++; |
| 4058 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4059 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4060 | } else { |
| 4061 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 4062 | } |
| 4063 | |
| 4064 | /* Only real trickiness here is that the uncommitted |
| 4065 | * command structure is not counted in pi->num_cmds. */ |
| 4066 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4067 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 4068 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4069 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4070 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 4071 | } |
| 4072 | |
| 4073 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 4074 | { |
| 4075 | int not_null; |
| 4076 | |
| 4077 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 4078 | /* Close previous command */ |
| 4079 | not_null = done_command(ctx); |
| 4080 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4081 | #if HAS_KEYWORDS |
| 4082 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 4083 | ctx->ctx_inverted = 0; |
| 4084 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 4085 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4086 | |
| 4087 | /* Without this check, even just <enter> on command line generates |
| 4088 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4089 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4090 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4091 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4092 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4093 | #endif |
| 4094 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4095 | || ctx->ctx_res_w == RES_DONE |
| 4096 | || ctx->ctx_res_w == RES_FOR |
| 4097 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4098 | #endif |
| 4099 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4100 | || ctx->ctx_res_w == RES_ESAC |
| 4101 | #endif |
| 4102 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4103 | struct pipe *new_p; |
| 4104 | debug_printf_parse("done_pipe: adding new pipe: " |
| 4105 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 4106 | not_null, ctx->ctx_res_w); |
| 4107 | new_p = new_pipe(); |
| 4108 | ctx->pipe->next = new_p; |
| 4109 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4110 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4111 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4112 | * This is used to control execution. |
| 4113 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 4114 | * cases where variable or value happens to match a keyword): |
| 4115 | */ |
| 4116 | #if ENABLE_HUSH_LOOPS |
| 4117 | if (ctx->ctx_res_w == RES_FOR |
| 4118 | || ctx->ctx_res_w == RES_IN) |
| 4119 | ctx->ctx_res_w = RES_NONE; |
| 4120 | #endif |
| 4121 | #if ENABLE_HUSH_CASE |
| 4122 | if (ctx->ctx_res_w == RES_MATCH) |
| 4123 | ctx->ctx_res_w = RES_CASEI; |
| 4124 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4125 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4126 | /* Create the memory for command, roughly: |
| 4127 | * ctx->pipe->cmds = new struct command; |
| 4128 | * ctx->command = &ctx->pipe->cmds[0]; |
| 4129 | */ |
| 4130 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4131 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4132 | } |
| 4133 | debug_printf_parse("done_pipe return\n"); |
| 4134 | } |
| 4135 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4136 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4137 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4138 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4139 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4140 | /* Create the memory for command, roughly: |
| 4141 | * ctx->pipe->cmds = new struct command; |
| 4142 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4143 | */ |
| 4144 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4145 | } |
| 4146 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4147 | /* If a reserved word is found and processed, parse context is modified |
| 4148 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4149 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4150 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4151 | struct reserved_combo { |
| 4152 | char literal[6]; |
| 4153 | unsigned char res; |
| 4154 | unsigned char assignment_flag; |
| 4155 | int flag; |
| 4156 | }; |
| 4157 | enum { |
| 4158 | FLAG_END = (1 << RES_NONE ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4159 | #if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4160 | FLAG_IF = (1 << RES_IF ), |
| 4161 | FLAG_THEN = (1 << RES_THEN ), |
| 4162 | FLAG_ELIF = (1 << RES_ELIF ), |
| 4163 | FLAG_ELSE = (1 << RES_ELSE ), |
| 4164 | FLAG_FI = (1 << RES_FI ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4165 | #endif |
| 4166 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4167 | FLAG_FOR = (1 << RES_FOR ), |
| 4168 | FLAG_WHILE = (1 << RES_WHILE), |
| 4169 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 4170 | FLAG_DO = (1 << RES_DO ), |
| 4171 | FLAG_DONE = (1 << RES_DONE ), |
| 4172 | FLAG_IN = (1 << RES_IN ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4173 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4174 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4175 | FLAG_MATCH = (1 << RES_MATCH), |
| 4176 | FLAG_ESAC = (1 << RES_ESAC ), |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4177 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4178 | FLAG_START = (1 << RES_XXXX ), |
| 4179 | }; |
| 4180 | |
| 4181 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 4182 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4183 | /* Mostly a list of accepted follow-up reserved words. |
| 4184 | * FLAG_END means we are done with the sequence, and are ready |
| 4185 | * to turn the compound list into a command. |
| 4186 | * FLAG_START means the word must start a new compound list. |
| 4187 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4188 | static const struct reserved_combo reserved_list[] = { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4189 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4190 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 4191 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 4192 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 4193 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 4194 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 4195 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4196 | #endif |
| 4197 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4198 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 4199 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 4200 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 4201 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 4202 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 4203 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4204 | #endif |
| 4205 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4206 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 4207 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4208 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4209 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4210 | const struct reserved_combo *r; |
| 4211 | |
| 4212 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 4213 | if (strcmp(word->data, r->literal) == 0) |
| 4214 | return r; |
| 4215 | } |
| 4216 | return NULL; |
| 4217 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4218 | /* Return 0: not a keyword, 1: keyword |
| 4219 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4220 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 4221 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4222 | #if ENABLE_HUSH_CASE |
| 4223 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4224 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4225 | }; |
| 4226 | #endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4227 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4228 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4229 | if (word->o_quoted) |
| 4230 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4231 | r = match_reserved_word(word); |
| 4232 | if (!r) |
| 4233 | return 0; |
| 4234 | |
| 4235 | 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] | 4236 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4237 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE) |
| 4238 | /* "case word IN ..." - IN part starts first match part */ |
| 4239 | r = &reserved_match; |
| 4240 | else |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4241 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4242 | if (r->flag == 0) { /* '!' */ |
| 4243 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4244 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4245 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4246 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4247 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4248 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4249 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4250 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4251 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4252 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4253 | old = xmalloc(sizeof(*old)); |
| 4254 | debug_printf_parse("push stack %p\n", old); |
| 4255 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4256 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4257 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4258 | } 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] | 4259 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4260 | ctx->ctx_res_w = RES_SNTX; |
| 4261 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4262 | } else { |
| 4263 | /* "{...} fi" is ok. "{...} if" is not |
| 4264 | * Example: |
| 4265 | * if { echo foo; } then { echo bar; } fi */ |
| 4266 | if (ctx->command->group) |
| 4267 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4268 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4269 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4270 | ctx->ctx_res_w = r->res; |
| 4271 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4272 | word->o_assignment = r->assignment_flag; |
| 4273 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4274 | if (ctx->old_flag & FLAG_END) { |
| 4275 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4276 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4277 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4278 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4279 | old = ctx->stack; |
| 4280 | old->command->group = ctx->list_head; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4281 | old->command->grp_type = GRP_NORMAL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4282 | #if !BB_MMU |
| 4283 | o_addstr(&old->as_string, ctx->as_string.data); |
| 4284 | o_free_unsafe(&ctx->as_string); |
| 4285 | old->command->group_as_string = xstrdup(old->as_string.data); |
| 4286 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 4287 | old->command->group_as_string); |
| 4288 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4289 | *ctx = *old; /* physical copy */ |
| 4290 | free(old); |
| 4291 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4292 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4293 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4294 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4295 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4296 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4297 | * Normal return is 0. Syntax errors return 1. |
| 4298 | * Note: on return, word is reset, but not o_free'd! |
| 4299 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4300 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4301 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4302 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4303 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4304 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4305 | if (word->length == 0 && word->o_quoted == 0) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4306 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 4307 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4308 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4309 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4310 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4311 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 4312 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4313 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4314 | * "2.7 Redirection |
| 4315 | * ...the word that follows the redirection operator |
| 4316 | * shall be subjected to tilde expansion, parameter expansion, |
| 4317 | * command substitution, arithmetic expansion, and quote |
| 4318 | * removal. Pathname expansion shall not be performed |
| 4319 | * on the word by a non-interactive shell; an interactive |
| 4320 | * shell may perform it, but shall do so only when |
| 4321 | * the expansion would result in one word." |
| 4322 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4323 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4324 | /* Cater for >\file case: |
| 4325 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 4326 | * Same with heredocs: |
| 4327 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 4328 | */ |
| 4329 | unbackslash(ctx->pending_redirect->rd_filename); |
| 4330 | /* Is it <<"HEREDOC"? */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4331 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC |
| 4332 | && word->o_quoted |
| 4333 | ) { |
| 4334 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 4335 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4336 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4337 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4338 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4339 | /* If this word wasn't an assignment, next ones definitely |
| 4340 | * can't be assignments. Even if they look like ones. */ |
| 4341 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 4342 | && word->o_assignment != WORD_IS_KEYWORD |
| 4343 | ) { |
| 4344 | word->o_assignment = NOT_ASSIGNMENT; |
| 4345 | } else { |
| 4346 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
| 4347 | command->assignment_cnt++; |
| 4348 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 4349 | } |
| 4350 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4351 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4352 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 4353 | if (ctx->ctx_dsemicolon |
| 4354 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 4355 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 4356 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4357 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 4358 | ctx->ctx_dsemicolon = 0; |
| 4359 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4360 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4361 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4362 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4363 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 4364 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4365 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4366 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4367 | debug_printf_parse("checking '%s' for reserved-ness\n", word->data); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4368 | if (reserved_word(word, ctx)) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4369 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4370 | debug_printf_parse("done_word return %d\n", |
| 4371 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4372 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4373 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4374 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4375 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4376 | if (command->group) { |
| 4377 | /* "{ echo foo; } echo bar" - bad */ |
| 4378 | syntax_error_at(word->data); |
| 4379 | debug_printf_parse("done_word return 1: syntax error, " |
| 4380 | "groups and arglists don't mix\n"); |
| 4381 | return 1; |
| 4382 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4383 | 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] | 4384 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 4385 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4386 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4387 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4388 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 4389 | char *p = word->data; |
| 4390 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 4391 | && (p[1] & 0x7f) == '@' |
| 4392 | && p[2] == SPECIAL_VAR_SYMBOL |
| 4393 | ) { |
| 4394 | p += 3; |
| 4395 | } |
| 4396 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4397 | /* saw no "$@", or not only "$@" but some |
| 4398 | * real text is there too */ |
| 4399 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 4400 | * e.g. "", $empty"" etc to not disappear */ |
| 4401 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 4402 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 4403 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 4404 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4405 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4406 | //SEGV, but good idea. |
| 4407 | // command->argv = add_string_to_strings(command->argv, word->data); |
| 4408 | // word->data = NULL; |
| 4409 | // word->length = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4410 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4411 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4412 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4413 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4414 | if (ctx->ctx_res_w == RES_FOR) { |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4415 | if (word->o_quoted |
| 4416 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 4417 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4418 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4419 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4420 | return 1; |
| 4421 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4422 | /* Force FOR to have just one word (variable name) */ |
| 4423 | /* NB: basically, this makes hush see "for v in ..." |
| 4424 | * syntax as if it is "for v; in ...". FOR and IN become |
| 4425 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4426 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4427 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4428 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4429 | #if ENABLE_HUSH_CASE |
| 4430 | /* Force CASE to have just one word */ |
| 4431 | if (ctx->ctx_res_w == RES_CASE) { |
| 4432 | done_pipe(ctx, PIPE_SEQ); |
| 4433 | } |
| 4434 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4435 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4436 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4437 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4438 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4439 | return 0; |
| 4440 | } |
| 4441 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4442 | |
| 4443 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 4444 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4445 | * Return: |
| 4446 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 4447 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 4448 | * REDIRFD_TO_FILE if no & was seen, |
| 4449 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4450 | */ |
| 4451 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4452 | #define parse_redir_right_fd(as_string, input) \ |
| 4453 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4454 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4455 | 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] | 4456 | { |
| 4457 | int ch, d, ok; |
| 4458 | |
| 4459 | ch = i_peek(input); |
| 4460 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4461 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4462 | |
| 4463 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4464 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4465 | ch = i_peek(input); |
| 4466 | if (ch == '-') { |
| 4467 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4468 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4469 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4470 | } |
| 4471 | d = 0; |
| 4472 | ok = 0; |
| 4473 | while (ch != EOF && isdigit(ch)) { |
| 4474 | d = d*10 + (ch-'0'); |
| 4475 | ok = 1; |
| 4476 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4477 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4478 | ch = i_peek(input); |
| 4479 | } |
| 4480 | if (ok) return d; |
| 4481 | |
| 4482 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 4483 | |
| 4484 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4485 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4488 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4489 | */ |
| 4490 | static int parse_redirect(struct parse_context *ctx, |
| 4491 | int fd, |
| 4492 | redir_type style, |
| 4493 | struct in_str *input) |
| 4494 | { |
| 4495 | struct command *command = ctx->command; |
| 4496 | struct redir_struct *redir; |
| 4497 | struct redir_struct **redirp; |
| 4498 | int dup_num; |
| 4499 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4500 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4501 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4502 | /* Check for a '>&1' type redirect */ |
| 4503 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 4504 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 4505 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4506 | } else { |
| 4507 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4508 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4509 | if (dup_num) { /* <<-... */ |
| 4510 | ch = i_getch(input); |
| 4511 | nommu_addchr(&ctx->as_string, ch); |
| 4512 | ch = i_peek(input); |
| 4513 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4514 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4515 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4516 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4517 | int ch = i_peek(input); |
| 4518 | if (ch == '|') { |
| 4519 | /* >|FILE redirect ("clobbering" >). |
| 4520 | * Since we do not support "set -o noclobber" yet, |
| 4521 | * >| and > are the same for now. Just eat |. |
| 4522 | */ |
| 4523 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4524 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4525 | } |
| 4526 | } |
| 4527 | |
| 4528 | /* Create a new redir_struct and append it to the linked list */ |
| 4529 | redirp = &command->redirects; |
| 4530 | while ((redir = *redirp) != NULL) { |
| 4531 | redirp = &(redir->next); |
| 4532 | } |
| 4533 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 4534 | /* redir->next = NULL; */ |
| 4535 | /* redir->rd_filename = NULL; */ |
| 4536 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4537 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4538 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4539 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 4540 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4541 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4542 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4543 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4544 | /* Erik had a check here that the file descriptor in question |
| 4545 | * is legit; I postpone that to "run time" |
| 4546 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4547 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 4548 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4549 | } else { |
| 4550 | /* Set ctx->pending_redirect, so we know what to do at the |
| 4551 | * end of the next parsed word. */ |
| 4552 | ctx->pending_redirect = redir; |
| 4553 | } |
| 4554 | return 0; |
| 4555 | } |
| 4556 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4557 | /* If a redirect is immediately preceded by a number, that number is |
| 4558 | * supposed to tell which file descriptor to redirect. This routine |
| 4559 | * looks for such preceding numbers. In an ideal world this routine |
| 4560 | * needs to handle all the following classes of redirects... |
| 4561 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 4562 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 4563 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 4564 | * 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] | 4565 | * |
| 4566 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4567 | * "2.7 Redirection |
| 4568 | * ... If n is quoted, the number shall not be recognized as part of |
| 4569 | * the redirection expression. For example: |
| 4570 | * echo \2>a |
| 4571 | * writes the character 2 into file a" |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4572 | * We are getting it right by setting ->o_quoted on any \<char> |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4573 | * |
| 4574 | * A -1 return means no valid number was found, |
| 4575 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4576 | */ |
| 4577 | static int redirect_opt_num(o_string *o) |
| 4578 | { |
| 4579 | int num; |
| 4580 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4581 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4582 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4583 | num = bb_strtou(o->data, NULL, 10); |
| 4584 | if (errno || num < 0) |
| 4585 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4586 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4587 | return num; |
| 4588 | } |
| 4589 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4590 | #if BB_MMU |
| 4591 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 4592 | fetch_till_str(input, word, skip_tabs) |
| 4593 | #endif |
| 4594 | static char *fetch_till_str(o_string *as_string, |
| 4595 | struct in_str *input, |
| 4596 | const char *word, |
| 4597 | int skip_tabs) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4598 | { |
| 4599 | o_string heredoc = NULL_O_STRING; |
| 4600 | int past_EOL = 0; |
| 4601 | int ch; |
| 4602 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4603 | goto jump_in; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4604 | while (1) { |
| 4605 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4606 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4607 | if (ch == '\n') { |
| 4608 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 4609 | heredoc.data[past_EOL] = '\0'; |
| 4610 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 4611 | return heredoc.data; |
| 4612 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4613 | do { |
| 4614 | o_addchr(&heredoc, ch); |
| 4615 | past_EOL = heredoc.length; |
| 4616 | jump_in: |
| 4617 | do { |
| 4618 | ch = i_getch(input); |
| 4619 | nommu_addchr(as_string, ch); |
| 4620 | } while (skip_tabs && ch == '\t'); |
| 4621 | } while (ch == '\n'); |
| 4622 | } |
| 4623 | if (ch == EOF) { |
| 4624 | o_free_unsafe(&heredoc); |
| 4625 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4626 | } |
| 4627 | o_addchr(&heredoc, ch); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4628 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4629 | } |
| 4630 | } |
| 4631 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4632 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 4633 | * and load them all. There should be exactly heredoc_cnt of them. |
| 4634 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4635 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 4636 | { |
| 4637 | struct pipe *pi = ctx->list_head; |
| 4638 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4639 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4640 | int i; |
| 4641 | struct command *cmd = pi->cmds; |
| 4642 | |
| 4643 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 4644 | pi->num_cmds, |
| 4645 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 4646 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4647 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4648 | |
| 4649 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 4650 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4651 | while (redir) { |
| 4652 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4653 | char *p; |
| 4654 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4655 | redir->rd_type = REDIRECT_HEREDOC2; |
| 4656 | /* redir->dup is (ab)used to indicate <<- */ |
| 4657 | p = fetch_till_str(&ctx->as_string, input, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4658 | redir->rd_filename, redir->rd_dup & HEREDOC_SKIPTABS); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4659 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4660 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4661 | return 1; |
| 4662 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4663 | free(redir->rd_filename); |
| 4664 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4665 | heredoc_cnt--; |
| 4666 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4667 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4668 | } |
| 4669 | cmd++; |
| 4670 | } |
| 4671 | pi = pi->next; |
| 4672 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4673 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4674 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4675 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4676 | bb_error_msg_and_die("heredoc BUG 2"); |
| 4677 | #endif |
| 4678 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4679 | } |
| 4680 | |
| 4681 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4682 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4683 | static FILE *generate_stream_from_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4684 | { |
| 4685 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4686 | int pid, channel[2]; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4687 | #if !BB_MMU |
| 4688 | char **to_free; |
| 4689 | #endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 4690 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 4691 | xpipe(channel); |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 4692 | pid = BB_MMU ? fork() : vfork(); |
| 4693 | if (pid < 0) |
| 4694 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4695 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4696 | if (pid == 0) { /* child */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4697 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 4698 | /* Process substitution is not considered to be usual |
| 4699 | * 'command execution'. |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 4700 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 4701 | */ |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 4702 | bb_signals(0 |
| 4703 | + (1 << SIGTSTP) |
| 4704 | + (1 << SIGTTIN) |
| 4705 | + (1 << SIGTTOU) |
| 4706 | , SIG_IGN); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4707 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 4708 | xmove_fd(channel[1], 1); |
| 4709 | /* Prevent it from trying to handle ctrl-z etc */ |
| 4710 | USE_HUSH_JOB(G.run_list_level = 1;) |
| 4711 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4712 | reset_traps_to_defaults(); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4713 | parse_and_run_string(s); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4714 | _exit(G.last_exitcode); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4715 | #else |
| 4716 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 4717 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 4718 | * huge=`cat BIG` # was blocking here forever |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4719 | * echo OK |
| 4720 | */ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4721 | re_execute_shell(&to_free, |
| 4722 | s, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4723 | G.global_argv[0], |
| 4724 | G.global_argv + 1); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4725 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4726 | } |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4727 | |
| 4728 | /* parent */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4729 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4730 | #if !BB_MMU |
| 4731 | free(to_free); |
| 4732 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4733 | close(channel[1]); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 4734 | pf = fdopen(channel[0], "r"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4735 | return pf; |
| 4736 | } |
| 4737 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4738 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4739 | static int process_command_subs(o_string *dest, const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4740 | { |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4741 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4742 | struct in_str pipe_str; |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4743 | int ch, eol_cnt; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 4744 | |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4745 | pf = generate_stream_from_string(s); |
| 4746 | if (pf == NULL) |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4747 | return 1; |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4748 | close_on_exec_on(fileno(pf)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4749 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4750 | /* Now send results of command back into original context */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4751 | setup_file_in_str(&pipe_str, pf); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 4752 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4753 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 4754 | if (ch == '\n') { |
| 4755 | eol_cnt++; |
| 4756 | continue; |
| 4757 | } |
| 4758 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4759 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 4760 | eol_cnt--; |
| 4761 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 4762 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4763 | } |
| 4764 | |
| 4765 | debug_printf("done reading from pipe, pclose()ing\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4766 | /* Note: we got EOF, and we just close the read end of the pipe. |
| 4767 | * 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] | 4768 | * Try these: |
| 4769 | * echo `echo Hi; exec 1>&-; sleep 2` - bash waits 2 sec |
| 4770 | * `false`; echo $? - bash outputs "1" |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4771 | */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4772 | fclose(pf); |
| 4773 | debug_printf("closed FILE from child. return 0\n"); |
| 4774 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4775 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4776 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4777 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4778 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4779 | struct in_str *input, int ch) |
| 4780 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4781 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4782 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4783 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4784 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4785 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4786 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4787 | |
| 4788 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4789 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4790 | if (ch == '(' && !dest->o_quoted) { |
| 4791 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4792 | if (done_word(dest, ctx)) |
| 4793 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4794 | if (!command->argv) |
| 4795 | goto skip; /* (... */ |
| 4796 | if (command->argv[1]) { /* word word ... (... */ |
| 4797 | syntax_error_unexpected_ch('('); |
| 4798 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4799 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4800 | /* it is "word(..." or "word (..." */ |
| 4801 | do |
| 4802 | ch = i_getch(input); |
| 4803 | while (ch == ' ' || ch == '\t'); |
| 4804 | if (ch != ')') { |
| 4805 | syntax_error_unexpected_ch(ch); |
| 4806 | return 1; |
| 4807 | } |
| 4808 | nommu_addchr(&ctx->as_string, ch); |
| 4809 | do |
| 4810 | ch = i_getch(input); |
| 4811 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 4812 | if (ch != '{') { |
| 4813 | syntax_error_unexpected_ch(ch); |
| 4814 | return 1; |
| 4815 | } |
| 4816 | nommu_addchr(&ctx->as_string, ch); |
| 4817 | command->grp_type = GRP_FUNCTION; |
| 4818 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4819 | } |
| 4820 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4821 | if (command->argv /* word [word]{... */ |
| 4822 | || dest->length /* word{... */ |
| 4823 | || dest->o_quoted /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4824 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4825 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4826 | debug_printf_parse("parse_group return 1: " |
| 4827 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4828 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4829 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4830 | |
| 4831 | #if ENABLE_HUSH_FUNCTIONS |
| 4832 | skip: |
| 4833 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4834 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4835 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4836 | endch = ')'; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4837 | command->grp_type = GRP_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4838 | } else { |
| 4839 | /* bash does not allow "{echo...", requires whitespace */ |
| 4840 | ch = i_getch(input); |
| 4841 | if (ch != ' ' && ch != '\t' && ch != '\n') { |
| 4842 | syntax_error_unexpected_ch(ch); |
| 4843 | return 1; |
| 4844 | } |
| 4845 | nommu_addchr(&ctx->as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4846 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4847 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4848 | { |
| 4849 | #if !BB_MMU |
| 4850 | char *as_string = NULL; |
| 4851 | #endif |
| 4852 | pipe_list = parse_stream(&as_string, input, endch); |
| 4853 | #if !BB_MMU |
| 4854 | if (as_string) |
| 4855 | o_addstr(&ctx->as_string, as_string); |
| 4856 | #endif |
| 4857 | /* empty ()/{} or parse error? */ |
| 4858 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4859 | /* parse_stream already emitted error msg */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4860 | #if !BB_MMU |
| 4861 | free(as_string); |
| 4862 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4863 | debug_printf_parse("parse_group return 1: " |
| 4864 | "parse_stream returned %p\n", pipe_list); |
| 4865 | return 1; |
| 4866 | } |
| 4867 | command->group = pipe_list; |
| 4868 | #if !BB_MMU |
| 4869 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 4870 | command->group_as_string = as_string; |
| 4871 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 4872 | command->group_as_string); |
| 4873 | #endif |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4874 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4875 | debug_printf_parse("parse_group return 0\n"); |
| 4876 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4877 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4878 | } |
| 4879 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4880 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4881 | /* Subroutines for copying $(...) and `...` things */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4882 | static void add_till_backquote(o_string *dest, struct in_str *input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4883 | /* '...' */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4884 | 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] | 4885 | { |
| 4886 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4887 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4888 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4889 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4890 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4891 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4892 | if (ch == '\'') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4893 | return; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4894 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4895 | } |
| 4896 | } |
| 4897 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4898 | 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] | 4899 | { |
| 4900 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4901 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4902 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4903 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4904 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4905 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4906 | if (ch == '"') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4907 | return; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4908 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4909 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4910 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4911 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4912 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4913 | if (ch == '`') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4914 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4915 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4916 | continue; |
| 4917 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4918 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4919 | } |
| 4920 | } |
| 4921 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 4922 | * \` quoting. |
| 4923 | * "Within the backquoted style of command substitution, backslash |
| 4924 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 4925 | * The search for the matching backquote shall be satisfied by the first |
| 4926 | * backquote found without a preceding backslash; during this search, |
| 4927 | * if a non-escaped backquote is encountered within a shell comment, |
| 4928 | * a here-document, an embedded command substitution of the $(command) |
| 4929 | * form, or a quoted string, undefined results occur. A single-quoted |
| 4930 | * or double-quoted string that begins, but does not end, within the |
| 4931 | * "`...`" sequence produces undefined results." |
| 4932 | * Example Output |
| 4933 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 4934 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4935 | static void add_till_backquote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4936 | { |
| 4937 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4938 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4939 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4940 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4941 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4942 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4943 | if (ch == '`') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4944 | return; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4945 | if (ch == '\\') { |
| 4946 | /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4947 | int ch2 = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4948 | if (ch2 == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4949 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4950 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4951 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4952 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4953 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4954 | ch = ch2; |
| 4955 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4956 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4957 | } |
| 4958 | } |
| 4959 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 4960 | * quoting and nested ()s. |
| 4961 | * "With the $(command) style of command substitution, all characters |
| 4962 | * following the open parenthesis to the matching closing parenthesis |
| 4963 | * constitute the command. Any valid shell script can be used for command, |
| 4964 | * except a script consisting solely of redirections which produces |
| 4965 | * unspecified results." |
| 4966 | * Example Output |
| 4967 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 4968 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 4969 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 4970 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4971 | 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] | 4972 | { |
| 4973 | int count = 0; |
| 4974 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4975 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4976 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4977 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4978 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4979 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4980 | if (ch == '(') |
| 4981 | count++; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4982 | if (ch == ')') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4983 | if (--count < 0) { |
| 4984 | if (!dbl) |
| 4985 | break; |
| 4986 | if (i_peek(input) == ')') { |
| 4987 | i_getch(input); |
| 4988 | break; |
| 4989 | } |
| 4990 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4991 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4992 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4993 | if (ch == '\'') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4994 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4995 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4996 | continue; |
| 4997 | } |
| 4998 | if (ch == '"') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4999 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5000 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5001 | continue; |
| 5002 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5003 | if (ch == '\\') { |
| 5004 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5005 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5006 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5007 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5008 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5009 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5010 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5011 | continue; |
| 5012 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5013 | } |
| 5014 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5015 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5016 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5017 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5018 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5019 | #define handle_dollar(as_string, dest, input) \ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5020 | handle_dollar(dest, input) |
| 5021 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5022 | static int handle_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5023 | o_string *dest, |
| 5024 | struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5025 | { |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 5026 | int expansion; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5027 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5028 | unsigned char quote_mask = dest->o_escape ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5029 | |
| 5030 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5031 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5032 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5033 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 5034 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5035 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5036 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5037 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5038 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5039 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5040 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5041 | if (!isalnum(ch) && ch != '_') |
| 5042 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5043 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5044 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5045 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5046 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5047 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5048 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5049 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5050 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5051 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5052 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5053 | o_addchr(dest, ch | quote_mask); |
| 5054 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5055 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5056 | case '$': /* pid */ |
| 5057 | case '!': /* last bg pid */ |
| 5058 | case '?': /* last exit code */ |
| 5059 | case '#': /* number of args */ |
| 5060 | case '*': /* args */ |
| 5061 | case '@': /* args */ |
| 5062 | goto make_one_char_var; |
| 5063 | case '{': { |
| 5064 | bool first_char, all_digits; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 5065 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5066 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5067 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5068 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 5069 | /* TODO: maybe someone will try to escape the '}' */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5070 | expansion = 0; |
| 5071 | first_char = true; |
| 5072 | all_digits = false; |
| 5073 | while (1) { |
| 5074 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5075 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5076 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5077 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5078 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5079 | if (first_char) { |
| 5080 | if (ch == '#') |
| 5081 | /* ${#var}: length of var contents */ |
| 5082 | goto char_ok; |
| 5083 | else if (isdigit(ch)) { |
| 5084 | all_digits = true; |
| 5085 | goto char_ok; |
| 5086 | } |
| 5087 | } |
| 5088 | |
| 5089 | if (expansion < 2 |
| 5090 | && ( (all_digits && !isdigit(ch)) |
| 5091 | || (!all_digits && !isalnum(ch) && ch != '_') |
| 5092 | ) |
| 5093 | ) { |
| 5094 | /* handle parameter expansions |
| 5095 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 5096 | */ |
| 5097 | if (first_char) |
| 5098 | goto case_default; |
| 5099 | switch (ch) { |
| 5100 | case ':': /* null modifier */ |
| 5101 | if (expansion == 0) { |
| 5102 | debug_printf_parse(": null modifier\n"); |
| 5103 | ++expansion; |
| 5104 | break; |
| 5105 | } |
| 5106 | goto case_default; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5107 | case '#': /* remove prefix */ |
| 5108 | case '%': /* remove suffix */ |
| 5109 | if (expansion == 0) { |
| 5110 | debug_printf_parse(": remove suffix/prefix\n"); |
| 5111 | expansion = 2; |
| 5112 | break; |
| 5113 | } |
| 5114 | goto case_default; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5115 | case '-': /* default value */ |
| 5116 | case '=': /* assign default */ |
| 5117 | case '+': /* alternative */ |
| 5118 | case '?': /* error indicate */ |
| 5119 | debug_printf_parse(": parameter expansion\n"); |
| 5120 | expansion = 2; |
| 5121 | break; |
| 5122 | default: |
| 5123 | case_default: |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5124 | syntax_error_unterm_str("${name}"); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5125 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 5126 | return 1; |
| 5127 | } |
| 5128 | } |
| 5129 | char_ok: |
| 5130 | debug_printf_parse(": '%c'\n", ch); |
| 5131 | o_addchr(dest, ch | quote_mask); |
| 5132 | quote_mask = 0; |
| 5133 | first_char = false; |
| 5134 | } |
| 5135 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5136 | break; |
| 5137 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5138 | #if (ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK) |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5139 | case '(': { |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5140 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5141 | int pos; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5142 | # endif |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5143 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5144 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5145 | # if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5146 | if (i_peek(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5147 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5148 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5149 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5150 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5151 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5152 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5153 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5154 | add_till_closing_paren(dest, input, true); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5155 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5156 | if (as_string) { |
| 5157 | o_addstr(as_string, dest->data + pos); |
| 5158 | o_addchr(as_string, ')'); |
| 5159 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5160 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5161 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5162 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5163 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5164 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5165 | # endif |
| 5166 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5167 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5168 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5169 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5170 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5171 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5172 | add_till_closing_paren(dest, input, false); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5173 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5174 | if (as_string) { |
| 5175 | o_addstr(as_string, dest->data + pos); |
| 5176 | o_addchr(as_string, '`'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5177 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5178 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5179 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5180 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5181 | break; |
| 5182 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5183 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5184 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5185 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5186 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5187 | ch = i_peek(input); |
| 5188 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 5189 | ch = '_'; |
| 5190 | goto make_var; |
| 5191 | } |
| 5192 | /* else: it's $_ */ |
| 5193 | /* TODO: */ |
| 5194 | /* $_ Shell or shell script name; or last cmd name */ |
| 5195 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 5196 | default: |
| 5197 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5198 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5199 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5200 | return 0; |
| 5201 | } |
| 5202 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5203 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5204 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5205 | parse_stream_dquoted(dest, input, dquote_end) |
| 5206 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5207 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5208 | o_string *dest, |
| 5209 | struct in_str *input, |
| 5210 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5211 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5212 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5213 | int next; |
| 5214 | |
| 5215 | again: |
| 5216 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5217 | if (ch != EOF) |
| 5218 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5219 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5220 | if (dest->o_assignment == NOT_ASSIGNMENT) |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5221 | dest->o_escape ^= 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5222 | debug_printf_parse("parse_stream_dquoted return 0\n"); |
| 5223 | return 0; |
| 5224 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5225 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5226 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5227 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5228 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5229 | } |
| 5230 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5231 | if (ch != '\n') { |
| 5232 | next = i_peek(input); |
| 5233 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5234 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 5235 | ch, ch, dest->o_escape); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5236 | if (ch == '\\') { |
| 5237 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5238 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5239 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5240 | } |
| 5241 | /* bash: |
| 5242 | * "The backslash retains its special meaning [in "..."] |
| 5243 | * only when followed by one of the following characters: |
| 5244 | * $, `, ", \, or <newline>. A double quote may be quoted |
| 5245 | * within double quotes by preceding it with a backslash. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5246 | */ |
| 5247 | if (strchr("$`\"\\", next) != NULL) { |
| 5248 | o_addqchr(dest, i_getch(input)); |
| 5249 | } else { |
| 5250 | o_addqchr(dest, '\\'); |
| 5251 | } |
| 5252 | goto again; |
| 5253 | } |
| 5254 | if (ch == '$') { |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5255 | if (handle_dollar(as_string, dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5256 | debug_printf_parse("parse_stream_dquoted return 1: " |
| 5257 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5258 | return 1; |
| 5259 | } |
| 5260 | goto again; |
| 5261 | } |
| 5262 | #if ENABLE_HUSH_TICK |
| 5263 | if (ch == '`') { |
| 5264 | //int pos = dest->length; |
| 5265 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5266 | o_addchr(dest, 0x80 | '`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5267 | add_till_backquote(dest, input); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5268 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5269 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5270 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5271 | } |
| 5272 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5273 | o_addQchr(dest, ch); |
| 5274 | if (ch == '=' |
| 5275 | && (dest->o_assignment == MAYBE_ASSIGNMENT |
| 5276 | || dest->o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5277 | && is_well_formed_var_name(dest->data, '=') |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5278 | ) { |
| 5279 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 5280 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5281 | goto again; |
| 5282 | } |
| 5283 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5284 | /* |
| 5285 | * Scan input until EOF or end_trigger char. |
| 5286 | * Return a list of pipes to execute, or NULL on EOF |
| 5287 | * or if end_trigger character is met. |
| 5288 | * On syntax error, exit is shell is not interactive, |
| 5289 | * reset parsing machinery and start parsing anew, |
| 5290 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5291 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5292 | static struct pipe *parse_stream(char **pstring, |
| 5293 | struct in_str *input, |
| 5294 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5295 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5296 | struct parse_context ctx; |
| 5297 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5298 | int is_in_dquote; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5299 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5300 | |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5301 | /* Double-quote state is handled in the state variable is_in_dquote. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5302 | * 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] | 5303 | * found. When recursing, quote state is passed in via dest->o_escape. |
| 5304 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5305 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
| 5306 | end_trigger ? : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5307 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5308 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5309 | G.ifs = get_local_var_value("IFS"); |
| 5310 | if (G.ifs == NULL) |
| 5311 | G.ifs = " \t\n"; |
| 5312 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5313 | reset: |
| 5314 | #if ENABLE_HUSH_INTERACTIVE |
| 5315 | input->promptmode = 0; /* PS1 */ |
| 5316 | #endif |
| 5317 | /* dest.o_assignment = MAYBE_ASSIGNMENT; - already is */ |
| 5318 | initialize_context(&ctx); |
| 5319 | is_in_dquote = 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5320 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5321 | while (1) { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5322 | const char *is_ifs; |
| 5323 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5324 | int ch; |
| 5325 | int next; |
| 5326 | int redir_fd; |
| 5327 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5328 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5329 | if (is_in_dquote) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5330 | /* dest.o_quoted = 1; - already is (see below) */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5331 | if (parse_stream_dquoted(&ctx.as_string, &dest, input, '"')) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5332 | goto parse_error; |
| 5333 | } |
| 5334 | /* We reached closing '"' */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5335 | is_in_dquote = 0; |
| 5336 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5337 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5338 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 5339 | ch, ch, dest.o_escape); |
| 5340 | if (ch == EOF) { |
| 5341 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5342 | |
| 5343 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5344 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5345 | xfunc_die(); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5346 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5347 | if (done_word(&dest, &ctx)) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5348 | xfunc_die(); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5349 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5350 | o_free(&dest); |
| 5351 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5352 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5353 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5354 | /* (this makes bare "&" cmd a no-op. |
| 5355 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5356 | if (pi->num_cmds == 0 |
| 5357 | IF_HAS_KEYWORDS( && pi->res_word == RES_NONE) |
| 5358 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5359 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5360 | pi = NULL; |
| 5361 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5362 | #if !BB_MMU |
| 5363 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 5364 | if (pstring) |
| 5365 | *pstring = ctx.as_string.data; |
| 5366 | else |
| 5367 | o_free_unsafe(&ctx.as_string); |
| 5368 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5369 | debug_leave(); |
| 5370 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5371 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5372 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5373 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5374 | is_ifs = strchr(G.ifs, ch); |
| 5375 | is_special = strchr("<>;&|(){}#'" /* special outside of "str" */ |
| 5376 | "\\$\"" USE_HUSH_TICK("`") /* always special */ |
| 5377 | , ch); |
| 5378 | |
| 5379 | if (!is_special && !is_ifs) { /* ordinary char */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5380 | o_addQchr(&dest, ch); |
| 5381 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 5382 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5383 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5384 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5385 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5386 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5387 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5388 | continue; |
| 5389 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5390 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5391 | if (is_ifs) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5392 | if (done_word(&dest, &ctx)) { |
| 5393 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 5394 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5395 | if (ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5396 | #if ENABLE_HUSH_CASE |
| 5397 | /* "case ... in <newline> word) ..." - |
| 5398 | * newlines are ignored (but ';' wouldn't be) */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5399 | if (ctx.command->argv == NULL |
| 5400 | && ctx.ctx_res_w == RES_MATCH |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5401 | ) { |
| 5402 | continue; |
| 5403 | } |
| 5404 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5405 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5406 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5407 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 5408 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5409 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5410 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5411 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5412 | heredoc_cnt = 0; |
| 5413 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5414 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5415 | ch = ';'; |
Denis Vlasenko | 7c98612 | 2009-04-04 12:15:42 +0000 | [diff] [blame] | 5416 | /* note: if (is_ifs) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5417 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5418 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5419 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5420 | if (end_trigger && end_trigger == ch |
| 5421 | && (heredoc_cnt == 0 || end_trigger != ';') |
| 5422 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5423 | if (heredoc_cnt) { |
| 5424 | /* This is technically valid: |
| 5425 | * { cat <<HERE; }; echo Ok |
| 5426 | * heredoc |
| 5427 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5428 | * HERE |
| 5429 | * but we don't support this. |
| 5430 | * We require heredoc to be in enclosing {}/(), |
| 5431 | * if any. |
| 5432 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5433 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5434 | goto parse_error; |
| 5435 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5436 | if (done_word(&dest, &ctx)) { |
| 5437 | goto parse_error; |
| 5438 | } |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5439 | /* Disallow "{ cmd }" without semicolon or & */ |
| 5440 | //debug_printf_parse("null pi %d\n", IS_NULL_PIPE(ctx.pipe)) |
| 5441 | //debug_printf_parse("null cmd %d\n", IS_NULL_CMD(ctx.command)) |
| 5442 | if (ch == '}' |
| 5443 | && !(IS_NULL_PIPE(ctx.pipe) && IS_NULL_CMD(ctx.command)) |
| 5444 | ) { |
| 5445 | syntax_error_unexpected_ch(ch); |
| 5446 | goto parse_error; |
| 5447 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5448 | done_pipe(&ctx, PIPE_SEQ); |
| 5449 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5450 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5451 | if (!HAS_KEYWORDS |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5452 | 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] | 5453 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5454 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5455 | #if !BB_MMU |
| 5456 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 5457 | if (pstring) |
| 5458 | *pstring = ctx.as_string.data; |
| 5459 | else |
| 5460 | o_free_unsafe(&ctx.as_string); |
| 5461 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5462 | debug_leave(); |
| 5463 | debug_printf_parse("parse_stream return %p: " |
| 5464 | "end_trigger char found\n", |
| 5465 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5466 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5467 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5468 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5469 | if (is_ifs) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5470 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5471 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5472 | next = '\0'; |
| 5473 | if (ch != '\n') { |
| 5474 | next = i_peek(input); |
| 5475 | } |
| 5476 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5477 | /* Catch <, > before deciding whether this word is |
| 5478 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 5479 | switch (ch) { |
| 5480 | case '>': |
| 5481 | redir_fd = redirect_opt_num(&dest); |
| 5482 | if (done_word(&dest, &ctx)) { |
| 5483 | goto parse_error; |
| 5484 | } |
| 5485 | redir_style = REDIRECT_OVERWRITE; |
| 5486 | if (next == '>') { |
| 5487 | redir_style = REDIRECT_APPEND; |
| 5488 | ch = i_getch(input); |
| 5489 | nommu_addchr(&ctx.as_string, ch); |
| 5490 | } |
| 5491 | #if 0 |
| 5492 | else if (next == '(') { |
| 5493 | syntax_error(">(process) not supported"); |
| 5494 | goto parse_error; |
| 5495 | } |
| 5496 | #endif |
| 5497 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5498 | goto parse_error; |
| 5499 | continue; /* back to top of while (1) */ |
| 5500 | case '<': |
| 5501 | redir_fd = redirect_opt_num(&dest); |
| 5502 | if (done_word(&dest, &ctx)) { |
| 5503 | goto parse_error; |
| 5504 | } |
| 5505 | redir_style = REDIRECT_INPUT; |
| 5506 | if (next == '<') { |
| 5507 | redir_style = REDIRECT_HEREDOC; |
| 5508 | heredoc_cnt++; |
| 5509 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 5510 | ch = i_getch(input); |
| 5511 | nommu_addchr(&ctx.as_string, ch); |
| 5512 | } else if (next == '>') { |
| 5513 | redir_style = REDIRECT_IO; |
| 5514 | ch = i_getch(input); |
| 5515 | nommu_addchr(&ctx.as_string, ch); |
| 5516 | } |
| 5517 | #if 0 |
| 5518 | else if (next == '(') { |
| 5519 | syntax_error("<(process) not supported"); |
| 5520 | goto parse_error; |
| 5521 | } |
| 5522 | #endif |
| 5523 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5524 | goto parse_error; |
| 5525 | continue; /* back to top of while (1) */ |
| 5526 | } |
| 5527 | |
| 5528 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 5529 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 5530 | && !ctx.pending_redirect |
| 5531 | ) { |
| 5532 | /* ch is a special char and thus this word |
| 5533 | * cannot be an assignment */ |
| 5534 | dest.o_assignment = NOT_ASSIGNMENT; |
| 5535 | } |
| 5536 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5537 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5538 | case '#': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5539 | if (dest.length == 0) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5540 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5541 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5542 | if (ch == EOF || ch == '\n') |
| 5543 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5544 | i_getch(input); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5545 | /* note: we do not add it to &ctx.as_string */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5546 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5547 | nommu_addchr(&ctx.as_string, '\n'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5548 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5549 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5550 | } |
| 5551 | break; |
| 5552 | case '\\': |
| 5553 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5554 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5555 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5556 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5557 | o_addchr(&dest, '\\'); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5558 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5559 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5560 | o_addchr(&dest, ch); |
| 5561 | /* Example: echo Hello \2>file |
| 5562 | * we need to know that word 2 is quoted */ |
| 5563 | dest.o_quoted = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5564 | break; |
| 5565 | case '$': |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5566 | if (handle_dollar(&ctx.as_string, &dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5567 | debug_printf_parse("parse_stream parse error: " |
| 5568 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5569 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5570 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5571 | break; |
| 5572 | case '\'': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5573 | dest.o_quoted = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5574 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5575 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5576 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5577 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5578 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5579 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5580 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5581 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5582 | break; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5583 | if (dest.o_assignment == NOT_ASSIGNMENT) |
| 5584 | o_addqchr(&dest, ch); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5585 | else |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5586 | o_addchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5587 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5588 | break; |
| 5589 | case '"': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5590 | dest.o_quoted = 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5591 | is_in_dquote ^= 1; /* invert */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5592 | if (dest.o_assignment == NOT_ASSIGNMENT) |
| 5593 | dest.o_escape ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5594 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5595 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5596 | case '`': { |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5597 | #if !BB_MMU |
| 5598 | int pos; |
| 5599 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5600 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5601 | o_addchr(&dest, '`'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5602 | #if !BB_MMU |
| 5603 | pos = dest.length; |
| 5604 | #endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5605 | add_till_backquote(&dest, input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5606 | #if !BB_MMU |
| 5607 | o_addstr(&ctx.as_string, dest.data + pos); |
| 5608 | o_addchr(&ctx.as_string, '`'); |
| 5609 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5610 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5611 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5612 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5613 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5614 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5615 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5616 | #if ENABLE_HUSH_CASE |
| 5617 | case_semi: |
| 5618 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5619 | if (done_word(&dest, &ctx)) { |
| 5620 | goto parse_error; |
| 5621 | } |
| 5622 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5623 | #if ENABLE_HUSH_CASE |
| 5624 | /* Eat multiple semicolons, detect |
| 5625 | * whether it means something special */ |
| 5626 | while (1) { |
| 5627 | ch = i_peek(input); |
| 5628 | if (ch != ';') |
| 5629 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5630 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5631 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5632 | if (ctx.ctx_res_w == RES_CASEI) { |
| 5633 | ctx.ctx_dsemicolon = 1; |
| 5634 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5635 | break; |
| 5636 | } |
| 5637 | } |
| 5638 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5639 | new_cmd: |
| 5640 | /* We just finished a cmd. New one may start |
| 5641 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5642 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5643 | break; |
| 5644 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5645 | if (done_word(&dest, &ctx)) { |
| 5646 | goto parse_error; |
| 5647 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5648 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5649 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5650 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5651 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5652 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5653 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5654 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5655 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5656 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5657 | if (done_word(&dest, &ctx)) { |
| 5658 | goto parse_error; |
| 5659 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5660 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5661 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5662 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5663 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5664 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5665 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5666 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5667 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5668 | } else { |
| 5669 | /* we could pick up a file descriptor choice here |
| 5670 | * with redirect_opt_num(), but bash doesn't do it. |
| 5671 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5672 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5673 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5674 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5675 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5676 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5677 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5678 | if (ctx.ctx_res_w == RES_MATCH |
| 5679 | && ctx.command->argv == NULL /* not (word|(... */ |
| 5680 | && dest.length == 0 /* not word(... */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5681 | && dest.o_quoted == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5682 | ) { |
| 5683 | continue; |
| 5684 | } |
| 5685 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5686 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5687 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 5688 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5689 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5690 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5691 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5692 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5693 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5694 | goto case_semi; |
| 5695 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5696 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5697 | /* proper use of this character is caught by end_trigger: |
| 5698 | * if we see {, we call parse_group(..., end_trigger='}') |
| 5699 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5700 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5701 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5702 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5703 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 5704 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5705 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5706 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5707 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5708 | parse_error: |
| 5709 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5710 | struct parse_context *pctx; |
| 5711 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5712 | |
| 5713 | /* Clean up allocated tree. |
| 5714 | * Samples for finding leaks on syntax error recovery path. |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5715 | * Run them from interactive shell, watch pmap `pidof hush`. |
| 5716 | * while if false; then false; fi do break; done |
| 5717 | * (bash accepts it) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5718 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 5719 | * Samples to catch leaks at execution: |
| 5720 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 5721 | * 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] | 5722 | */ |
| 5723 | pctx = &ctx; |
| 5724 | do { |
| 5725 | /* Update pipe/command counts, |
| 5726 | * otherwise freeing may miss some */ |
| 5727 | done_pipe(pctx, PIPE_SEQ); |
| 5728 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 5729 | pctx->list_head, pctx); |
| 5730 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5731 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5732 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5733 | #if !BB_MMU |
| 5734 | o_free_unsafe(&pctx->as_string); |
| 5735 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5736 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5737 | if (pctx != &ctx) { |
| 5738 | free(pctx); |
| 5739 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5740 | IF_HAS_KEYWORDS(pctx = p2;) |
| 5741 | } while (HAS_KEYWORDS && pctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5742 | /* Free text, clear all dest fields */ |
| 5743 | o_free(&dest); |
| 5744 | /* If we are not in top-level parse, we return, |
| 5745 | * our caller will propagate error. |
| 5746 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5747 | if (end_trigger != ';') { |
| 5748 | #if !BB_MMU |
| 5749 | if (pstring) |
| 5750 | *pstring = NULL; |
| 5751 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5752 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5753 | return ERR_PTR; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5754 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5755 | /* Discard cached input, force prompt */ |
| 5756 | input->p = NULL; |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5757 | USE_HUSH_INTERACTIVE(input->promptme = 1;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5758 | goto reset; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5759 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5760 | } |
| 5761 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5762 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5763 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 5764 | * end_trigger controls how often we stop parsing |
| 5765 | * NUL: parse all, execute, return |
| 5766 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 5767 | */ |
| 5768 | 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] | 5769 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5770 | while (1) { |
| 5771 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5772 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5773 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5774 | if (!pipe_list) /* EOF */ |
| 5775 | break; |
| 5776 | debug_print_tree(pipe_list, 0); |
| 5777 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 5778 | run_and_free_list(pipe_list); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5779 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5780 | } |
| 5781 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5782 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5783 | { |
| 5784 | struct in_str input; |
| 5785 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5786 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5787 | } |
| 5788 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5789 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5790 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5791 | struct in_str input; |
| 5792 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5793 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5794 | } |
| 5795 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5796 | /* Called a few times only (or even once if "sh -c") */ |
| 5797 | static void block_signals(int second_time) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 5798 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5799 | unsigned sig; |
| 5800 | unsigned mask; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 5801 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5802 | mask = (1 << SIGQUIT); |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 5803 | if (G_interactive_fd) |
| 5804 | mask = (1 << SIGQUIT) | SPECIAL_INTERACTIVE_SIGS; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5805 | G.non_DFL_mask = mask; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 5806 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5807 | if (!second_time) |
| 5808 | sigprocmask(SIG_SETMASK, NULL, &G.blocked_set); |
| 5809 | sig = 0; |
| 5810 | while (mask) { |
| 5811 | if (mask & 1) |
| 5812 | sigaddset(&G.blocked_set, sig); |
| 5813 | mask >>= 1; |
| 5814 | sig++; |
| 5815 | } |
| 5816 | sigdelset(&G.blocked_set, SIGCHLD); |
| 5817 | |
| 5818 | sigprocmask(SIG_SETMASK, &G.blocked_set, |
| 5819 | second_time ? NULL : &G.inherited_set); |
| 5820 | /* POSIX allows shell to re-enable SIGCHLD |
| 5821 | * even if it was SIG_IGN on entry */ |
| 5822 | // G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 5823 | if (!second_time) |
| 5824 | signal(SIGCHLD, SIG_DFL); // SIGCHLD_handler); |
| 5825 | } |
| 5826 | |
| 5827 | #if ENABLE_HUSH_JOB |
| 5828 | /* helper */ |
| 5829 | static void maybe_set_to_sigexit(int sig) |
| 5830 | { |
| 5831 | void (*handler)(int); |
| 5832 | /* non_DFL_mask'ed signals are, well, masked, |
| 5833 | * no need to set handler for them. |
| 5834 | */ |
| 5835 | if (!((G.non_DFL_mask >> sig) & 1)) { |
| 5836 | handler = signal(sig, sigexit); |
| 5837 | if (handler == SIG_IGN) /* oops... restore back to IGN! */ |
| 5838 | signal(sig, handler); |
| 5839 | } |
| 5840 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 5841 | /* Set handlers to restore tty pgrp and exit */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5842 | static void set_fatal_handlers(void) |
| 5843 | { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 5844 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5845 | if (HUSH_DEBUG) { |
| 5846 | maybe_set_to_sigexit(SIGILL ); |
| 5847 | maybe_set_to_sigexit(SIGFPE ); |
| 5848 | maybe_set_to_sigexit(SIGBUS ); |
| 5849 | maybe_set_to_sigexit(SIGSEGV); |
| 5850 | maybe_set_to_sigexit(SIGTRAP); |
| 5851 | } /* else: hush is perfect. what SEGV? */ |
| 5852 | maybe_set_to_sigexit(SIGABRT); |
| 5853 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 5854 | maybe_set_to_sigexit(SIGPIPE); |
| 5855 | maybe_set_to_sigexit(SIGALRM); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 5856 | //TODO: disable and move down when proper SIGHUP handling is added |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5857 | maybe_set_to_sigexit(SIGHUP ); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 5858 | /* if we are interactive, [SIGHUP,] SIGTERM and SIGINT are masked. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5859 | * if we aren't interactive... but in this case |
| 5860 | * we never want to restore pgrp on exit, and this fn is not called */ |
| 5861 | /*maybe_set_to_sigexit(SIGTERM);*/ |
| 5862 | /*maybe_set_to_sigexit(SIGINT );*/ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 5863 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 5864 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 5865 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5866 | static int set_mode(const char cstate, const char mode) |
| 5867 | { |
| 5868 | int state = (cstate == '-' ? 1 : 0); |
| 5869 | switch (mode) { |
| 5870 | case 'n': G.fake_mode = state; break; |
| 5871 | case 'x': /*G.debug_mode = state;*/ break; |
| 5872 | default: return EXIT_FAILURE; |
| 5873 | } |
| 5874 | return EXIT_SUCCESS; |
| 5875 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5876 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 5877 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 5878 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5879 | { |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 5880 | static const struct variable const_shell_ver = { |
| 5881 | .next = NULL, |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 5882 | .varstr = (char*)hush_version_str, |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 5883 | .max_len = 1, /* 0 can provoke free(name) */ |
| 5884 | .flg_export = 1, |
| 5885 | .flg_read_only = 1, |
| 5886 | }; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5887 | int signal_mask_is_inited = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5888 | int opt; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 5889 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 5890 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 5891 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 5892 | INIT_G(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 5893 | if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5894 | G.last_exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 5895 | #if !BB_MMU |
| 5896 | G.argv0_for_re_execing = argv[0]; |
| 5897 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 5898 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5899 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 5900 | G.top_var = &G.shell_ver; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 5901 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 5902 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 5903 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 5904 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5905 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 5906 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 5907 | if (e) while (*e) { |
| 5908 | char *value = strchr(*e, '='); |
| 5909 | if (value) { /* paranoia */ |
| 5910 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 5911 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 5912 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 5913 | cur_var->max_len = strlen(*e); |
| 5914 | cur_var->flg_export = 1; |
| 5915 | } |
| 5916 | e++; |
| 5917 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 5918 | debug_printf_env("putenv '%s'\n", hush_version_str); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 5919 | putenv((char *)hush_version_str); /* reinstate HUSH_VERSION */ |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 5920 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5921 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 5922 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5923 | G.global_argc = argc; |
| 5924 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 5925 | /* Initialize some more globals to non-zero values */ |
| 5926 | set_cwd(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 5927 | #if ENABLE_HUSH_INTERACTIVE |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 5928 | if (ENABLE_FEATURE_EDITING) |
| 5929 | cmdedit_set_initial_prompt(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5930 | G.PS2 = "> "; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 5931 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 5932 | |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 5933 | if (setjmp(die_jmp)) { |
| 5934 | /* xfunc has failed! die die die */ |
| 5935 | /* no EXIT traps, this is an escape hatch! */ |
| 5936 | G.exiting = 1; |
| 5937 | hush_exit(xfunc_error_retval); |
| 5938 | } |
| 5939 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5940 | /* Shell is non-interactive at first. We need to call |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 5941 | * block_signals(0) if we are going to execute "sh <script>", |
| 5942 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5943 | * If we later decide that we are interactive, we run block_signals(0) |
| 5944 | * (or re-run block_signals(1) if we ran block_signals(0) before) |
| 5945 | * in order to intercept (more) signals. |
| 5946 | */ |
| 5947 | |
| 5948 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 5949 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5950 | while (1) { |
| 5951 | opt = getopt(argc, argv, "c:xins" |
| 5952 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 5953 | "<:$:R:V:" |
| 5954 | # if ENABLE_HUSH_FUNCTIONS |
| 5955 | "F:" |
| 5956 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5957 | #endif |
| 5958 | ); |
| 5959 | if (opt <= 0) |
| 5960 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5961 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 5962 | case 'c': |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 5963 | if (!G.root_pid) |
| 5964 | G.root_pid = getpid(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5965 | G.global_argv = argv + optind; |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 5966 | if (!argv[optind]) { |
| 5967 | /* -c 'script' (no params): prevent empty $0 */ |
| 5968 | *--G.global_argv = argv[0]; |
| 5969 | optind--; |
| 5970 | } /* else -c 'script' PAR0 PAR1: $0 is PAR0 */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5971 | G.global_argc = argc - optind; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5972 | block_signals(0); /* 0: called 1st time */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5973 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 5974 | goto final_return; |
| 5975 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 5976 | /* Well, we cannot just declare interactiveness, |
| 5977 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5978 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 5979 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 5980 | case 's': |
| 5981 | /* "-s" means "read from stdin", but this is how we always |
| 5982 | * operate, so simply do nothing here. */ |
| 5983 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5984 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 5985 | case '<': /* "big heredoc" support */ |
| 5986 | full_write(STDOUT_FILENO, optarg, strlen(optarg)); |
| 5987 | _exit(0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5988 | case '$': |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 5989 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 5990 | optarg++; |
| 5991 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 5992 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5993 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 5994 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 5995 | optarg++; |
| 5996 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 5997 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 5998 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5999 | case 'R': |
| 6000 | case 'V': |
| 6001 | set_local_var(xstrdup(optarg), 0, opt == 'R'); |
| 6002 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 6003 | # if ENABLE_HUSH_FUNCTIONS |
| 6004 | case 'F': { |
| 6005 | struct function *funcp = new_function(optarg); |
| 6006 | /* funcp->name is already set to optarg */ |
| 6007 | /* funcp->body is set to NULL. It's a special case. */ |
| 6008 | funcp->body_as_string = argv[optind]; |
| 6009 | optind++; |
| 6010 | break; |
| 6011 | } |
| 6012 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6013 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6014 | case 'n': |
| 6015 | case 'x': |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6016 | if (!set_mode('-', opt)) |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6017 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6018 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6019 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6020 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 6021 | " or: sh -c command [args]...\n\n"); |
| 6022 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6023 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6024 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6025 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6026 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6027 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6028 | |
| 6029 | if (!G.root_pid) |
| 6030 | G.root_pid = getpid(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6031 | |
| 6032 | /* If we are login shell... */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6033 | if (argv[0] && argv[0][0] == '-') { |
| 6034 | FILE *input; |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 6035 | /* TODO: what should argv be while sourcing /etc/profile? */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6036 | debug_printf("sourcing /etc/profile\n"); |
| 6037 | input = fopen_for_read("/etc/profile"); |
| 6038 | if (input != NULL) { |
| 6039 | close_on_exec_on(fileno(input)); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6040 | block_signals(0); /* 0: called 1st time */ |
| 6041 | signal_mask_is_inited = 1; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6042 | parse_and_run_file(input); |
| 6043 | fclose(input); |
| 6044 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6045 | /* bash: after sourcing /etc/profile, |
| 6046 | * tries to source (in the given order): |
| 6047 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
| 6048 | * stopping of first found. --noprofile turns this off. |
| 6049 | * bash also sources ~/.bash_logout on exit. |
| 6050 | * If called as sh, skips .bash_XXX files. |
| 6051 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6052 | } |
| 6053 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6054 | if (argv[optind]) { |
| 6055 | FILE *input; |
| 6056 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6057 | * "bash <script>" (which is never interactive (unless -i?)) |
| 6058 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6059 | * If called as sh, does the same but with $ENV. |
| 6060 | */ |
| 6061 | debug_printf("running script '%s'\n", argv[optind]); |
| 6062 | G.global_argv = argv + optind; |
| 6063 | G.global_argc = argc - optind; |
| 6064 | input = xfopen_for_read(argv[optind]); |
| 6065 | close_on_exec_on(fileno(input)); |
| 6066 | if (!signal_mask_is_inited) |
| 6067 | block_signals(0); /* 0: called 1st time */ |
| 6068 | parse_and_run_file(input); |
| 6069 | #if ENABLE_FEATURE_CLEAN_UP |
| 6070 | fclose(input); |
| 6071 | #endif |
| 6072 | goto final_return; |
| 6073 | } |
| 6074 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6075 | /* Up to here, shell was non-interactive. Now it may become one. |
| 6076 | * NB: don't forget to (re)run block_signals(0/1) as needed. |
| 6077 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6078 | |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6079 | /* A shell is interactive if the '-i' flag was given, or if all of |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6080 | * the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 6081 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6082 | * no arguments remaining or the -s flag given |
| 6083 | * standard input is a terminal |
| 6084 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6085 | * Refer to Posix.2, the description of the 'sh' utility. |
| 6086 | */ |
| 6087 | #if ENABLE_HUSH_JOB |
| 6088 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6089 | G.saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6090 | debug_printf("saved_tty_pgrp:%d\n", G.saved_tty_pgrp); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6091 | //TODO: "interactive" and "have job control" are two different things. |
| 6092 | //If tcgetpgrp fails here, "have job control" is false, but "interactive" |
| 6093 | //should stay on! Currently, we mix these into one. |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6094 | if (G.saved_tty_pgrp >= 0) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6095 | /* try to dup stdin to high fd#, >= 255 */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6096 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 6097 | if (G_interactive_fd < 0) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 6098 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6099 | G_interactive_fd = dup(STDIN_FILENO); |
| 6100 | if (G_interactive_fd < 0) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 6101 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6102 | G_interactive_fd = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 6103 | } |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6104 | // TODO: track & disallow any attempts of user |
| 6105 | // to (inadvertently) close/redirect it |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 6106 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6107 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6108 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6109 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6110 | pid_t shell_pgrp; |
| 6111 | |
| 6112 | /* We are indeed interactive shell, and we will perform |
| 6113 | * job control. Setting up for that. */ |
| 6114 | |
| 6115 | close_on_exec_on(G_interactive_fd); |
| 6116 | /* If we were run as 'hush &', sleep until we are |
| 6117 | * in the foreground (tty pgrp == our pgrp). |
| 6118 | * If we get started under a job aware app (like bash), |
| 6119 | * make sure we are now in charge so we don't fight over |
| 6120 | * who gets the foreground */ |
| 6121 | while (1) { |
| 6122 | shell_pgrp = getpgrp(); |
| 6123 | G.saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 6124 | if (G.saved_tty_pgrp == shell_pgrp) |
| 6125 | break; |
| 6126 | /* send TTIN to ourself (should stop us) */ |
| 6127 | kill(- shell_pgrp, SIGTTIN); |
| 6128 | } |
| 6129 | /* Block some signals */ |
| 6130 | block_signals(signal_mask_is_inited); |
| 6131 | /* Set other signals to restore saved_tty_pgrp */ |
| 6132 | set_fatal_handlers(); |
| 6133 | /* Put ourselves in our own process group */ |
| 6134 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 6135 | /* Grab control of the terminal */ |
| 6136 | tcsetpgrp(G_interactive_fd, getpid()); |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 6137 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 6138 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6139 | enable_restore_tty_pgrp_on_exit(); /* sets die_sleep = -1 */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6140 | } else if (!signal_mask_is_inited) { |
| 6141 | block_signals(0); /* 0: called 1st time */ |
| 6142 | } /* else: block_signals(0) was done before */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6143 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6144 | /* No job control compiled in, only prompt/line editing */ |
| 6145 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6146 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 6147 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6148 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6149 | G_interactive_fd = dup(STDIN_FILENO); |
| 6150 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6151 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6152 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6153 | } |
| 6154 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6155 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6156 | close_on_exec_on(G_interactive_fd); |
| 6157 | block_signals(signal_mask_is_inited); |
| 6158 | } else if (!signal_mask_is_inited) { |
| 6159 | block_signals(0); |
| 6160 | } |
| 6161 | #else |
| 6162 | /* We have interactiveness code disabled */ |
| 6163 | if (!signal_mask_is_inited) { |
| 6164 | block_signals(0); |
| 6165 | } |
| 6166 | #endif |
| 6167 | /* bash: |
| 6168 | * if interactive but not a login shell, sources ~/.bashrc |
| 6169 | * (--norc turns this off, --rcfile <file> overrides) |
| 6170 | */ |
| 6171 | |
| 6172 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Mike Frysinger | 258275d | 2009-04-05 21:19:43 +0000 | [diff] [blame] | 6173 | printf("\n\n%s hush - the humble shell\n", bb_banner); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 6174 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
| 6175 | } |
| 6176 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6177 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6178 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6179 | final_return: |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 6180 | #if ENABLE_FEATURE_CLEAN_UP |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6181 | if (G.cwd != bb_msg_unknown) |
| 6182 | free((char*)G.cwd); |
| 6183 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6184 | while (cur_var) { |
| 6185 | struct variable *tmp = cur_var; |
| 6186 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 6187 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6188 | cur_var = cur_var->next; |
| 6189 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 6190 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6191 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6192 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6193 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 6194 | |
| 6195 | |
| 6196 | #if ENABLE_LASH |
| 6197 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 6198 | int lash_main(int argc, char **argv) |
| 6199 | { |
| 6200 | //bb_error_msg("lash is deprecated, please use hush instead"); |
| 6201 | return hush_main(argc, argv); |
| 6202 | } |
| 6203 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6204 | |
| 6205 | |
| 6206 | /* |
| 6207 | * Built-ins |
| 6208 | */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 6209 | static int builtin_trap(char **argv) |
| 6210 | { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6211 | int i; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 6212 | int sig; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6213 | char *new_cmd; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 6214 | |
| 6215 | if (!G.traps) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6216 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 6217 | |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 6218 | argv++; |
| 6219 | if (!*argv) { |
| 6220 | /* No args: print all trapped. This isn't 100% correct as we |
| 6221 | * should be escaping the cmd so that it can be pasted back in |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 6222 | */ |
| 6223 | for (i = 0; i < NSIG; ++i) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6224 | if (G.traps[i]) |
| 6225 | printf("trap -- '%s' %s\n", G.traps[i], get_signame(i)); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 6226 | return EXIT_SUCCESS; |
| 6227 | } |
| 6228 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6229 | new_cmd = NULL; |
| 6230 | i = 0; |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 6231 | /* If first arg is decimal: reset all specified signals */ |
| 6232 | sig = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 6233 | if (errno == 0) { |
| 6234 | int ret; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 6235 | set_all: |
| 6236 | ret = EXIT_SUCCESS; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6237 | while (*argv) { |
| 6238 | sig = get_signum(*argv++); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 6239 | if (sig < 0 || sig >= NSIG) { |
| 6240 | ret = EXIT_FAILURE; |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 6241 | /* Mimic bash message exactly */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 6242 | bb_perror_msg("trap: %s: invalid signal specification", argv[i]); |
| 6243 | continue; |
| 6244 | } |
| 6245 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6246 | free(G.traps[sig]); |
| 6247 | G.traps[sig] = xstrdup(new_cmd); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 6248 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6249 | debug_printf("trap: setting SIG%s (%i) to '%s'", |
| 6250 | get_signame(sig), sig, G.traps[sig]); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 6251 | |
| 6252 | /* There is no signal for 0 (EXIT) */ |
| 6253 | if (sig == 0) |
| 6254 | continue; |
| 6255 | |
| 6256 | if (new_cmd) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6257 | sigaddset(&G.blocked_set, sig); |
| 6258 | } else { |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 6259 | /* There was a trap handler, we are removing it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6260 | * (if sig has non-DFL handling, |
| 6261 | * we don't need to do anything) */ |
| 6262 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 6263 | continue; |
| 6264 | sigdelset(&G.blocked_set, sig); |
| 6265 | } |
| 6266 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 6267 | } |
| 6268 | return ret; |
| 6269 | } |
| 6270 | |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 6271 | /* First arg is "-": reset all specified to default */ |
| 6272 | /* First arg is "": ignore all specified */ |
| 6273 | /* Everything else: execute first arg upon signal */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6274 | if (!argv[1]) { |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 6275 | bb_error_msg("trap: invalid arguments"); |
| 6276 | return EXIT_FAILURE; |
| 6277 | } |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 6278 | if (NOT_LONE_DASH(*argv)) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6279 | new_cmd = *argv; |
| 6280 | argv++; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 6281 | goto set_all; |
| 6282 | } |
| 6283 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6284 | static int builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6285 | { |
| 6286 | return 0; |
| 6287 | } |
| 6288 | |
| 6289 | static int builtin_test(char **argv) |
| 6290 | { |
| 6291 | int argc = 0; |
| 6292 | while (*argv) { |
| 6293 | argc++; |
| 6294 | argv++; |
| 6295 | } |
| 6296 | return test_main(argc, argv - argc); |
| 6297 | } |
| 6298 | |
| 6299 | static int builtin_echo(char **argv) |
| 6300 | { |
| 6301 | int argc = 0; |
| 6302 | while (*argv) { |
| 6303 | argc++; |
| 6304 | argv++; |
| 6305 | } |
| 6306 | return echo_main(argc, argv - argc); |
| 6307 | } |
| 6308 | |
| 6309 | static int builtin_eval(char **argv) |
| 6310 | { |
| 6311 | int rcode = EXIT_SUCCESS; |
| 6312 | |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6313 | if (*++argv) { |
| 6314 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6315 | /* bash: |
| 6316 | * eval "echo Hi; done" ("done" is syntax error): |
| 6317 | * "echo Hi" will not execute too. |
| 6318 | */ |
| 6319 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6320 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6321 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6322 | } |
| 6323 | return rcode; |
| 6324 | } |
| 6325 | |
| 6326 | static int builtin_cd(char **argv) |
| 6327 | { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6328 | const char *newdir = argv[1]; |
| 6329 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6330 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6331 | * bash says "bash: cd: HOME not set" and does nothing |
| 6332 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6333 | */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6334 | newdir = getenv("HOME") ? : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6335 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6336 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6337 | /* Mimic bash message exactly */ |
| 6338 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6339 | return EXIT_FAILURE; |
| 6340 | } |
| 6341 | set_cwd(); |
| 6342 | return EXIT_SUCCESS; |
| 6343 | } |
| 6344 | |
| 6345 | static int builtin_exec(char **argv) |
| 6346 | { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6347 | if (*++argv == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6348 | return EXIT_SUCCESS; /* bash does this */ |
| 6349 | { |
| 6350 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6351 | nommu_save_t dummy; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6352 | #endif |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 6353 | // TODO: if exec fails, bash does NOT exit! We do... |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6354 | pseudo_exec_argv(&dummy, argv, 0, NULL); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6355 | /* never returns */ |
| 6356 | } |
| 6357 | } |
| 6358 | |
| 6359 | static int builtin_exit(char **argv) |
| 6360 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 6361 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6362 | // TODO: bash does it ONLY on top-level sh exit (+interacive only?) |
| 6363 | //puts("exit"); /* bash does it */ |
| 6364 | // TODO: warn if we have background jobs: "There are stopped jobs" |
| 6365 | // On second consecutive 'exit', exit anyway. |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 6366 | // perhaps use G.exiting = -1 as indicator "last cmd was exit" |
| 6367 | |
| 6368 | /* note: EXIT trap is run by hush_exit */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6369 | if (*++argv == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6370 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6371 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 6372 | xfunc_error_retval = 255; |
| 6373 | /* bash: exit -2 == exit 254, no error msg */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6374 | hush_exit(xatoi(*argv) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6375 | } |
| 6376 | |
| 6377 | static int builtin_export(char **argv) |
| 6378 | { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6379 | if (*++argv == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6380 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6381 | if (e) { |
| 6382 | while (*e) { |
| 6383 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6384 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6385 | #else |
| 6386 | /* ash emits: export VAR='VAL' |
| 6387 | * bash: declare -x VAR="VAL" |
| 6388 | * we follow ash example */ |
| 6389 | const char *s = *e++; |
| 6390 | const char *p = strchr(s, '='); |
| 6391 | |
| 6392 | if (!p) /* wtf? take next variable */ |
| 6393 | continue; |
| 6394 | /* export var= */ |
| 6395 | printf("export %.*s", (int)(p - s) + 1, s); |
| 6396 | s = p + 1; |
| 6397 | while (*s) { |
| 6398 | if (*s != '\'') { |
| 6399 | p = strchrnul(s, '\''); |
| 6400 | /* print 'xxxx' */ |
| 6401 | printf("'%.*s'", (int)(p - s), s); |
| 6402 | if (*p == '\0') |
| 6403 | break; |
| 6404 | s = p; |
| 6405 | } |
| 6406 | /* s points to '; print ''...'''" */ |
| 6407 | putchar('"'); |
| 6408 | do putchar('\''); while (*++s == '\''); |
| 6409 | putchar('"'); |
| 6410 | } |
| 6411 | putchar('\n'); |
| 6412 | #endif |
| 6413 | } |
| 6414 | fflush(stdout); |
| 6415 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6416 | return EXIT_SUCCESS; |
| 6417 | } |
| 6418 | |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6419 | do { |
| 6420 | const char *value; |
| 6421 | char *name = *argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6422 | |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6423 | value = strchr(name, '='); |
| 6424 | if (!value) { |
| 6425 | /* They are exporting something without a =VALUE */ |
| 6426 | struct variable *var; |
| 6427 | |
| 6428 | var = get_local_var(name); |
| 6429 | if (var) { |
| 6430 | var->flg_export = 1; |
| 6431 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 6432 | putenv(var->varstr); |
| 6433 | } |
| 6434 | /* bash does not return an error when trying to export |
| 6435 | * an undefined variable. Do likewise. */ |
| 6436 | continue; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6437 | } |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6438 | set_local_var(xstrdup(name), 1, 0); |
| 6439 | } while (*++argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6440 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6441 | return EXIT_SUCCESS; |
| 6442 | } |
| 6443 | |
| 6444 | #if ENABLE_HUSH_JOB |
| 6445 | /* built-in 'fg' and 'bg' handler */ |
| 6446 | static int builtin_fg_bg(char **argv) |
| 6447 | { |
| 6448 | int i, jobnum; |
| 6449 | struct pipe *pi; |
| 6450 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6451 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6452 | return EXIT_FAILURE; |
| 6453 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 6454 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6455 | for (pi = G.job_list; pi; pi = pi->next) { |
| 6456 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6457 | goto found; |
| 6458 | } |
| 6459 | } |
| 6460 | bb_error_msg("%s: no current job", argv[0]); |
| 6461 | return EXIT_FAILURE; |
| 6462 | } |
| 6463 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 6464 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 6465 | return EXIT_FAILURE; |
| 6466 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6467 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6468 | if (pi->jobid == jobnum) { |
| 6469 | goto found; |
| 6470 | } |
| 6471 | } |
| 6472 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 6473 | return EXIT_FAILURE; |
| 6474 | found: |
| 6475 | // TODO: bash prints a string representation |
| 6476 | // of job being foregrounded (like "sleep 1 | cat") |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 6477 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6478 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6479 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6480 | } |
| 6481 | |
| 6482 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 6483 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 6484 | for (i = 0; i < pi->num_cmds; i++) { |
| 6485 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 6486 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6487 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 6488 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6489 | |
| 6490 | i = kill(- pi->pgrp, SIGCONT); |
| 6491 | if (i < 0) { |
| 6492 | if (errno == ESRCH) { |
| 6493 | delete_finished_bg_job(pi); |
| 6494 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6495 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 6496 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6497 | } |
| 6498 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 6499 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6500 | remove_bg_job(pi); |
| 6501 | return checkjobs_and_fg_shell(pi); |
| 6502 | } |
| 6503 | return EXIT_SUCCESS; |
| 6504 | } |
| 6505 | #endif |
| 6506 | |
| 6507 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6508 | static int builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6509 | { |
| 6510 | const struct built_in_command *x; |
| 6511 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 6512 | printf("\n" |
| 6513 | "Built-in commands:\n" |
| 6514 | "------------------\n"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6515 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
| 6516 | printf("%s\t%s\n", x->cmd, x->descr); |
| 6517 | } |
| 6518 | printf("\n\n"); |
| 6519 | return EXIT_SUCCESS; |
| 6520 | } |
| 6521 | #endif |
| 6522 | |
| 6523 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6524 | static int builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6525 | { |
| 6526 | struct pipe *job; |
| 6527 | const char *status_string; |
| 6528 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6529 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 6530 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6531 | status_string = "Stopped"; |
| 6532 | else |
| 6533 | status_string = "Running"; |
| 6534 | |
| 6535 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 6536 | } |
| 6537 | return EXIT_SUCCESS; |
| 6538 | } |
| 6539 | #endif |
| 6540 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 6541 | #if HUSH_DEBUG |
| 6542 | static int builtin_memleak(char **argv UNUSED_PARAM) |
| 6543 | { |
| 6544 | void *p; |
| 6545 | unsigned long l; |
| 6546 | |
| 6547 | /* Crude attempt to find where "free memory" starts, |
| 6548 | * sans fragmentation. */ |
| 6549 | p = malloc(240); |
| 6550 | l = (unsigned long)p; |
| 6551 | free(p); |
| 6552 | p = malloc(3400); |
| 6553 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 6554 | free(p); |
| 6555 | |
| 6556 | if (!G.memleak_value) |
| 6557 | G.memleak_value = l; |
| 6558 | |
| 6559 | l -= G.memleak_value; |
| 6560 | if ((long)l < 0) |
| 6561 | l = 0; |
| 6562 | l /= 1024; |
| 6563 | if (l > 127) |
| 6564 | l = 127; |
| 6565 | |
| 6566 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 6567 | return l; |
| 6568 | } |
| 6569 | #endif |
| 6570 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6571 | static int builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6572 | { |
| 6573 | puts(set_cwd()); |
| 6574 | return EXIT_SUCCESS; |
| 6575 | } |
| 6576 | |
| 6577 | static int builtin_read(char **argv) |
| 6578 | { |
| 6579 | char *string; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6580 | const char *name = "REPLY"; |
| 6581 | |
| 6582 | if (argv[1]) { |
| 6583 | name = argv[1]; |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 6584 | /* bash (3.2.33(1)) bug: "read 0abcd" will execute, |
| 6585 | * and _after_ that_ it will complain */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6586 | if (!is_well_formed_var_name(name, '\0')) { |
| 6587 | /* Mimic bash message */ |
| 6588 | bb_error_msg("read: '%s': not a valid identifier", name); |
| 6589 | return 1; |
| 6590 | } |
| 6591 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6592 | |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 6593 | //TODO: bash unbackslashes input, splits words and puts them in argv[i] |
| 6594 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6595 | string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6596 | return set_local_var(string, 0, 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6597 | } |
| 6598 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6599 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 6600 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 6601 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6602 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 6603 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 6604 | * set -- [argument...] |
| 6605 | * set -o |
| 6606 | * set +o |
| 6607 | * Implementations shall support the options in both their hyphen and |
| 6608 | * plus-sign forms. These options can also be specified as options to sh. |
| 6609 | * Examples: |
| 6610 | * Write out all variables and their values: set |
| 6611 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 6612 | * Turn on the -x and -v options: set -xv |
| 6613 | * Unset all positional parameters: set -- |
| 6614 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 6615 | * Set the positional parameters to the expansion of x, even if x expands |
| 6616 | * with a leading '-' or '+': set -- $x |
| 6617 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6618 | * 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] | 6619 | */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6620 | static int builtin_set(char **argv) |
| 6621 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 6622 | int n; |
| 6623 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 6624 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6625 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 6626 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 6627 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6628 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6629 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 6630 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 6631 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6632 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6633 | do { |
| 6634 | if (!strcmp(arg, "--")) { |
| 6635 | ++argv; |
| 6636 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 6637 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 6638 | if (arg[0] != '+' && arg[0] != '-') |
| 6639 | break; |
| 6640 | for (n = 1; arg[n]; ++n) |
| 6641 | if (set_mode(arg[0], arg[n])) |
| 6642 | goto error; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 6643 | } while ((arg = *++argv) != NULL); |
| 6644 | /* Now argv[0] is 1st argument */ |
| 6645 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6646 | if (arg == NULL) |
| 6647 | return EXIT_SUCCESS; |
| 6648 | set_argv: |
| 6649 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 6650 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 6651 | g_argv = G.global_argv; |
| 6652 | if (G.global_args_malloced) { |
| 6653 | pp = g_argv; |
| 6654 | while (*++pp) |
| 6655 | free(*pp); |
| 6656 | g_argv[1] = NULL; |
| 6657 | } else { |
| 6658 | G.global_args_malloced = 1; |
| 6659 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 6660 | pp[0] = g_argv[0]; /* retain $0 */ |
| 6661 | g_argv = pp; |
| 6662 | } |
| 6663 | /* This realloc's G.global_argv */ |
| 6664 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 6665 | |
| 6666 | n = 1; |
| 6667 | while (*++pp) |
| 6668 | n++; |
| 6669 | G.global_argc = n; |
| 6670 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6671 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6672 | |
| 6673 | /* Nothing known, so abort */ |
| 6674 | error: |
| 6675 | bb_error_msg("set: %s: invalid option", arg); |
| 6676 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6677 | } |
| 6678 | |
| 6679 | static int builtin_shift(char **argv) |
| 6680 | { |
| 6681 | int n = 1; |
| 6682 | if (argv[1]) { |
| 6683 | n = atoi(argv[1]); |
| 6684 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6685 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 6686 | if (G.global_args_malloced) { |
| 6687 | int m = 1; |
| 6688 | while (m <= n) |
| 6689 | free(G.global_argv[m++]); |
| 6690 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6691 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 6692 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 6693 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6694 | return EXIT_SUCCESS; |
| 6695 | } |
| 6696 | return EXIT_FAILURE; |
| 6697 | } |
| 6698 | |
| 6699 | static int builtin_source(char **argv) |
| 6700 | { |
| 6701 | FILE *input; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 6702 | smallint sv_flg; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 6703 | save_arg_t sv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6704 | |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6705 | if (*++argv == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6706 | return EXIT_FAILURE; |
| 6707 | |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 6708 | /* TODO: search through $PATH is missing */ |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6709 | input = fopen_or_warn(*argv, "r"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6710 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6711 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6712 | return EXIT_FAILURE; |
| 6713 | } |
| 6714 | close_on_exec_on(fileno(input)); |
| 6715 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 6716 | sv_flg = G.flag_return_in_progress; |
| 6717 | /* "we are inside sourced file, ok to use return" */ |
| 6718 | G.flag_return_in_progress = -1; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 6719 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 6720 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6721 | parse_and_run_file(input); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6722 | fclose(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 6723 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 6724 | restore_G_args(&sv, argv); |
| 6725 | G.flag_return_in_progress = sv_flg; |
| 6726 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6727 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6728 | } |
| 6729 | |
| 6730 | static int builtin_umask(char **argv) |
| 6731 | { |
| 6732 | mode_t new_umask; |
| 6733 | const char *arg = argv[1]; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6734 | if (arg) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6735 | //TODO: umask may take chmod-like symbolic masks |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 6736 | new_umask = bb_strtou(arg, NULL, 8); |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6737 | if (errno) { |
| 6738 | //Message? bash examples: |
| 6739 | //bash: umask: 'q': invalid symbolic mode operator |
| 6740 | //bash: umask: 999: octal number out of range |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6741 | return EXIT_FAILURE; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6742 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6743 | } else { |
| 6744 | new_umask = umask(0); |
| 6745 | printf("%.3o\n", (unsigned) new_umask); |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6746 | /* fall through and restore new_umask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6747 | } |
| 6748 | umask(new_umask); |
| 6749 | return EXIT_SUCCESS; |
| 6750 | } |
| 6751 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 6752 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6753 | static int builtin_unset(char **argv) |
| 6754 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 6755 | int ret; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6756 | char var; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 6757 | |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6758 | if (!*++argv) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 6759 | return EXIT_SUCCESS; |
| 6760 | |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6761 | var = 'v'; |
| 6762 | if (argv[0][0] == '-') { |
| 6763 | switch (argv[0][1]) { |
| 6764 | case 'v': |
| 6765 | case 'f': |
| 6766 | var = argv[0][1]; |
| 6767 | break; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 6768 | default: |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6769 | bb_error_msg("unset: %s: invalid option", *argv); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 6770 | return EXIT_FAILURE; |
| 6771 | } |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6772 | //TODO: disallow "unset -vf ..." too |
| 6773 | argv++; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 6774 | } |
| 6775 | |
| 6776 | ret = EXIT_SUCCESS; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6777 | while (*argv) { |
| 6778 | if (var == 'v') { |
| 6779 | if (unset_local_var(*argv)) { |
| 6780 | /* unset <nonexistent_var> doesn't fail. |
| 6781 | * Error is when one tries to unset RO var. |
| 6782 | * Message was printed by unset_local_var. */ |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 6783 | ret = EXIT_FAILURE; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6784 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 6785 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 6786 | //#if ENABLE_HUSH_FUNCTIONS |
| 6787 | // else { |
| 6788 | // unset_local_func(*argv); |
| 6789 | // } |
| 6790 | //#endif |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6791 | argv++; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 6792 | } |
| 6793 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6794 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 6795 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 6796 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
| 6797 | static int builtin_wait(char **argv) |
| 6798 | { |
| 6799 | int ret = EXIT_SUCCESS; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 6800 | int status, sig; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 6801 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 6802 | if (*++argv == NULL) { |
| 6803 | /* Don't care about wait results */ |
| 6804 | /* Note 1: must wait until there are no more children */ |
| 6805 | /* Note 2: must be interruptible */ |
| 6806 | /* Examples: |
| 6807 | * $ sleep 3 & sleep 6 & wait |
| 6808 | * [1] 30934 sleep 3 |
| 6809 | * [2] 30935 sleep 6 |
| 6810 | * [1] Done sleep 3 |
| 6811 | * [2] Done sleep 6 |
| 6812 | * $ sleep 3 & sleep 6 & wait |
| 6813 | * [1] 30936 sleep 3 |
| 6814 | * [2] 30937 sleep 6 |
| 6815 | * [1] Done sleep 3 |
| 6816 | * ^C <-- after ~4 sec from keyboard |
| 6817 | * $ |
| 6818 | */ |
| 6819 | sigaddset(&G.blocked_set, SIGCHLD); |
| 6820 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 6821 | while (1) { |
| 6822 | checkjobs(NULL); |
| 6823 | if (errno == ECHILD) |
| 6824 | break; |
| 6825 | /* Wait for SIGCHLD or any other signal of interest */ |
| 6826 | /* sigtimedwait with infinite timeout: */ |
| 6827 | sig = sigwaitinfo(&G.blocked_set, NULL); |
| 6828 | if (sig > 0) { |
| 6829 | sig = check_and_run_traps(sig); |
| 6830 | if (sig && sig != SIGCHLD) { /* see note 2 */ |
| 6831 | ret = 128 + sig; |
| 6832 | break; |
| 6833 | } |
| 6834 | } |
| 6835 | } |
| 6836 | sigdelset(&G.blocked_set, SIGCHLD); |
| 6837 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 6838 | return ret; |
| 6839 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 6840 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 6841 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6842 | while (*argv) { |
| 6843 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 6844 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6845 | /* mimic bash message */ |
| 6846 | 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] | 6847 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6848 | } |
| 6849 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 6850 | if (WIFSIGNALED(status)) |
| 6851 | ret = 128 + WTERMSIG(status); |
| 6852 | else if (WIFEXITED(status)) |
| 6853 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6854 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 6855 | ret = EXIT_FAILURE; |
| 6856 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6857 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 6858 | ret = 127; |
| 6859 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6860 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 6861 | } |
| 6862 | |
| 6863 | return ret; |
| 6864 | } |
| 6865 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 6866 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 6867 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 6868 | { |
| 6869 | if (argv[1]) { |
| 6870 | def = bb_strtou(argv[1], NULL, 10); |
| 6871 | if (errno || def < def_min || argv[2]) { |
| 6872 | bb_error_msg("%s: bad arguments", argv[0]); |
| 6873 | def = UINT_MAX; |
| 6874 | } |
| 6875 | } |
| 6876 | return def; |
| 6877 | } |
| 6878 | #endif |
| 6879 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 6880 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 6881 | static int builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 6882 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 6883 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6884 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 6885 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 6886 | return EXIT_SUCCESS; /* bash compat */ |
| 6887 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6888 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 6889 | |
| 6890 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 6891 | if (depth == UINT_MAX) |
| 6892 | G.flag_break_continue = BC_BREAK; |
| 6893 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6894 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 6895 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 6896 | return EXIT_SUCCESS; |
| 6897 | } |
| 6898 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 6899 | static int builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 6900 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 6901 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 6902 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 6903 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 6904 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame^] | 6905 | |
| 6906 | #if ENABLE_HUSH_FUNCTIONS |
| 6907 | static int builtin_return(char **argv UNUSED_PARAM) |
| 6908 | { |
| 6909 | int rc; |
| 6910 | |
| 6911 | if (G.flag_return_in_progress != -1) { |
| 6912 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 6913 | return EXIT_FAILURE; /* bash compat */ |
| 6914 | } |
| 6915 | |
| 6916 | G.flag_return_in_progress = 1; |
| 6917 | |
| 6918 | /* bash: |
| 6919 | * out of range: wraps around at 256, does not error out |
| 6920 | * non-numeric param: |
| 6921 | * f() { false; return qwe; }; f; echo $? |
| 6922 | * bash: return: qwe: numeric argument required <== we do this |
| 6923 | * 255 <== we also do this |
| 6924 | */ |
| 6925 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 6926 | return rc; |
| 6927 | } |
| 6928 | #endif |