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) |
Denis Vlasenko | fa4ca78 | 2009-04-16 12:00:15 +0000 | [diff] [blame] | 53 | * $var refs in function do not pick up values set by "var=val func" |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 54 | * builtins: ulimit |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 55 | * follow IFS rules more precisely, including update semantics |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 56 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 57 | * 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] | 58 | */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 59 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 60 | #include <glob.h> |
| 61 | /* #include <dmalloc.h> */ |
| 62 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 63 | # include <fnmatch.h> |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 64 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 65 | #include "math.h" |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 66 | #include "match.h" |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 67 | #ifndef PIPE_BUF |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 68 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 69 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 70 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 71 | |
| 72 | /* Debug build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 73 | #define LEAK_HUNTING 0 |
| 74 | #define BUILD_AS_NOMMU 0 |
| 75 | /* Enable/disable sanity checks. Ok to enable in production, |
| 76 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 77 | * Keeping 1 for now even in released versions. |
| 78 | */ |
| 79 | #define HUSH_DEBUG 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 80 | |
| 81 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 82 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 83 | # undef BB_MMU |
| 84 | # undef USE_FOR_NOMMU |
| 85 | # undef USE_FOR_MMU |
| 86 | # define BB_MMU 0 |
| 87 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 88 | # define USE_FOR_MMU(...) |
| 89 | #endif |
| 90 | |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 91 | #if defined SINGLE_APPLET_MAIN |
| 92 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 93 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 94 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 95 | # undef IF_FEATURE_SH_STANDALONE |
| 96 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 97 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 98 | # define IF_FEATURE_SH_STANDALONE(...) |
| 99 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 100 | #endif |
| 101 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 102 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 103 | # undef ENABLE_FEATURE_EDITING |
| 104 | # define ENABLE_FEATURE_EDITING 0 |
| 105 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 106 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 107 | #endif |
| 108 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 109 | /* Do we support ANY keywords? */ |
| 110 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 111 | # define HAS_KEYWORDS 1 |
| 112 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 113 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 114 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 115 | # define HAS_KEYWORDS 0 |
| 116 | # define IF_HAS_KEYWORDS(...) |
| 117 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 118 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 119 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 120 | /* If you comment out one of these below, it will be #defined later |
| 121 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 122 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 123 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 124 | #define debug_printf_parse(...) do {} while (0) |
| 125 | #define debug_print_tree(a, b) do {} while (0) |
| 126 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 127 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 128 | #define debug_printf_jobs(...) do {} while (0) |
| 129 | #define debug_printf_expand(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 130 | #define debug_printf_glob(...) do {} while (0) |
| 131 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 132 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 133 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 134 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 135 | #define ERR_PTR ((void*)(long)1) |
| 136 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 137 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 138 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 139 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 140 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 141 | struct variable; |
| 142 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 143 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 144 | |
| 145 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 146 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 147 | */ |
| 148 | #if !BB_MMU |
| 149 | typedef struct nommu_save_t { |
| 150 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 151 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 152 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 153 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 154 | } nommu_save_t; |
| 155 | #endif |
| 156 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 157 | /* The descrip member of this structure is only used to make |
| 158 | * debugging output pretty */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 159 | static const struct { |
| 160 | int mode; |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 161 | signed char default_fd; |
| 162 | char descrip[3]; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 163 | } redir_table[] = { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 164 | { 0, 0, "??" }, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 165 | { O_RDONLY, 0, "<" }, |
| 166 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 167 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 168 | { O_RDONLY, 0, "<<" }, |
| 169 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 170 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 171 | /* { O_RDONLY, 77, "<<" }, */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 174 | typedef enum reserved_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 175 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 176 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 177 | RES_IF , |
| 178 | RES_THEN , |
| 179 | RES_ELIF , |
| 180 | RES_ELSE , |
| 181 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 182 | #endif |
| 183 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 184 | RES_FOR , |
| 185 | RES_WHILE , |
| 186 | RES_UNTIL , |
| 187 | RES_DO , |
| 188 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 189 | #endif |
| 190 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 191 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 192 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 193 | #if ENABLE_HUSH_CASE |
| 194 | RES_CASE , |
| 195 | /* two pseudo-keywords support contrived "case" syntax: */ |
| 196 | RES_MATCH , /* "word)" */ |
| 197 | RES_CASEI , /* "this command is inside CASE" */ |
| 198 | RES_ESAC , |
| 199 | #endif |
| 200 | RES_XXXX , |
| 201 | RES_SNTX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 202 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 203 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 204 | typedef struct o_string { |
| 205 | char *data; |
| 206 | int length; /* position where data is appended */ |
| 207 | int maxlen; |
| 208 | /* Protect newly added chars against globbing |
| 209 | * (by prepending \ to *, ?, [, \) */ |
| 210 | smallint o_escape; |
| 211 | smallint o_glob; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 212 | /* At least some part of the string was inside '' or "", |
| 213 | * possibly empty one: word"", wo''rd etc. */ |
| 214 | smallint o_quoted; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 215 | smallint has_empty_slot; |
| 216 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 217 | } o_string; |
| 218 | enum { |
| 219 | MAYBE_ASSIGNMENT = 0, |
| 220 | DEFINITELY_ASSIGNMENT = 1, |
| 221 | NOT_ASSIGNMENT = 2, |
| 222 | WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */ |
| 223 | }; |
| 224 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 225 | #define NULL_O_STRING { NULL } |
| 226 | |
| 227 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
| 228 | * available? Where is it documented? */ |
| 229 | typedef struct in_str { |
| 230 | const char *p; |
| 231 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 232 | char eof_flag; /* meaningless if ->p == NULL */ |
| 233 | char peek_buf[2]; |
| 234 | #if ENABLE_HUSH_INTERACTIVE |
| 235 | smallint promptme; |
| 236 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 237 | #endif |
| 238 | FILE *file; |
| 239 | int (*get) (struct in_str *); |
| 240 | int (*peek) (struct in_str *); |
| 241 | } in_str; |
| 242 | #define i_getch(input) ((input)->get(input)) |
| 243 | #define i_peek(input) ((input)->peek(input)) |
| 244 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 245 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 246 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 247 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 248 | int rd_fd; /* fd to redirect */ |
| 249 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 250 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 251 | smallint rd_type; /* (enum redir_type) */ |
| 252 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 253 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
| 254 | * 1: do we need to trim leading tabs? |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 255 | * 2: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 256 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 257 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 258 | typedef enum redir_type { |
| 259 | REDIRECT_INVALID = 0, |
| 260 | REDIRECT_INPUT = 1, |
| 261 | REDIRECT_OVERWRITE = 2, |
| 262 | REDIRECT_APPEND = 3, |
| 263 | REDIRECT_HEREDOC = 4, |
| 264 | REDIRECT_IO = 5, |
| 265 | REDIRECT_HEREDOC2 = 6, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 266 | |
| 267 | REDIRFD_CLOSE = -3, |
| 268 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 269 | REDIRFD_TO_FILE = -1, |
| 270 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 271 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 272 | HEREDOC_SKIPTABS = 1, |
| 273 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 274 | } redir_type; |
| 275 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 276 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 277 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 278 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 279 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 280 | smallint is_stopped; /* is the command currently running? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 281 | smallint grp_type; /* GRP_xxx */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 282 | #define GRP_NORMAL 0 |
| 283 | #define GRP_SUBSHELL 1 |
| 284 | #if ENABLE_HUSH_FUNCTIONS |
| 285 | # define GRP_FUNCTION 2 |
| 286 | #endif |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 287 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 288 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 289 | #if !BB_MMU |
| 290 | char *group_as_string; |
| 291 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 292 | #if ENABLE_HUSH_FUNCTIONS |
| 293 | struct function *child_func; |
| 294 | /* This field is used to prevent a bug here: |
| 295 | * while...do f1() {a;}; f1; f1 {b;}; f1; done |
| 296 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 297 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
| 298 | * when we execute "f1 {b;}", we notice that f1 exists, |
| 299 | * and that it's "parent cmd" struct is still "alive", |
| 300 | * we put those fields back into cmd->xxx |
| 301 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 302 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 303 | * Without this trick, loop would execute a;b;b;b;... |
| 304 | * instead of correct sequence a;b;a;b;... |
| 305 | * When command is freed, it severs the link |
| 306 | * (sets ->child_func->parent_cmd to NULL). |
| 307 | */ |
| 308 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 309 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 310 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 311 | * and on execution these are substituted with their values. |
| 312 | * Substitution can make _several_ words out of one argv[n]! |
| 313 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 314 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 315 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 316 | struct redir_struct *redirects; /* I/O redirections */ |
| 317 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 318 | /* Is there anything in this command at all? */ |
| 319 | #define IS_NULL_CMD(cmd) \ |
| 320 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 321 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 322 | |
| 323 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 324 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 325 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 326 | int alive_cmds; /* number of commands running (not exited) */ |
| 327 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 328 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 329 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 330 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 331 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 332 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 333 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 334 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 335 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 336 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 337 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 338 | typedef enum pipe_style { |
| 339 | PIPE_SEQ = 1, |
| 340 | PIPE_AND = 2, |
| 341 | PIPE_OR = 3, |
| 342 | PIPE_BG = 4, |
| 343 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 344 | /* Is there anything in this pipe at all? */ |
| 345 | #define IS_NULL_PIPE(pi) \ |
| 346 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 347 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 348 | /* This holds pointers to the various results of parsing */ |
| 349 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 350 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 351 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 352 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 353 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 354 | /* last command in pipe (being constructed right now) */ |
| 355 | struct command *command; |
| 356 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 357 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 358 | #if !BB_MMU |
| 359 | o_string as_string; |
| 360 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 361 | #if HAS_KEYWORDS |
| 362 | smallint ctx_res_w; |
| 363 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 364 | #if ENABLE_HUSH_CASE |
| 365 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 366 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 367 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 368 | int old_flag; |
| 369 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 370 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 371 | * when we see "if" or "then", we malloc and copy current context, |
| 372 | * and make ->stack point to it. then we parse pipeN. |
| 373 | * when closing "then" / fi" / whatever is found, |
| 374 | * we move list_head into ->stack->command->group, |
| 375 | * copy ->stack into current context, and delete ->stack. |
| 376 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 377 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 378 | struct parse_context *stack; |
| 379 | #endif |
| 380 | }; |
| 381 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 382 | /* On program start, environ points to initial environment. |
| 383 | * putenv adds new pointers into it, unsetenv removes them. |
| 384 | * Neither of these (de)allocates the strings. |
| 385 | * setenv allocates new strings in malloc space and does putenv, |
| 386 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 387 | #define setenv(...) setenv_is_leaky_dont_use() |
| 388 | struct variable { |
| 389 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 390 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 391 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 392 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 393 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 394 | }; |
| 395 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 396 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 397 | BC_BREAK = 1, |
| 398 | BC_CONTINUE = 2, |
| 399 | }; |
| 400 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 401 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 402 | struct function { |
| 403 | struct function *next; |
| 404 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 405 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 406 | struct pipe *body; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 407 | #if !BB_MMU |
| 408 | char *body_as_string; |
| 409 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 410 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 411 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 412 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 413 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 414 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 415 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 416 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 417 | /* interactive_fd != 0 means we are an interactive shell. |
| 418 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 419 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 420 | * job control still works, but terminal signals |
| 421 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 422 | * process groups can only be created with "cmd &". |
| 423 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 424 | * to give tty to the foreground process group, |
| 425 | * and will take it back when the group is stopped (^Z) |
| 426 | * or killed (^C). |
| 427 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 428 | #if ENABLE_HUSH_INTERACTIVE |
| 429 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 430 | * _AND_ if we decided to act interactively */ |
| 431 | int interactive_fd; |
| 432 | const char *PS1; |
| 433 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 434 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 435 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 436 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 437 | #endif |
| 438 | #if ENABLE_FEATURE_EDITING |
| 439 | line_input_t *line_input_state; |
| 440 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 441 | pid_t root_pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 442 | pid_t last_bg_pid; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 443 | #if ENABLE_HUSH_JOB |
| 444 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 445 | int last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 446 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 447 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 448 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 449 | #else |
| 450 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 451 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 452 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 453 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 454 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 455 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 456 | #if ENABLE_HUSH_FUNCTIONS |
| 457 | /* 0: outside of a function (or sourced file) |
| 458 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 459 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 460 | */ |
| 461 | smallint flag_return_in_progress; |
| 462 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 463 | smallint fake_mode; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 464 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 465 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 466 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 467 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 468 | smalluint global_args_malloced; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 469 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 470 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 471 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 472 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 473 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 474 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 475 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 476 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 477 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 478 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 479 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 480 | const char *cwd; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 481 | struct variable *top_var; /* = &G.shell_ver (set in main()) */ |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 482 | struct variable shell_ver; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 483 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 484 | struct function *top_func; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 485 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 486 | /* Signal and trap handling */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 487 | // unsigned count_SIGCHLD; |
| 488 | // unsigned handled_SIGCHLD; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 489 | /* which signals have non-DFL handler (even with no traps set)? */ |
| 490 | unsigned non_DFL_mask; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 491 | char **traps; /* char *traps[NSIG] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 492 | sigset_t blocked_set; |
| 493 | sigset_t inherited_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 494 | #if HUSH_DEBUG |
| 495 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 496 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 497 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 498 | char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2]; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 499 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 500 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 501 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 502 | * (too many defines). Also, I actually prefer to see when a variable |
| 503 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 504 | #define INIT_G() do { \ |
| 505 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 506 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 507 | |
| 508 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 509 | /* Function prototypes for builtins */ |
| 510 | static int builtin_cd(char **argv); |
| 511 | static int builtin_echo(char **argv); |
| 512 | static int builtin_eval(char **argv); |
| 513 | static int builtin_exec(char **argv); |
| 514 | static int builtin_exit(char **argv); |
| 515 | static int builtin_export(char **argv); |
| 516 | #if ENABLE_HUSH_JOB |
| 517 | static int builtin_fg_bg(char **argv); |
| 518 | static int builtin_jobs(char **argv); |
| 519 | #endif |
| 520 | #if ENABLE_HUSH_HELP |
| 521 | static int builtin_help(char **argv); |
| 522 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 523 | #if HUSH_DEBUG |
| 524 | static int builtin_memleak(char **argv); |
| 525 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 526 | static int builtin_pwd(char **argv); |
| 527 | static int builtin_read(char **argv); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 528 | static int builtin_set(char **argv); |
| 529 | static int builtin_shift(char **argv); |
| 530 | static int builtin_source(char **argv); |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 531 | static int builtin_test(char **argv); |
| 532 | static int builtin_trap(char **argv); |
| 533 | static int builtin_true(char **argv); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 534 | static int builtin_umask(char **argv); |
| 535 | static int builtin_unset(char **argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 536 | static int builtin_wait(char **argv); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 537 | #if ENABLE_HUSH_LOOPS |
| 538 | static int builtin_break(char **argv); |
| 539 | static int builtin_continue(char **argv); |
| 540 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 541 | #if ENABLE_HUSH_FUNCTIONS |
| 542 | static int builtin_return(char **argv); |
| 543 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 544 | |
| 545 | /* Table of built-in functions. They can be forked or not, depending on |
| 546 | * context: within pipes, they fork. As simple commands, they do not. |
| 547 | * When used in non-forking context, they can change global variables |
| 548 | * in the parent shell process. If forked, of course they cannot. |
| 549 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 550 | * still be set at the end. */ |
| 551 | struct built_in_command { |
| 552 | const char *cmd; |
| 553 | int (*function)(char **argv); |
| 554 | #if ENABLE_HUSH_HELP |
| 555 | const char *descr; |
| 556 | #define BLTIN(cmd, func, help) { cmd, func, help } |
| 557 | #else |
| 558 | #define BLTIN(cmd, func, help) { cmd, func } |
| 559 | #endif |
| 560 | }; |
| 561 | |
| 562 | /* For now, echo and test are unconditionally enabled. |
| 563 | * Maybe make it configurable? */ |
| 564 | static const struct built_in_command bltins[] = { |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 565 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 566 | BLTIN(":" , builtin_true , "No-op"), |
| 567 | BLTIN("[" , builtin_test , "Test condition"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 568 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 569 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 570 | #endif |
| 571 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 572 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 573 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 574 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 575 | #if ENABLE_HUSH_LOOPS |
| 576 | BLTIN("continue", builtin_continue, "Start new loop iteration"), |
| 577 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 578 | BLTIN("echo" , builtin_echo , "Write to stdout"), |
| 579 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 580 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 581 | BLTIN("exit" , builtin_exit , "Exit"), |
| 582 | BLTIN("export" , builtin_export , "Set environment variable"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 583 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 584 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 585 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 586 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 587 | BLTIN("help" , builtin_help , "List shell built-in commands"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 588 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 589 | #if ENABLE_HUSH_JOB |
| 590 | BLTIN("jobs" , builtin_jobs , "List active jobs"), |
| 591 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 592 | #if HUSH_DEBUG |
| 593 | BLTIN("memleak" , builtin_memleak , "Debug tool"), |
| 594 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 595 | BLTIN("pwd" , builtin_pwd , "Print current directory"), |
| 596 | BLTIN("read" , builtin_read , "Input environment variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 597 | #if ENABLE_HUSH_FUNCTIONS |
| 598 | BLTIN("return" , builtin_return , "Return from a function"), |
| 599 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 600 | BLTIN("set" , builtin_set , "Set/unset shell local variables"), |
| 601 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
| 602 | BLTIN("test" , builtin_test , "Test condition"), |
| 603 | BLTIN("trap" , builtin_trap , "Trap signals"), |
| 604 | // BLTIN("ulimit" , builtin_return , "Control resource limits"), |
| 605 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 606 | BLTIN("unset" , builtin_unset , "Unset environment variable"), |
| 607 | BLTIN("wait" , builtin_wait , "Wait for process"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 608 | }; |
| 609 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 610 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 611 | /* Debug printouts. |
| 612 | */ |
| 613 | #if HUSH_DEBUG |
| 614 | /* prevent disasters with G.debug_indent < 0 */ |
| 615 | # define indent() fprintf(stderr, "%*s", (G.debug_indent * 2) & 0xff, "") |
| 616 | # define debug_enter() (G.debug_indent++) |
| 617 | # define debug_leave() (G.debug_indent--) |
| 618 | #else |
| 619 | # define indent() ((void)0) |
| 620 | # define debug_enter() ((void)0) |
| 621 | # define debug_leave() ((void)0) |
| 622 | #endif |
| 623 | |
| 624 | #ifndef debug_printf |
| 625 | # define debug_printf(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 626 | #endif |
| 627 | |
| 628 | #ifndef debug_printf_parse |
| 629 | # define debug_printf_parse(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 630 | #endif |
| 631 | |
| 632 | #ifndef debug_printf_exec |
| 633 | #define debug_printf_exec(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 634 | #endif |
| 635 | |
| 636 | #ifndef debug_printf_env |
| 637 | # define debug_printf_env(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 638 | #endif |
| 639 | |
| 640 | #ifndef debug_printf_jobs |
| 641 | # define debug_printf_jobs(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 642 | # define DEBUG_JOBS 1 |
| 643 | #else |
| 644 | # define DEBUG_JOBS 0 |
| 645 | #endif |
| 646 | |
| 647 | #ifndef debug_printf_expand |
| 648 | # define debug_printf_expand(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 649 | # define DEBUG_EXPAND 1 |
| 650 | #else |
| 651 | # define DEBUG_EXPAND 0 |
| 652 | #endif |
| 653 | |
| 654 | #ifndef debug_printf_glob |
| 655 | # define debug_printf_glob(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 656 | # define DEBUG_GLOB 1 |
| 657 | #else |
| 658 | # define DEBUG_GLOB 0 |
| 659 | #endif |
| 660 | |
| 661 | #ifndef debug_printf_list |
| 662 | # define debug_printf_list(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 663 | #endif |
| 664 | |
| 665 | #ifndef debug_printf_subst |
| 666 | # define debug_printf_subst(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 667 | #endif |
| 668 | |
| 669 | #ifndef debug_printf_clean |
| 670 | # define debug_printf_clean(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 671 | # define DEBUG_CLEAN 1 |
| 672 | #else |
| 673 | # define DEBUG_CLEAN 0 |
| 674 | #endif |
| 675 | |
| 676 | #if DEBUG_EXPAND |
| 677 | static void debug_print_strings(const char *prefix, char **vv) |
| 678 | { |
| 679 | indent(); |
| 680 | fprintf(stderr, "%s:\n", prefix); |
| 681 | while (*vv) |
| 682 | fprintf(stderr, " '%s'\n", *vv++); |
| 683 | } |
| 684 | #else |
| 685 | #define debug_print_strings(prefix, vv) ((void)0) |
| 686 | #endif |
| 687 | |
| 688 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 689 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 690 | */ |
| 691 | #if LEAK_HUNTING |
| 692 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 693 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 694 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 695 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 696 | return ptr; |
| 697 | } |
| 698 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 699 | { |
| 700 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 701 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 702 | return ptr; |
| 703 | } |
| 704 | static char *xxstrdup(int lineno, const char *str) |
| 705 | { |
| 706 | char *ptr = xstrdup(str); |
| 707 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 708 | return ptr; |
| 709 | } |
| 710 | static void xxfree(void *ptr) |
| 711 | { |
| 712 | fdprintf(2, "free %p\n", ptr); |
| 713 | free(ptr); |
| 714 | } |
| 715 | #define xmalloc(s) xxmalloc(__LINE__, s) |
| 716 | #define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 717 | #define xstrdup(s) xxstrdup(__LINE__, s) |
| 718 | #define free(p) xxfree(p) |
| 719 | #endif |
| 720 | |
| 721 | |
| 722 | /* Syntax and runtime errors. They always abort scripts. |
| 723 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 724 | * and return to the prompt. |
| 725 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 726 | */ |
| 727 | #if HUSH_DEBUG < 2 |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 728 | # define die_if_script(lineno, fmt...) die_if_script(fmt) |
| 729 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 730 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 731 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 732 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 733 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 734 | #endif |
| 735 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 736 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 737 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 738 | va_list p; |
| 739 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 740 | #if HUSH_DEBUG >= 2 |
| 741 | bb_error_msg("hush.c:%u", lineno); |
| 742 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 743 | va_start(p, fmt); |
| 744 | bb_verror_msg(fmt, p, NULL); |
| 745 | va_end(p); |
| 746 | if (!G_interactive_fd) |
| 747 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 748 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 749 | |
| 750 | static void syntax_error(unsigned lineno, const char *msg) |
| 751 | { |
| 752 | if (msg) |
| 753 | die_if_script(lineno, "syntax error: %s", msg); |
| 754 | else |
| 755 | die_if_script(lineno, "syntax error", NULL); |
| 756 | } |
| 757 | |
| 758 | static void syntax_error_at(unsigned lineno, const char *msg) |
| 759 | { |
| 760 | die_if_script(lineno, "syntax error at '%s'", msg); |
| 761 | } |
| 762 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 763 | /* It so happens that all such cases are totally fatal |
| 764 | * even if shell is interactive: EOF while looking for closing |
| 765 | * delimiter. There is nowhere to read stuff from after that, |
| 766 | * it's EOF! The only choice is to terminate. |
| 767 | */ |
| 768 | static void syntax_error_unterm_ch(unsigned lineno, char ch) NORETURN; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 769 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 770 | { |
| 771 | char msg[2]; |
| 772 | msg[0] = ch; |
| 773 | msg[1] = '\0'; |
| 774 | die_if_script(lineno, "syntax error: unterminated %s", msg); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 775 | xfunc_die(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 778 | static void syntax_error_unterm_str(unsigned lineno, const char *s) |
| 779 | { |
| 780 | die_if_script(lineno, "syntax error: unterminated %s", s); |
| 781 | } |
| 782 | |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 783 | static void syntax_error_unexpected_ch(unsigned lineno, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 784 | { |
| 785 | char msg[2]; |
| 786 | msg[0] = ch; |
| 787 | msg[1] = '\0'; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 788 | die_if_script(lineno, "syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 791 | #if HUSH_DEBUG < 2 |
| 792 | # undef die_if_script |
| 793 | # undef syntax_error |
| 794 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 795 | # undef syntax_error_unterm_ch |
| 796 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 797 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 798 | #else |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 799 | # define die_if_script(fmt...) die_if_script(__LINE__, fmt) |
| 800 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 801 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 802 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 803 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 804 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 805 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 806 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 807 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 808 | #if ENABLE_HUSH_INTERACTIVE |
| 809 | static void cmdedit_update_prompt(void); |
| 810 | #else |
| 811 | # define cmdedit_update_prompt() |
| 812 | #endif |
| 813 | |
| 814 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 815 | /* Utility functions |
| 816 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 817 | static int glob_needed(const char *s) |
| 818 | { |
| 819 | while (*s) { |
| 820 | if (*s == '\\') |
| 821 | s++; |
| 822 | if (*s == '*' || *s == '[' || *s == '?') |
| 823 | return 1; |
| 824 | s++; |
| 825 | } |
| 826 | return 0; |
| 827 | } |
| 828 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 829 | static int is_well_formed_var_name(const char *s, char terminator) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 830 | { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 831 | if (!s || !(isalpha(*s) || *s == '_')) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 832 | return 0; |
| 833 | s++; |
| 834 | while (isalnum(*s) || *s == '_') |
| 835 | s++; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 836 | return *s == terminator; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 839 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 840 | static char *unbackslash(char *src) |
| 841 | { |
| 842 | char *dst = src; |
| 843 | while (1) { |
| 844 | if (*src == '\\') |
| 845 | src++; |
| 846 | if ((*dst++ = *src++) == '\0') |
| 847 | break; |
| 848 | } |
| 849 | return dst; |
| 850 | } |
| 851 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 852 | 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] | 853 | { |
| 854 | int i; |
| 855 | unsigned count1; |
| 856 | unsigned count2; |
| 857 | char **v; |
| 858 | |
| 859 | v = strings; |
| 860 | count1 = 0; |
| 861 | if (v) { |
| 862 | while (*v) { |
| 863 | count1++; |
| 864 | v++; |
| 865 | } |
| 866 | } |
| 867 | count2 = 0; |
| 868 | v = add; |
| 869 | while (*v) { |
| 870 | count2++; |
| 871 | v++; |
| 872 | } |
| 873 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 874 | v[count1 + count2] = NULL; |
| 875 | i = count2; |
| 876 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 877 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 878 | return v; |
| 879 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 880 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 881 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 882 | { |
| 883 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 884 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 885 | return ptr; |
| 886 | } |
| 887 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 888 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 889 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 890 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 891 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 892 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 893 | { |
| 894 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 895 | v[0] = add; |
| 896 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 897 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 898 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 899 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 900 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 901 | { |
| 902 | char **ptr = add_string_to_strings(strings, add); |
| 903 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 904 | return ptr; |
| 905 | } |
| 906 | #define add_string_to_strings(strings, add) \ |
| 907 | xx_add_string_to_strings(__LINE__, strings, add) |
| 908 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 909 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 910 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 911 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 912 | char **v; |
| 913 | |
| 914 | if (!strings) |
| 915 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 916 | v = strings; |
| 917 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 918 | free(*v); |
| 919 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 920 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 921 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 924 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 925 | /* Helpers for setting new $n and restoring them back |
| 926 | */ |
| 927 | typedef struct save_arg_t { |
| 928 | char *sv_argv0; |
| 929 | char **sv_g_argv; |
| 930 | int sv_g_argc; |
| 931 | smallint sv_g_malloced; |
| 932 | } save_arg_t; |
| 933 | |
| 934 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 935 | { |
| 936 | int n; |
| 937 | |
| 938 | sv->sv_argv0 = argv[0]; |
| 939 | sv->sv_g_argv = G.global_argv; |
| 940 | sv->sv_g_argc = G.global_argc; |
| 941 | sv->sv_g_malloced = G.global_args_malloced; |
| 942 | |
| 943 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 944 | G.global_argv = argv; |
| 945 | G.global_args_malloced = 0; |
| 946 | |
| 947 | n = 1; |
| 948 | while (*++argv) |
| 949 | n++; |
| 950 | G.global_argc = n; |
| 951 | } |
| 952 | |
| 953 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 954 | { |
| 955 | char **pp; |
| 956 | |
| 957 | if (G.global_args_malloced) { |
| 958 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 959 | pp = G.global_argv; |
| 960 | while (*++pp) /* note: does not free $0 */ |
| 961 | free(*pp); |
| 962 | free(G.global_argv); |
| 963 | } |
| 964 | argv[0] = sv->sv_argv0; |
| 965 | G.global_argv = sv->sv_g_argv; |
| 966 | G.global_argc = sv->sv_g_argc; |
| 967 | G.global_args_malloced = sv->sv_g_malloced; |
| 968 | } |
| 969 | |
| 970 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 971 | /* Basic theory of signal handling in shell |
| 972 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 973 | * This does not describe what hush does, rather, it is current understanding |
| 974 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 975 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 976 | * |
| 977 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 978 | * is finished or backgrounded. It is the same in interactive and |
| 979 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 980 | * 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] | 981 | * ^C or ^Z from keyboard seem to execute "at once" because it usually |
| 982 | * backgrounds (i.e. stops) or kills all members of currently running |
| 983 | * pipe. |
| 984 | * |
| 985 | * Wait builtin in interruptible by signals for which user trap is set |
| 986 | * or by SIGINT in interactive shell. |
| 987 | * |
| 988 | * Trap handlers will execute even within trap handlers. (right?) |
| 989 | * |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 990 | * User trap handlers are forgotten when subshell ("(cmd)") is entered. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 991 | * |
| 992 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 993 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 994 | * |
| 995 | * Commands run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 996 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 997 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 998 | * Ordinary commands have signals set to SIG_IGN/DFL set as inherited |
| 999 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1000 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1001 | * Siganls which differ from SIG_DFL action |
| 1002 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1003 | * |
| 1004 | * SIGQUIT: ignore |
| 1005 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1006 | * SIGHUP (interactive): |
| 1007 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1008 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1009 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1010 | * that all pipe members are stopped. Try this in bash: |
| 1011 | * while :; do :; done - ^Z does not background it |
| 1012 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1013 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1014 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1015 | * to interactive shell while shell is waiting for a pipe, |
| 1016 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1017 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1018 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1019 | * Example 2: this does not wait and does not execute ls: |
| 1020 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1021 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1022 | * "sleep 5; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1023 | * |
| 1024 | * (What happens to signals which are IGN on shell start?) |
| 1025 | * (What happens with signal mask on shell start?) |
| 1026 | * |
| 1027 | * Implementation in hush |
| 1028 | * ====================== |
| 1029 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1030 | * We block all signals which we don't want to take action immediately, |
| 1031 | * i.e. we block all signals which need to have special handling as described |
| 1032 | * above, and all signals which have traps set. |
| 1033 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1034 | * and act on them. |
| 1035 | * |
| 1036 | * unsigned non_DFL_mask: a mask of such "special" signals |
| 1037 | * sigset_t blocked_set: current blocked signal set |
| 1038 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1039 | * "trap - SIGxxx": |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1040 | * 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] | 1041 | * "trap 'cmd' SIGxxx": |
| 1042 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1043 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1044 | * unblock signals with special interactive handling |
| 1045 | * (child shell is not interactive), |
| 1046 | * unset all traps (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1047 | * after [v]fork, if we plan to exec: |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1048 | * POSIX says pending signal mask is cleared in child - no need to clear it. |
| 1049 | * 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] | 1050 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1051 | * Note: as a result, we do not use signal handlers much. The only uses |
| 1052 | * are to count SIGCHLDs [disabled - bug somewhere, + bloat] |
| 1053 | * and to restore tty pgrp on signal-induced exit. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1054 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1055 | enum { |
| 1056 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1057 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1058 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1059 | | (1 << SIGHUP) |
| 1060 | , |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1061 | SPECIAL_JOB_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1062 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1063 | | (1 << SIGTTIN) |
| 1064 | | (1 << SIGTTOU) |
| 1065 | | (1 << SIGTSTP) |
| 1066 | #endif |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1067 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1068 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1069 | //static void SIGCHLD_handler(int sig UNUSED_PARAM) |
| 1070 | //{ |
| 1071 | // G.count_SIGCHLD++; |
| 1072 | //} |
| 1073 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1074 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1075 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1076 | /* After [v]fork, in child: do not restore tty pgrp on xfunc death */ |
| 1077 | #define disable_restore_tty_pgrp_on_exit() (die_sleep = 0) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1078 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1079 | #define enable_restore_tty_pgrp_on_exit() (die_sleep = -1) |
| 1080 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1081 | /* Restores tty foreground process group, and exits. |
| 1082 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1083 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1084 | * or called directly with -EXITCODE. |
| 1085 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1086 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1087 | static void sigexit(int sig) |
| 1088 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1089 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 1090 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1091 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1092 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1093 | * tty pgrp then, only top-level shell process does that */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1094 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 1095 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1096 | |
| 1097 | /* Not a signal, just exit */ |
| 1098 | if (sig <= 0) |
| 1099 | _exit(- sig); |
| 1100 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1101 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1102 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1103 | #else |
| 1104 | |
| 1105 | #define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1106 | #define enable_restore_tty_pgrp_on_exit() ((void)0) |
| 1107 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1108 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1109 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1110 | /* Restores tty foreground process group, and exits. */ |
| 1111 | static void hush_exit(int exitcode) NORETURN; |
| 1112 | static void hush_exit(int exitcode) |
| 1113 | { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1114 | if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) { |
| 1115 | /* Prevent recursion: |
| 1116 | * trap "echo Hi; exit" EXIT; exit |
| 1117 | */ |
| 1118 | char *argv[] = { NULL, G.traps[0], NULL }; |
| 1119 | G.traps[0] = NULL; |
| 1120 | G.exiting = 1; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1121 | builtin_eval(argv); |
| 1122 | free(argv[1]); |
| 1123 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1124 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1125 | #if ENABLE_HUSH_JOB |
| 1126 | fflush(NULL); /* flush all streams */ |
| 1127 | sigexit(- (exitcode & 0xff)); |
| 1128 | #else |
| 1129 | exit(exitcode); |
| 1130 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1133 | static int check_and_run_traps(int sig) |
| 1134 | { |
| 1135 | static const struct timespec zero_timespec = { 0, 0 }; |
| 1136 | smalluint save_rcode; |
| 1137 | int last_sig = 0; |
| 1138 | |
| 1139 | if (sig) |
| 1140 | goto jump_in; |
| 1141 | while (1) { |
| 1142 | sig = sigtimedwait(&G.blocked_set, NULL, &zero_timespec); |
| 1143 | if (sig <= 0) |
| 1144 | break; |
| 1145 | jump_in: |
| 1146 | last_sig = sig; |
| 1147 | if (G.traps && G.traps[sig]) { |
| 1148 | if (G.traps[sig][0]) { |
| 1149 | /* We have user-defined handler */ |
| 1150 | char *argv[] = { NULL, xstrdup(G.traps[sig]), NULL }; |
| 1151 | save_rcode = G.last_exitcode; |
| 1152 | builtin_eval(argv); |
| 1153 | free(argv[1]); |
| 1154 | G.last_exitcode = save_rcode; |
| 1155 | } /* else: "" trap, ignoring signal */ |
| 1156 | continue; |
| 1157 | } |
| 1158 | /* not a trap: special action */ |
| 1159 | switch (sig) { |
| 1160 | // case SIGCHLD: |
| 1161 | // G.count_SIGCHLD++; |
| 1162 | // break; |
| 1163 | case SIGINT: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1164 | /* Builtin was ^C'ed, make it look prettier: */ |
| 1165 | bb_putchar('\n'); |
| 1166 | G.flag_SIGINT = 1; |
| 1167 | break; |
| 1168 | #if ENABLE_HUSH_JOB |
| 1169 | case SIGHUP: { |
| 1170 | struct pipe *job; |
| 1171 | /* bash is observed to signal whole process groups, |
| 1172 | * not individual processes */ |
| 1173 | for (job = G.job_list; job; job = job->next) { |
| 1174 | if (job->pgrp <= 0) |
| 1175 | continue; |
| 1176 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 1177 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 1178 | kill(- job->pgrp, SIGCONT); |
| 1179 | } |
| 1180 | sigexit(SIGHUP); |
| 1181 | } |
| 1182 | #endif |
| 1183 | default: /* ignored: */ |
| 1184 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
| 1185 | break; |
| 1186 | } |
| 1187 | } |
| 1188 | return last_sig; |
| 1189 | } |
| 1190 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1191 | |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1192 | static const char *set_cwd(void) |
| 1193 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1194 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1195 | * we must not try to free(bb_msg_unknown) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1196 | if (G.cwd == bb_msg_unknown) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1197 | G.cwd = NULL; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1198 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1199 | if (!G.cwd) |
| 1200 | G.cwd = bb_msg_unknown; |
| 1201 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1204 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1205 | /* |
| 1206 | * Shell and environment variable support |
| 1207 | */ |
| 1208 | static struct variable **get_ptr_to_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1209 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1210 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1211 | struct variable *cur; |
| 1212 | int len; |
| 1213 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1214 | len = strlen(name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1215 | pp = &G.top_var; |
| 1216 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1217 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1218 | return pp; |
| 1219 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1220 | } |
| 1221 | return NULL; |
| 1222 | } |
| 1223 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1224 | static struct variable *get_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1225 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1226 | struct variable **pp = get_ptr_to_local_var(name); |
| 1227 | if (pp) |
| 1228 | return *pp; |
| 1229 | return NULL; |
| 1230 | } |
| 1231 | |
| 1232 | static const char *get_local_var_value(const char *name) |
| 1233 | { |
| 1234 | struct variable **pp = get_ptr_to_local_var(name); |
| 1235 | if (pp) |
| 1236 | return strchr((*pp)->varstr, '=') + 1; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1237 | return NULL; |
| 1238 | } |
| 1239 | |
| 1240 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1241 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1242 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1243 | * 0: do not change export flag |
| 1244 | * (if creating new variable, flag will be 0) |
| 1245 | * 1: set export flag and putenv the variable |
| 1246 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1247 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1248 | */ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1249 | #if BB_MMU |
| 1250 | #define set_local_var(str, flg_export, flg_read_only) \ |
| 1251 | set_local_var(str, flg_export) |
| 1252 | #endif |
| 1253 | 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] | 1254 | { |
| 1255 | struct variable *cur; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1256 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1257 | int name_len; |
| 1258 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1259 | eq_sign = strchr(str, '='); |
| 1260 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1261 | free(str); |
| 1262 | return -1; |
| 1263 | } |
| 1264 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1265 | name_len = eq_sign - str + 1; /* including '=' */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1266 | cur = G.top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */ |
| 1267 | while (1) { |
| 1268 | if (strncmp(cur->varstr, str, name_len) != 0) { |
| 1269 | if (!cur->next) { |
| 1270 | /* Bail out. Note that now cur points |
| 1271 | * to last var in linked list */ |
| 1272 | break; |
| 1273 | } |
| 1274 | cur = cur->next; |
| 1275 | continue; |
| 1276 | } |
| 1277 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1278 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1279 | #if !BB_MMU |
| 1280 | if (!flg_read_only) |
| 1281 | #endif |
| 1282 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1283 | free(str); |
| 1284 | return -1; |
| 1285 | } |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1286 | if (flg_export == -1) { |
| 1287 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1288 | *eq_sign = '\0'; |
| 1289 | unsetenv(str); |
| 1290 | *eq_sign = '='; |
| 1291 | } |
| 1292 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 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; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1322 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1323 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1324 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1325 | if (flg_export == -1) { |
| 1326 | cur->flg_export = 0; |
| 1327 | /* unsetenv was already done */ |
| 1328 | } else { |
| 1329 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1330 | return putenv(cur->varstr); |
| 1331 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1332 | } |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1336 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1337 | { |
| 1338 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1339 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1340 | |
| 1341 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1342 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1343 | var_pp = &G.top_var; |
| 1344 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1345 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1346 | if (cur->flg_read_only) { |
| 1347 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1348 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1349 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1350 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1351 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1352 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1353 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1354 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1355 | if (!cur->max_len) |
| 1356 | free(cur->varstr); |
| 1357 | free(cur); |
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 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1360 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1361 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1362 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1365 | static int unset_local_var(const char *name) |
| 1366 | { |
| 1367 | return unset_local_var_len(name, strlen(name)); |
| 1368 | } |
| 1369 | |
| 1370 | static void unset_vars(char **strings) |
| 1371 | { |
| 1372 | char **v; |
| 1373 | |
| 1374 | if (!strings) |
| 1375 | return; |
| 1376 | v = strings; |
| 1377 | while (*v) { |
| 1378 | const char *eq = strchrnul(*v, '='); |
| 1379 | unset_local_var_len(*v, (int)(eq - *v)); |
| 1380 | v++; |
| 1381 | } |
| 1382 | free(strings); |
| 1383 | } |
| 1384 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1385 | #if ENABLE_SH_MATH_SUPPORT |
| 1386 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1387 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 1388 | static char *endofname(const char *name) |
| 1389 | { |
| 1390 | char *p; |
| 1391 | |
| 1392 | p = (char *) name; |
| 1393 | if (!is_name(*p)) |
| 1394 | return p; |
| 1395 | while (*++p) { |
| 1396 | if (!is_in_name(*p)) |
| 1397 | break; |
| 1398 | } |
| 1399 | return p; |
| 1400 | } |
| 1401 | |
| 1402 | static void arith_set_local_var(const char *name, const char *val, int flags) |
| 1403 | { |
| 1404 | /* arith code doesnt malloc space, so do it for it */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1405 | char *var = xasprintf("%s=%s", name, val); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1406 | set_local_var(var, flags, 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1407 | } |
| 1408 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1409 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1410 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1411 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1412 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 1413 | */ |
| 1414 | static void add_vars(struct variable *var) |
| 1415 | { |
| 1416 | struct variable *next; |
| 1417 | |
| 1418 | while (var) { |
| 1419 | next = var->next; |
| 1420 | var->next = G.top_var; |
| 1421 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1422 | if (var->flg_export) { |
| 1423 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1424 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1425 | } else { |
| 1426 | debug_printf_env("%s: restoring local '%s'\n", __func__, var->varstr); |
| 1427 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1428 | var = next; |
| 1429 | } |
| 1430 | } |
| 1431 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1432 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1433 | { |
| 1434 | char **s; |
| 1435 | struct variable *old = NULL; |
| 1436 | |
| 1437 | if (!strings) |
| 1438 | return old; |
| 1439 | s = strings; |
| 1440 | while (*s) { |
| 1441 | struct variable *var_p; |
| 1442 | struct variable **var_pp; |
| 1443 | char *eq; |
| 1444 | |
| 1445 | eq = strchr(*s, '='); |
| 1446 | if (eq) { |
| 1447 | *eq = '\0'; |
| 1448 | var_pp = get_ptr_to_local_var(*s); |
| 1449 | *eq = '='; |
| 1450 | if (var_pp) { |
| 1451 | /* Remove variable from global linked list */ |
| 1452 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1453 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1454 | *var_pp = var_p->next; |
| 1455 | /* Add it to returned list */ |
| 1456 | var_p->next = old; |
| 1457 | old = var_p; |
| 1458 | } |
| 1459 | set_local_var(*s, 1, 0); |
| 1460 | } |
| 1461 | s++; |
| 1462 | } |
| 1463 | return old; |
| 1464 | } |
| 1465 | |
| 1466 | |
| 1467 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1468 | * in_str support |
| 1469 | */ |
| 1470 | static int static_get(struct in_str *i) |
| 1471 | { |
| 1472 | int ch = *i->p++; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1473 | if (ch != '\0') |
| 1474 | return ch; |
| 1475 | i->p--; |
| 1476 | return EOF; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | static int static_peek(struct in_str *i) |
| 1480 | { |
| 1481 | return *i->p; |
| 1482 | } |
| 1483 | |
| 1484 | #if ENABLE_HUSH_INTERACTIVE |
| 1485 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1486 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1487 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1488 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1489 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1490 | if (G.PS1 == NULL) |
| 1491 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1492 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1493 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1494 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1495 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1496 | if (G.PS2 == NULL) |
| 1497 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1498 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1499 | |
| 1500 | static const char* setup_prompt_string(int promptmode) |
| 1501 | { |
| 1502 | const char *prompt_str; |
| 1503 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1504 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1505 | /* Set up the prompt */ |
| 1506 | if (promptmode == 0) { /* PS1 */ |
| 1507 | free((char*)G.PS1); |
| 1508 | G.PS1 = xasprintf("%s %c ", G.cwd, (geteuid() != 0) ? '$' : '#'); |
| 1509 | prompt_str = G.PS1; |
| 1510 | } else |
| 1511 | prompt_str = G.PS2; |
| 1512 | } else |
| 1513 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1514 | debug_printf("result '%s'\n", prompt_str); |
| 1515 | return prompt_str; |
| 1516 | } |
| 1517 | |
| 1518 | static void get_user_input(struct in_str *i) |
| 1519 | { |
| 1520 | int r; |
| 1521 | const char *prompt_str; |
| 1522 | |
| 1523 | prompt_str = setup_prompt_string(i->promptmode); |
| 1524 | #if ENABLE_FEATURE_EDITING |
| 1525 | /* Enable command line editing only while a command line |
| 1526 | * is actually being read */ |
| 1527 | do { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1528 | G.flag_SIGINT = 0; |
| 1529 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 1530 | * only after <Enter>. (^C will work) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1531 | 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] | 1532 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1533 | check_and_run_traps(0); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1534 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1535 | i->eof_flag = (r < 0); |
| 1536 | if (i->eof_flag) { /* EOF/error detected */ |
| 1537 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1538 | G.user_input_buf[1] = '\0'; |
| 1539 | } |
| 1540 | #else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1541 | do { |
| 1542 | G.flag_SIGINT = 0; |
| 1543 | fputs(prompt_str, stdout); |
| 1544 | fflush(stdout); |
| 1545 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1546 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1547 | //do we need check_and_run_traps(0)? (maybe only if stdin) |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1548 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1549 | i->eof_flag = (r == EOF); |
| 1550 | #endif |
| 1551 | i->p = G.user_input_buf; |
| 1552 | } |
| 1553 | |
| 1554 | #endif /* INTERACTIVE */ |
| 1555 | |
| 1556 | /* This is the magic location that prints prompts |
| 1557 | * and gets data back from the user */ |
| 1558 | static int file_get(struct in_str *i) |
| 1559 | { |
| 1560 | int ch; |
| 1561 | |
| 1562 | /* If there is data waiting, eat it up */ |
| 1563 | if (i->p && *i->p) { |
| 1564 | #if ENABLE_HUSH_INTERACTIVE |
| 1565 | take_cached: |
| 1566 | #endif |
| 1567 | ch = *i->p++; |
| 1568 | if (i->eof_flag && !*i->p) |
| 1569 | ch = EOF; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1570 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1571 | } else { |
| 1572 | /* need to double check i->file because we might be doing something |
| 1573 | * more complicated by now, like sourcing or substituting. */ |
| 1574 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 1575 | if (G_interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1576 | do { |
| 1577 | get_user_input(i); |
| 1578 | } while (!*i->p); /* need non-empty line */ |
| 1579 | i->promptmode = 1; /* PS2 */ |
| 1580 | i->promptme = 0; |
| 1581 | goto take_cached; |
| 1582 | } |
| 1583 | #endif |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1584 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1585 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1586 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1587 | #if ENABLE_HUSH_INTERACTIVE |
| 1588 | if (ch == '\n') |
| 1589 | i->promptme = 1; |
| 1590 | #endif |
| 1591 | return ch; |
| 1592 | } |
| 1593 | |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1594 | /* All callers guarantee this routine will never |
| 1595 | * be used right after a newline, so prompting is not needed. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1596 | */ |
| 1597 | static int file_peek(struct in_str *i) |
| 1598 | { |
| 1599 | int ch; |
| 1600 | if (i->p && *i->p) { |
| 1601 | if (i->eof_flag && !i->p[1]) |
| 1602 | return EOF; |
| 1603 | return *i->p; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1604 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1605 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1606 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1607 | i->eof_flag = (ch == EOF); |
| 1608 | i->peek_buf[0] = ch; |
| 1609 | i->peek_buf[1] = '\0'; |
| 1610 | i->p = i->peek_buf; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1611 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1612 | return ch; |
| 1613 | } |
| 1614 | |
| 1615 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1616 | { |
| 1617 | i->peek = file_peek; |
| 1618 | i->get = file_get; |
| 1619 | #if ENABLE_HUSH_INTERACTIVE |
| 1620 | i->promptme = 1; |
| 1621 | i->promptmode = 0; /* PS1 */ |
| 1622 | #endif |
| 1623 | i->file = f; |
| 1624 | i->p = NULL; |
| 1625 | } |
| 1626 | |
| 1627 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1628 | { |
| 1629 | i->peek = static_peek; |
| 1630 | i->get = static_get; |
| 1631 | #if ENABLE_HUSH_INTERACTIVE |
| 1632 | i->promptme = 1; |
| 1633 | i->promptmode = 0; /* PS1 */ |
| 1634 | #endif |
| 1635 | i->p = s; |
| 1636 | i->eof_flag = 0; |
| 1637 | } |
| 1638 | |
| 1639 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1640 | /* |
| 1641 | * o_string support |
| 1642 | */ |
| 1643 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1644 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1645 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1646 | { |
| 1647 | o->length = 0; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 1648 | o->o_quoted = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1649 | if (o->data) |
| 1650 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1653 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1654 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1655 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1656 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1659 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 1660 | { |
| 1661 | free(o->data); |
| 1662 | } |
| 1663 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1664 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1665 | { |
| 1666 | if (o->length + len > o->maxlen) { |
| 1667 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 1668 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 1669 | } |
| 1670 | } |
| 1671 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1672 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1673 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1674 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 1675 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1676 | o->data[o->length] = ch; |
| 1677 | o->length++; |
| 1678 | o->data[o->length] = '\0'; |
| 1679 | } |
| 1680 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1681 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1682 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1683 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1684 | memcpy(&o->data[o->length], str, len); |
| 1685 | o->length += len; |
| 1686 | o->data[o->length] = '\0'; |
| 1687 | } |
| 1688 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1689 | #if !BB_MMU |
| 1690 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1691 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1692 | o_addblock(o, str, strlen(str)); |
| 1693 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1694 | static void nommu_addchr(o_string *o, int ch) |
| 1695 | { |
| 1696 | if (o) |
| 1697 | o_addchr(o, ch); |
| 1698 | } |
| 1699 | #else |
| 1700 | #define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1701 | #endif |
| 1702 | |
| 1703 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 1704 | { |
| 1705 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1708 | 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] | 1709 | { |
| 1710 | while (len) { |
| 1711 | o_addchr(o, *str); |
| 1712 | if (*str++ == '\\' |
| 1713 | && (*str != '*' && *str != '?' && *str != '[') |
| 1714 | ) { |
| 1715 | o_addchr(o, '\\'); |
| 1716 | } |
| 1717 | len--; |
| 1718 | } |
| 1719 | } |
| 1720 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1721 | /* My analysis of quoting semantics tells me that state information |
| 1722 | * is associated with a destination, not a source. |
| 1723 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1724 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1725 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1726 | int sz = 1; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1727 | char *found = strchr("*?[\\", ch); |
| 1728 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1729 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1730 | o_grow_by(o, sz); |
| 1731 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1732 | o->data[o->length] = '\\'; |
| 1733 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1734 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1735 | o->data[o->length] = ch; |
| 1736 | o->length++; |
| 1737 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1740 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1741 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1742 | int sz = 1; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1743 | if (o->o_escape && strchr("*?[\\", ch)) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1744 | sz++; |
| 1745 | o->data[o->length] = '\\'; |
| 1746 | o->length++; |
| 1747 | } |
| 1748 | o_grow_by(o, sz); |
| 1749 | o->data[o->length] = ch; |
| 1750 | o->length++; |
| 1751 | o->data[o->length] = '\0'; |
| 1752 | } |
| 1753 | |
| 1754 | static void o_addQstr(o_string *o, const char *str, int len) |
| 1755 | { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1756 | if (!o->o_escape) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1757 | o_addblock(o, str, len); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1758 | return; |
| 1759 | } |
| 1760 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1761 | char ch; |
| 1762 | int sz; |
| 1763 | int ordinary_cnt = strcspn(str, "*?[\\"); |
| 1764 | if (ordinary_cnt > len) /* paranoia */ |
| 1765 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1766 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1767 | if (ordinary_cnt == len) |
| 1768 | return; |
| 1769 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1770 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1771 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1772 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1773 | sz = 1; |
| 1774 | if (ch) { /* it is necessarily one of "*?[\\" */ |
| 1775 | sz++; |
| 1776 | o->data[o->length] = '\\'; |
| 1777 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1778 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1779 | o_grow_by(o, sz); |
| 1780 | o->data[o->length] = ch; |
| 1781 | o->length++; |
| 1782 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1783 | } |
| 1784 | } |
| 1785 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1786 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1787 | * 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] | 1788 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1789 | * list[i] contains an INDEX (int!) into this string data. |
| 1790 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1791 | * but list[i]'s need not be modified. |
| 1792 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1793 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1794 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1795 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1796 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1797 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1798 | { |
| 1799 | char **list = (char**)o->data; |
| 1800 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1801 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1802 | |
| 1803 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1804 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1805 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1806 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1807 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1808 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1809 | o->data + (int)list[i] + string_start, |
| 1810 | o->data + (int)list[i] + string_start); |
| 1811 | i++; |
| 1812 | } |
| 1813 | if (n) { |
| 1814 | const char *p = o->data + (int)list[n - 1] + string_start; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1815 | indent(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1816 | 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] | 1817 | } |
| 1818 | } |
| 1819 | #else |
| 1820 | #define debug_print_list(prefix, o, n) ((void)0) |
| 1821 | #endif |
| 1822 | |
| 1823 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1824 | * in list[n] so that it points past last stored byte so far. |
| 1825 | * It returns n+1. */ |
| 1826 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1827 | { |
| 1828 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1829 | int string_start; |
| 1830 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1831 | |
| 1832 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1833 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1834 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1835 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1836 | 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] | 1837 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1838 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1839 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 1840 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1841 | memmove(list + n + 0x10, list + n, string_len); |
| 1842 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1843 | } else { |
| 1844 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 1845 | n, string_len, string_start); |
| 1846 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1847 | } else { |
| 1848 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1849 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1850 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1851 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 1852 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1853 | o->has_empty_slot = 0; |
| 1854 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1855 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1856 | return n + 1; |
| 1857 | } |
| 1858 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1859 | /* "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] | 1860 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1861 | { |
| 1862 | char **list = (char**)o->data; |
| 1863 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1864 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1865 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1868 | /* o_glob performs globbing on last list[], saving each result |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1869 | * as a new list[]. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1870 | static int o_glob(o_string *o, int n) |
| 1871 | { |
| 1872 | glob_t globdata; |
| 1873 | int gr; |
| 1874 | char *pattern; |
| 1875 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1876 | 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] | 1877 | if (!o->data) |
| 1878 | return o_save_ptr_helper(o, n); |
| 1879 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1880 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1881 | if (!glob_needed(pattern)) { |
| 1882 | literal: |
| 1883 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1884 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1885 | return o_save_ptr_helper(o, n); |
| 1886 | } |
| 1887 | |
| 1888 | memset(&globdata, 0, sizeof(globdata)); |
| 1889 | gr = glob(pattern, 0, NULL, &globdata); |
| 1890 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 1891 | if (gr == GLOB_NOSPACE) |
| 1892 | bb_error_msg_and_die("out of memory during glob"); |
| 1893 | if (gr == GLOB_NOMATCH) { |
| 1894 | globfree(&globdata); |
| 1895 | goto literal; |
| 1896 | } |
| 1897 | if (gr != 0) { /* GLOB_ABORTED ? */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 1898 | /* TODO: testcase for bad glob pattern behavior */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1899 | bb_error_msg("glob(3) error %d on '%s'", gr, pattern); |
| 1900 | } |
| 1901 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 1902 | char **argv = globdata.gl_pathv; |
| 1903 | o->length = pattern - o->data; /* "forget" pattern */ |
| 1904 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1905 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1906 | n = o_save_ptr_helper(o, n); |
| 1907 | argv++; |
| 1908 | if (!*argv) |
| 1909 | break; |
| 1910 | } |
| 1911 | } |
| 1912 | globfree(&globdata); |
| 1913 | if (DEBUG_GLOB) |
| 1914 | debug_print_list("o_glob returning", o, n); |
| 1915 | return n; |
| 1916 | } |
| 1917 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1918 | /* If o->o_glob == 1, glob the string so far remembered. |
| 1919 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1920 | static int o_save_ptr(o_string *o, int n) |
| 1921 | { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 1922 | if (o->o_glob) { /* if globbing is requested */ |
| 1923 | /* If o->has_empty_slot, list[n] was already globbed |
| 1924 | * (if it was requested back then when it was filled) |
| 1925 | * so don't do that again! */ |
| 1926 | if (!o->has_empty_slot) |
| 1927 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 1928 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1929 | return o_save_ptr_helper(o, n); |
| 1930 | } |
| 1931 | |
| 1932 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1933 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1934 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1935 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1936 | int string_start; |
| 1937 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1938 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 1939 | if (DEBUG_EXPAND) |
| 1940 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1941 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1942 | list = (char**)o->data; |
| 1943 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1944 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1945 | while (n) { |
| 1946 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1947 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1948 | } |
| 1949 | return list; |
| 1950 | } |
| 1951 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1952 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1953 | /* Expansion can recurse */ |
| 1954 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 1955 | static int process_command_subs(o_string *dest, const char *s); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1956 | #endif |
| 1957 | static char *expand_string_to_string(const char *str); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1958 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 1959 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1960 | parse_stream_dquoted(dest, input, dquote_end) |
| 1961 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 1962 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1963 | o_string *dest, |
| 1964 | struct in_str *input, |
| 1965 | int dquote_end); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1966 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1967 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 1968 | * all variable references within and returns a pointer to |
| 1969 | * a list of expanded strings, possibly with larger number |
| 1970 | * of strings. (Think VAR="a b"; echo $VAR). |
| 1971 | * This new list is allocated as a single malloc block. |
| 1972 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 1973 | * followed by strings themself. |
| 1974 | * Caller can deallocate entire list by single free(list). */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1975 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1976 | /* Store given string, finalizing the word and starting new one whenever |
| 1977 | * we encounter IFS char(s). This is used for expanding variable values. |
| 1978 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 1979 | 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] | 1980 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1981 | while (1) { |
| 1982 | int word_len = strcspn(str, G.ifs); |
| 1983 | if (word_len) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1984 | if (output->o_escape || !output->o_glob) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1985 | o_addQstr(output, str, word_len); |
| 1986 | else /* protect backslashes against globbing up :) */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1987 | o_addblock_duplicate_backslash(output, str, word_len); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1988 | str += word_len; |
| 1989 | } |
| 1990 | if (!*str) /* EOL - do not finalize word */ |
| 1991 | break; |
| 1992 | o_addchr(output, '\0'); |
| 1993 | debug_print_list("expand_on_ifs", output, n); |
| 1994 | n = o_save_ptr(output, n); |
| 1995 | str += strspn(str, G.ifs); /* skip ifs chars */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1996 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1997 | debug_print_list("expand_on_ifs[1]", output, n); |
| 1998 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2001 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 2002 | * they are in double quotes, with the exception that they are not :). |
| 2003 | * Just the rules are similar: "expand only $var and `cmd`" |
| 2004 | * |
| 2005 | * Returns malloced string. |
| 2006 | * As an optimization, we return NULL if expansion is not needed. |
| 2007 | */ |
| 2008 | static char *expand_pseudo_dquoted(const char *str) |
| 2009 | { |
| 2010 | char *exp_str; |
| 2011 | struct in_str input; |
| 2012 | o_string dest = NULL_O_STRING; |
| 2013 | |
| 2014 | if (strchr(str, '$') == NULL |
| 2015 | #if ENABLE_HUSH_TICK |
| 2016 | && strchr(str, '`') == NULL |
| 2017 | #endif |
| 2018 | ) { |
| 2019 | return NULL; |
| 2020 | } |
| 2021 | |
| 2022 | /* We need to expand. Example: |
| 2023 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 2024 | */ |
| 2025 | setup_string_in_str(&input, str); |
| 2026 | parse_stream_dquoted(NULL, &dest, &input, EOF); |
| 2027 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
| 2028 | exp_str = expand_string_to_string(dest.data); |
| 2029 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 2030 | o_free_unsafe(&dest); |
| 2031 | return exp_str; |
| 2032 | } |
| 2033 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2034 | /* Expand all variable references in given string, adding words to list[] |
| 2035 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 2036 | * to be filled). This routine is extremely tricky: has to deal with |
| 2037 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 2038 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
| 2039 | 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] | 2040 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2041 | /* or_mask is either 0 (normal case) or 0x80 |
| 2042 | * (expansion of right-hand side of assignment == 1-element expand. |
| 2043 | * 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] | 2044 | |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2045 | char ored_ch; |
| 2046 | char *p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2047 | |
| 2048 | ored_ch = 0; |
| 2049 | |
| 2050 | debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg); |
| 2051 | debug_print_list("expand_vars_to_list", output, n); |
| 2052 | n = o_save_ptr(output, n); |
| 2053 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 2054 | |
| 2055 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2056 | char first_ch; |
| 2057 | int i; |
| 2058 | char *dyn_val = NULL; |
| 2059 | const char *val = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2060 | #if ENABLE_HUSH_TICK |
| 2061 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2062 | #endif |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2063 | #if ENABLE_SH_MATH_SUPPORT |
| 2064 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 2065 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2066 | o_addblock(output, arg, p - arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2067 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 2068 | arg = ++p; |
| 2069 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 2070 | |
| 2071 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
| 2072 | /* "$@" is special. Even if quoted, it can still |
| 2073 | * expand to nothing (not even an empty string) */ |
| 2074 | if ((first_ch & 0x7f) != '@') |
| 2075 | ored_ch |= first_ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2076 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2077 | switch (first_ch & 0x7f) { |
| 2078 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 2079 | case '$': /* pid */ |
| 2080 | val = utoa(G.root_pid); |
| 2081 | break; |
| 2082 | case '!': /* bg pid */ |
| 2083 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
| 2084 | break; |
| 2085 | case '?': /* exitcode */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 2086 | val = utoa(G.last_exitcode); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2087 | break; |
| 2088 | case '#': /* argc */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2089 | if (arg[1] != SPECIAL_VAR_SYMBOL) |
| 2090 | /* actually, it's a ${#var} */ |
| 2091 | goto case_default; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2092 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 2093 | break; |
| 2094 | case '*': |
| 2095 | case '@': |
| 2096 | i = 1; |
| 2097 | if (!G.global_argv[i]) |
| 2098 | break; |
| 2099 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
| 2100 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2101 | smallint sv = output->o_escape; |
| 2102 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2103 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2104 | while (G.global_argv[i]) { |
| 2105 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 2106 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 2107 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 2108 | /* this argv[] is not empty and not last: |
| 2109 | * put terminating NUL, start new word */ |
| 2110 | o_addchr(output, '\0'); |
| 2111 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 2112 | n = o_save_ptr(output, n); |
| 2113 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 2114 | } |
| 2115 | } |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2116 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2117 | } else |
| 2118 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
| 2119 | * and in this case should treat it like '$*' - see 'else...' below */ |
| 2120 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
| 2121 | while (1) { |
| 2122 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2123 | if (++i >= G.global_argc) |
| 2124 | break; |
| 2125 | o_addchr(output, '\0'); |
| 2126 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 2127 | n = o_save_ptr(output, n); |
| 2128 | } |
| 2129 | } else { /* quoted $*: add as one word */ |
| 2130 | while (1) { |
| 2131 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2132 | if (!G.global_argv[++i]) |
| 2133 | break; |
| 2134 | if (G.ifs[0]) |
| 2135 | o_addchr(output, G.ifs[0]); |
| 2136 | } |
| 2137 | } |
| 2138 | break; |
| 2139 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 2140 | /* "Empty variable", used to make "" etc to not disappear */ |
| 2141 | arg++; |
| 2142 | ored_ch = 0x80; |
| 2143 | break; |
| 2144 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2145 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2146 | *p = '\0'; |
| 2147 | arg++; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2148 | /* Can't just stuff it into output o_string, |
| 2149 | * expanded result may need to be globbed |
| 2150 | * and $IFS-splitted */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2151 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2152 | process_command_subs(&subst_result, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2153 | debug_printf_subst("SUBST RES '%s'\n", subst_result.data); |
| 2154 | val = subst_result.data; |
| 2155 | goto store_val; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2156 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2157 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2158 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2159 | arith_eval_hooks_t hooks; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2160 | arith_t res; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2161 | int errcode; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2162 | char *exp_str; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2163 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2164 | arg++; /* skip '+' */ |
| 2165 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2166 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2167 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2168 | exp_str = expand_pseudo_dquoted(arg); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 2169 | hooks.lookupvar = get_local_var_value; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2170 | hooks.setvar = arith_set_local_var; |
| 2171 | hooks.endofname = endofname; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2172 | res = arith(exp_str ? exp_str : arg, &errcode, &hooks); |
| 2173 | free(exp_str); |
| 2174 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2175 | if (errcode < 0) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2176 | const char *msg = "error in arithmetic"; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2177 | switch (errcode) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2178 | case -3: |
| 2179 | msg = "exponent less than 0"; |
| 2180 | break; |
| 2181 | case -2: |
| 2182 | msg = "divide by 0"; |
| 2183 | break; |
| 2184 | case -5: |
| 2185 | msg = "expression recursion loop detected"; |
| 2186 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2187 | } |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2188 | die_if_script(msg); |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2189 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2190 | debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2191 | sprintf(arith_buf, arith_t_fmt, res); |
| 2192 | val = arith_buf; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2193 | break; |
| 2194 | } |
| 2195 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2196 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2197 | case_default: { |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2198 | bool exp_len = false; |
| 2199 | bool exp_null = false; |
| 2200 | char *var = arg; |
| 2201 | char exp_save = exp_save; /* for compiler */ |
| 2202 | char exp_op = exp_op; /* for compiler */ |
| 2203 | char *exp_word = exp_word; /* for compiler */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2204 | size_t exp_off = 0; |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2205 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2206 | *p = '\0'; |
| 2207 | arg[0] = first_ch & 0x7f; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2208 | |
| 2209 | /* prepare for expansions */ |
| 2210 | if (var[0] == '#') { |
| 2211 | /* handle length expansion ${#var} */ |
| 2212 | exp_len = true; |
| 2213 | ++var; |
| 2214 | } else { |
| 2215 | /* maybe handle parameter expansion */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2216 | exp_off = strcspn(var, ":-=+?%#"); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2217 | if (!var[exp_off]) |
| 2218 | exp_off = 0; |
| 2219 | if (exp_off) { |
| 2220 | exp_save = var[exp_off]; |
| 2221 | exp_null = exp_save == ':'; |
| 2222 | exp_word = var + exp_off; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2223 | if (exp_null) |
| 2224 | ++exp_word; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2225 | exp_op = *exp_word++; |
| 2226 | var[exp_off] = '\0'; |
| 2227 | } |
| 2228 | } |
| 2229 | |
| 2230 | /* lookup the variable in question */ |
| 2231 | if (isdigit(var[0])) { |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 2232 | /* handle_dollar() should have vetted var for us */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2233 | i = xatoi_u(var); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2234 | if (i < G.global_argc) |
| 2235 | val = G.global_argv[i]; |
| 2236 | /* else val remains NULL: $N with too big N */ |
| 2237 | } else |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 2238 | val = get_local_var_value(var); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2239 | |
| 2240 | /* handle any expansions */ |
| 2241 | if (exp_len) { |
| 2242 | debug_printf_expand("expand: length of '%s' = ", val); |
| 2243 | val = utoa(val ? strlen(val) : 0); |
| 2244 | debug_printf_expand("%s\n", val); |
| 2245 | } else if (exp_off) { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2246 | if (exp_op == '%' || exp_op == '#') { |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2247 | if (val) { |
| 2248 | /* we need to do a pattern match */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2249 | bool match_at_left; |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2250 | char *loc; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2251 | scan_t scan = pick_scan(exp_op, *exp_word, &match_at_left); |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2252 | if (exp_op == *exp_word) /* ## or %% */ |
| 2253 | ++exp_word; |
| 2254 | val = dyn_val = xstrdup(val); |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2255 | loc = scan(dyn_val, exp_word, match_at_left); |
| 2256 | if (match_at_left) /* # or ## */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2257 | val = loc; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2258 | else if (loc) /* % or %% and match was found */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2259 | *loc = '\0'; |
| 2260 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2261 | } else { |
| 2262 | /* we need to do an expansion */ |
| 2263 | int exp_test = (!val || (exp_null && !val[0])); |
| 2264 | if (exp_op == '+') |
| 2265 | exp_test = !exp_test; |
| 2266 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 2267 | exp_null ? "true" : "false", exp_test); |
| 2268 | if (exp_test) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2269 | if (exp_op == '?') { |
| 2270 | //TODO: how interactive bash aborts expansion mid-command? |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2271 | /* ${var?[error_msg_if_unset]} */ |
| 2272 | /* ${var:?[error_msg_if_unset_or_null]} */ |
| 2273 | /* mimic bash message */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2274 | die_if_script("%s: %s", |
| 2275 | var, |
| 2276 | exp_word[0] ? exp_word : "parameter null or not set" |
| 2277 | ); |
| 2278 | } else { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2279 | val = exp_word; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2280 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2281 | |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2282 | if (exp_op == '=') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2283 | /* ${var=[word]} or ${var:=[word]} */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2284 | if (isdigit(var[0]) || var[0] == '#') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2285 | /* mimic bash message */ |
| 2286 | die_if_script("$%s: cannot assign in this way", var); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2287 | val = NULL; |
| 2288 | } else { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2289 | char *new_var = xasprintf("%s=%s", var, val); |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2290 | set_local_var(new_var, 0, 0); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2291 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2292 | } |
| 2293 | } |
| 2294 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2295 | |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2296 | var[exp_off] = exp_save; |
| 2297 | } |
| 2298 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2299 | arg[0] = first_ch; |
| 2300 | #if ENABLE_HUSH_TICK |
| 2301 | store_val: |
| 2302 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2303 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2304 | 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] | 2305 | if (val) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2306 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2307 | smallint sv = output->o_escape; |
| 2308 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2309 | n = expand_on_ifs(output, n, val); |
| 2310 | val = NULL; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2311 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2312 | } |
| 2313 | } else { /* quoted $VAR, val will be appended below */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2314 | 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] | 2315 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2316 | } /* default: */ |
| 2317 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2318 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2319 | if (val) { |
| 2320 | o_addQstr(output, val, strlen(val)); |
| 2321 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2322 | free(dyn_val); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2323 | /* Do the check to avoid writing to a const string */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2324 | if (*p != SPECIAL_VAR_SYMBOL) |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2325 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2326 | |
| 2327 | #if ENABLE_HUSH_TICK |
| 2328 | o_free(&subst_result); |
| 2329 | #endif |
| 2330 | arg = ++p; |
| 2331 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 2332 | |
| 2333 | if (arg[0]) { |
| 2334 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 2335 | /* this part is literal, and it was already pre-quoted |
| 2336 | * if needed (much earlier), do not use o_addQstr here! */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2337 | o_addstr_with_NUL(output, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2338 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 2339 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 2340 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 2341 | ) { |
| 2342 | n--; |
| 2343 | /* allow to reuse list[n] later without re-growth */ |
| 2344 | output->has_empty_slot = 1; |
| 2345 | } else { |
| 2346 | o_addchr(output, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2347 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2348 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2351 | static char **expand_variables(char **argv, int or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2352 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2353 | int n; |
| 2354 | char **list; |
| 2355 | char **v; |
| 2356 | o_string output = NULL_O_STRING; |
| 2357 | |
| 2358 | if (or_mask & 0x100) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2359 | output.o_escape = 1; /* protect against globbing for "$var" */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2360 | /* (unquoted $var will temporarily switch it off) */ |
| 2361 | output.o_glob = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2362 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2363 | |
| 2364 | n = 0; |
| 2365 | v = argv; |
| 2366 | while (*v) { |
| 2367 | n = expand_vars_to_list(&output, n, *v, (char)or_mask); |
| 2368 | v++; |
| 2369 | } |
| 2370 | debug_print_list("expand_variables", &output, n); |
| 2371 | |
| 2372 | /* output.data (malloced in one block) gets returned in "list" */ |
| 2373 | list = o_finalize_list(&output, n); |
| 2374 | debug_print_strings("expand_variables[1]", list); |
| 2375 | return list; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2376 | } |
| 2377 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2378 | static char **expand_strvec_to_strvec(char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2379 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2380 | return expand_variables(argv, 0x100); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2383 | /* Used for expansion of right hand of assignments */ |
| 2384 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 2385 | * "v=/bin/c*" */ |
| 2386 | static char *expand_string_to_string(const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2387 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2388 | char *argv[2], **list; |
| 2389 | |
| 2390 | argv[0] = (char*)str; |
| 2391 | argv[1] = NULL; |
| 2392 | list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */ |
| 2393 | if (HUSH_DEBUG) |
| 2394 | if (!list[0] || list[1]) |
| 2395 | bb_error_msg_and_die("BUG in varexp2"); |
| 2396 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 2397 | overlapping_strcpy((char*)list, list[0]); |
| 2398 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 2399 | return (char*)list; |
| 2400 | } |
| 2401 | |
| 2402 | /* Used for "eval" builtin */ |
| 2403 | static char* expand_strvec_to_string(char **argv) |
| 2404 | { |
| 2405 | char **list; |
| 2406 | |
| 2407 | list = expand_variables(argv, 0x80); |
| 2408 | /* Convert all NULs to spaces */ |
| 2409 | if (list[0]) { |
| 2410 | int n = 1; |
| 2411 | while (list[n]) { |
| 2412 | if (HUSH_DEBUG) |
| 2413 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 2414 | bb_error_msg_and_die("BUG in varexp3"); |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 2415 | /* bash uses ' ' regardless of $IFS contents */ |
| 2416 | list[n][-1] = ' '; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2417 | n++; |
| 2418 | } |
| 2419 | } |
| 2420 | overlapping_strcpy((char*)list, list[0]); |
| 2421 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 2422 | return (char*)list; |
| 2423 | } |
| 2424 | |
| 2425 | static char **expand_assignments(char **argv, int count) |
| 2426 | { |
| 2427 | int i; |
| 2428 | char **p = NULL; |
| 2429 | /* Expand assignments into one string each */ |
| 2430 | for (i = 0; i < count; i++) { |
| 2431 | p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
| 2432 | } |
| 2433 | return p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2436 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2437 | #if BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2438 | /* never called */ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2439 | 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] | 2440 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2441 | static void reset_traps_to_defaults(void) |
| 2442 | { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2443 | /* This function is always called in a child shell |
| 2444 | * after fork (not vfork, NOMMU doesn't use this function). |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2445 | * Child shells are not interactive. |
| 2446 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 2447 | * Testcase: (while :; do :; done) + ^Z should background. |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2448 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2449 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2450 | unsigned sig; |
| 2451 | unsigned mask; |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2452 | |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2453 | if (!G.traps && !(G.non_DFL_mask & SPECIAL_INTERACTIVE_SIGS)) |
| 2454 | return; |
| 2455 | |
| 2456 | /* Stupid. It can be done with *single* &= op, but we can't use |
| 2457 | * the fact that G.blocked_set is implemented as a bitmask... */ |
| 2458 | mask = (SPECIAL_INTERACTIVE_SIGS >> 1); |
| 2459 | sig = 1; |
| 2460 | while (1) { |
| 2461 | if (mask & 1) |
| 2462 | sigdelset(&G.blocked_set, sig); |
| 2463 | mask >>= 1; |
| 2464 | if (!mask) |
| 2465 | break; |
| 2466 | sig++; |
| 2467 | } |
| 2468 | |
| 2469 | G.non_DFL_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
| 2470 | mask = G.non_DFL_mask; |
| 2471 | if (G.traps) for (sig = 0; sig < NSIG; sig++, mask >>= 1) { |
| 2472 | if (!G.traps[sig]) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2473 | continue; |
| 2474 | free(G.traps[sig]); |
| 2475 | G.traps[sig] = NULL; |
| 2476 | /* There is no signal for 0 (EXIT) */ |
| 2477 | if (sig == 0) |
| 2478 | continue; |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2479 | /* There was a trap handler, we are removing it. |
| 2480 | * But if sig still has non-DFL handling, |
| 2481 | * we should not unblock it. */ |
| 2482 | if (mask & 1) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2483 | continue; |
| 2484 | sigdelset(&G.blocked_set, sig); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2485 | } |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2486 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | #else /* !BB_MMU */ |
| 2490 | |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2491 | static void re_execute_shell(char ***to_free, const char *s, char *g_argv0, char **g_argv) NORETURN; |
| 2492 | 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] | 2493 | { |
| 2494 | char param_buf[sizeof("-$%x:%x:%x:%x") + sizeof(unsigned) * 4]; |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2495 | char *heredoc_argv[4]; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2496 | struct variable *cur; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2497 | #if ENABLE_HUSH_FUNCTIONS |
| 2498 | struct function *funcp; |
| 2499 | #endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2500 | char **argv, **pp; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2501 | unsigned cnt; |
| 2502 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2503 | if (!g_argv0) { /* heredoc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2504 | argv = heredoc_argv; |
| 2505 | argv[0] = (char *) G.argv0_for_re_execing; |
| 2506 | argv[1] = (char *) "-<"; |
| 2507 | argv[2] = (char *) s; |
| 2508 | argv[3] = NULL; |
Denis Vlasenko | f50caac | 2009-04-09 01:40:15 +0000 | [diff] [blame] | 2509 | pp = &argv[3]; /* used as pointer to empty environment */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2510 | goto do_exec; |
| 2511 | } |
| 2512 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 2513 | sprintf(param_buf, "-$%x:%x:%x" IF_HUSH_LOOPS(":%x") |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2514 | , (unsigned) G.root_pid |
| 2515 | , (unsigned) G.last_bg_pid |
| 2516 | , (unsigned) G.last_exitcode |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 2517 | IF_HUSH_LOOPS(, G.depth_of_loop) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2518 | ); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2519 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<depth> <vars...> <funcs...> |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2520 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2521 | */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2522 | cnt = 6; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2523 | for (cur = G.top_var; cur; cur = cur->next) { |
| 2524 | if (!cur->flg_export || cur->flg_read_only) |
| 2525 | cnt += 2; |
| 2526 | } |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2527 | #if ENABLE_HUSH_FUNCTIONS |
| 2528 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 2529 | cnt += 3; |
| 2530 | #endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2531 | pp = g_argv; |
| 2532 | while (*pp++) |
| 2533 | cnt++; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2534 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2535 | *pp++ = (char *) G.argv0_for_re_execing; |
| 2536 | *pp++ = param_buf; |
| 2537 | for (cur = G.top_var; cur; cur = cur->next) { |
| 2538 | if (cur->varstr == hush_version_str) |
| 2539 | continue; |
| 2540 | if (cur->flg_read_only) { |
| 2541 | *pp++ = (char *) "-R"; |
| 2542 | *pp++ = cur->varstr; |
| 2543 | } else if (!cur->flg_export) { |
| 2544 | *pp++ = (char *) "-V"; |
| 2545 | *pp++ = cur->varstr; |
| 2546 | } |
| 2547 | } |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2548 | #if ENABLE_HUSH_FUNCTIONS |
| 2549 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 2550 | *pp++ = (char *) "-F"; |
| 2551 | *pp++ = funcp->name; |
| 2552 | *pp++ = funcp->body_as_string; |
| 2553 | } |
| 2554 | #endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2555 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 2556 | * |
| 2557 | * However, POSIX says that subshells reset signals with traps |
| 2558 | * to SIG_DFL. |
| 2559 | * I tested bash-3.2 and it not only does that with true subshells |
| 2560 | * of the form ( list ), but with any forked children shells. |
| 2561 | * I set trap "echo W" WINCH; and then tried: |
| 2562 | * |
| 2563 | * { echo 1; sleep 20; echo 2; } & |
| 2564 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 2565 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 2566 | * |
| 2567 | * In all these cases sending SIGWINCH to the child shell |
| 2568 | * did not run the trap. If I add trap "echo V" WINCH; |
| 2569 | * _inside_ group (just before echo 1), it works. |
| 2570 | * |
| 2571 | * I conclude it means we don't need to pass active traps here. |
| 2572 | * exec syscall below resets them to SIG_DFL for us. |
| 2573 | */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2574 | *pp++ = (char *) "-c"; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2575 | *pp++ = (char *) s; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2576 | *pp++ = g_argv0; |
| 2577 | while (*g_argv) |
| 2578 | *pp++ = *g_argv++; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2579 | /* *pp = NULL; - is already there */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2580 | pp = environ; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2581 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2582 | do_exec: |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2583 | debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s); |
| 2584 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2585 | execve(bb_busybox_exec_path, argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2586 | /* Fallback. Useful for init=/bin/hush usage etc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2587 | if (argv[0][0] == '/') |
| 2588 | execve(argv[0], argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2589 | xfunc_error_retval = 127; |
| 2590 | bb_error_msg_and_die("can't re-execute the shell"); |
| 2591 | } |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2592 | #endif /* !BB_MMU */ |
| 2593 | |
| 2594 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2595 | static void setup_heredoc(struct redir_struct *redir) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2596 | { |
| 2597 | struct fd_pair pair; |
| 2598 | pid_t pid; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2599 | int len, written; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2600 | /* the _body_ of heredoc (misleading field name) */ |
| 2601 | const char *heredoc = redir->rd_filename; |
| 2602 | char *expanded; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2603 | #if !BB_MMU |
| 2604 | char **to_free; |
| 2605 | #endif |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2606 | |
| 2607 | expanded = NULL; |
| 2608 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
| 2609 | expanded = expand_pseudo_dquoted(heredoc); |
| 2610 | if (expanded) |
| 2611 | heredoc = expanded; |
| 2612 | } |
| 2613 | len = strlen(heredoc); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2614 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2615 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2616 | xpiped_pair(pair); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2617 | xmove_fd(pair.rd, redir->rd_fd); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2618 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2619 | /* Try writing without forking. Newer kernels have |
| 2620 | * dynamically growing pipes. Must use non-blocking write! */ |
| 2621 | ndelay_on(pair.wr); |
| 2622 | while (1) { |
| 2623 | written = write(pair.wr, heredoc, len); |
| 2624 | if (written <= 0) |
| 2625 | break; |
| 2626 | len -= written; |
| 2627 | if (len == 0) { |
| 2628 | close(pair.wr); |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 2629 | free(expanded); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2630 | return; |
| 2631 | } |
| 2632 | heredoc += written; |
| 2633 | } |
| 2634 | ndelay_off(pair.wr); |
| 2635 | |
| 2636 | /* Okay, pipe buffer was not big enough */ |
| 2637 | /* Note: we must not create a stray child (bastard? :) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2638 | * for the unsuspecting parent process. Child creates a grandchild |
| 2639 | * and exits before parent execs the process which consumes heredoc |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2640 | * (that exec happens after we return from this function) */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 2641 | #if !BB_MMU |
| 2642 | to_free = NULL; |
| 2643 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2644 | pid = vfork(); |
| 2645 | if (pid < 0) |
| 2646 | bb_perror_msg_and_die("vfork"); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2647 | if (pid == 0) { |
| 2648 | /* child */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 2649 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2650 | pid = BB_MMU ? fork() : vfork(); |
| 2651 | if (pid < 0) |
| 2652 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
| 2653 | if (pid != 0) |
| 2654 | _exit(0); |
| 2655 | /* grandchild */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2656 | close(redir->rd_fd); /* read side of the pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2657 | #if BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2658 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2659 | _exit(0); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2660 | #else |
| 2661 | /* Delegate blocking writes to another process */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2662 | xmove_fd(pair.wr, STDOUT_FILENO); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2663 | re_execute_shell(&to_free, heredoc, NULL, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2664 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2665 | } |
| 2666 | /* parent */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2667 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2668 | #if !BB_MMU |
| 2669 | free(to_free); |
| 2670 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2671 | close(pair.wr); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2672 | free(expanded); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2673 | wait(NULL); /* wait till child has died */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2676 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 2677 | * and stderr if they are redirected. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2678 | static int setup_redirects(struct command *prog, int squirrel[]) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2679 | { |
| 2680 | int openfd, mode; |
| 2681 | struct redir_struct *redir; |
| 2682 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2683 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2684 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2685 | /* rd_fd<<HERE case */ |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 2686 | if (squirrel && redir->rd_fd < 3 |
| 2687 | && squirrel[redir->rd_fd] < 0 |
| 2688 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2689 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2690 | } |
| 2691 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 2692 | * of the heredoc */ |
| 2693 | debug_printf_parse("set heredoc '%s'\n", |
| 2694 | redir->rd_filename); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2695 | setup_heredoc(redir); |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2696 | continue; |
| 2697 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2698 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2699 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
| 2700 | /* rd_fd<*>file case (<*> is <,>,>>,<>) */ |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2701 | char *p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2702 | if (redir->rd_filename == NULL) { |
| 2703 | /* Something went wrong in the parse. |
| 2704 | * Pretend it didn't happen */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2705 | bb_error_msg("bug in redirect parse"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2706 | continue; |
| 2707 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2708 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2709 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2710 | openfd = open_or_warn(p, mode); |
| 2711 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2712 | if (openfd < 0) { |
| 2713 | /* this could get lost if stderr has been redirected, but |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2714 | * bash and ash both lose it as well (though zsh doesn't!) */ |
| 2715 | //what the above comment tries to say? |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2716 | return 1; |
| 2717 | } |
| 2718 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2719 | /* rd_fd<*>rd_dup or rd_fd<*>- cases */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2720 | openfd = redir->rd_dup; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2723 | if (openfd != redir->rd_fd) { |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 2724 | if (squirrel && redir->rd_fd < 3 |
| 2725 | && squirrel[redir->rd_fd] < 0 |
| 2726 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2727 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2728 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2729 | if (openfd == REDIRFD_CLOSE) { |
| 2730 | /* "n>-" means "close me" */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2731 | close(redir->rd_fd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2732 | } else { |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2733 | xdup2(openfd, redir->rd_fd); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2734 | if (redir->rd_dup == REDIRFD_TO_FILE) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2735 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2736 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2737 | } |
| 2738 | } |
| 2739 | return 0; |
| 2740 | } |
| 2741 | |
| 2742 | static void restore_redirects(int squirrel[]) |
| 2743 | { |
| 2744 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2745 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2746 | fd = squirrel[i]; |
| 2747 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2748 | /* We simply die on error */ |
| 2749 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2750 | } |
| 2751 | } |
| 2752 | } |
| 2753 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2754 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2755 | static void free_pipe_list(struct pipe *head); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2756 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2757 | /* Return code is the exit status of the pipe */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2758 | static void free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2759 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2760 | char **p; |
| 2761 | struct command *command; |
| 2762 | struct redir_struct *r, *rnext; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2763 | int a, i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2764 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2765 | if (pi->stopped_cmds > 0) /* why? */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2766 | return; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2767 | debug_printf_clean("run pipe: (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2768 | for (i = 0; i < pi->num_cmds; i++) { |
| 2769 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2770 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2771 | if (command->argv) { |
| 2772 | for (a = 0, p = command->argv; *p; a++, p++) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2773 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2774 | } |
| 2775 | free_strings(command->argv); |
| 2776 | command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2777 | } |
| 2778 | /* not "else if": on syntax error, we may have both! */ |
| 2779 | if (command->group) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2780 | debug_printf_clean(" begin group (grp_type:%d)\n", |
| 2781 | command->grp_type); |
| 2782 | free_pipe_list(command->group); |
| 2783 | debug_printf_clean(" end group\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2784 | command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2785 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 2786 | /* else is crucial here. |
| 2787 | * If group != NULL, child_func is meaningless */ |
| 2788 | #if ENABLE_HUSH_FUNCTIONS |
| 2789 | else if (command->child_func) { |
| 2790 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 2791 | command->child_func->parent_cmd = NULL; |
| 2792 | } |
| 2793 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2794 | #if !BB_MMU |
| 2795 | free(command->group_as_string); |
| 2796 | command->group_as_string = NULL; |
| 2797 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2798 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2799 | debug_printf_clean(" redirect %d%s", |
| 2800 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2801 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 2802 | if (r->rd_filename) { |
| 2803 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 2804 | free(r->rd_filename); |
| 2805 | r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2806 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2807 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2808 | rnext = r->next; |
| 2809 | free(r); |
| 2810 | } |
| 2811 | command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2812 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2813 | free(pi->cmds); /* children are an array, they get freed all at once */ |
| 2814 | pi->cmds = NULL; |
| 2815 | #if ENABLE_HUSH_JOB |
| 2816 | free(pi->cmdtext); |
| 2817 | pi->cmdtext = NULL; |
| 2818 | #endif |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2819 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2820 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2821 | static void free_pipe_list(struct pipe *head) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2822 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2823 | struct pipe *pi, *next; |
| 2824 | |
| 2825 | for (pi = head; pi; pi = next) { |
| 2826 | #if HAS_KEYWORDS |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2827 | debug_printf_clean(" pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2828 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2829 | free_pipe(pi); |
| 2830 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2831 | next = pi->next; |
| 2832 | /*pi->next = NULL;*/ |
| 2833 | free(pi); |
| 2834 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2838 | static int run_list(struct pipe *pi); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2839 | #if BB_MMU |
| 2840 | #define parse_stream(pstring, input, end_trigger) \ |
| 2841 | parse_stream(input, end_trigger) |
| 2842 | #endif |
| 2843 | static struct pipe *parse_stream(char **pstring, |
| 2844 | struct in_str *input, |
| 2845 | int end_trigger); |
| 2846 | static void parse_and_run_string(const char *s); |
| 2847 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2848 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2849 | static const struct built_in_command* find_builtin(const char *name) |
| 2850 | { |
| 2851 | const struct built_in_command *x; |
| 2852 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
| 2853 | if (strcmp(name, x->cmd) != 0) |
| 2854 | continue; |
| 2855 | debug_printf_exec("found builtin '%s'\n", name); |
| 2856 | return x; |
| 2857 | } |
| 2858 | return NULL; |
| 2859 | } |
| 2860 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2861 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2862 | static const struct function *find_function(const char *name) |
| 2863 | { |
| 2864 | const struct function *funcp = G.top_func; |
| 2865 | while (funcp) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2866 | if (strcmp(name, funcp->name) == 0) { |
| 2867 | break; |
| 2868 | } |
| 2869 | funcp = funcp->next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2870 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2871 | if (funcp) |
| 2872 | debug_printf_exec("found function '%s'\n", name); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2873 | return funcp; |
| 2874 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 2875 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2876 | /* Note: takes ownership on name ptr */ |
| 2877 | static struct function *new_function(char *name) |
| 2878 | { |
| 2879 | struct function *funcp; |
| 2880 | struct function **funcpp = &G.top_func; |
| 2881 | |
| 2882 | while ((funcp = *funcpp) != NULL) { |
| 2883 | struct command *cmd; |
| 2884 | |
| 2885 | if (strcmp(funcp->name, name) != 0) { |
| 2886 | funcpp = &funcp->next; |
| 2887 | continue; |
| 2888 | } |
| 2889 | |
| 2890 | cmd = funcp->parent_cmd; |
| 2891 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 2892 | if (!cmd) { |
| 2893 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 2894 | free(funcp->name); |
| 2895 | /* Note: if !funcp->body, do not free body_as_string! |
| 2896 | * This is a special case of "-F name body" function: |
| 2897 | * body_as_string was not malloced! */ |
| 2898 | if (funcp->body) { |
| 2899 | free_pipe_list(funcp->body); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2900 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2901 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2902 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2903 | } |
| 2904 | } else { |
| 2905 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 2906 | cmd->argv[0] = funcp->name; |
| 2907 | cmd->group = funcp->body; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2908 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2909 | cmd->group_as_string = funcp->body_as_string; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2910 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2911 | } |
| 2912 | goto skip; |
| 2913 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2914 | debug_printf_exec("remembering new function '%s'\n", name); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2915 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 2916 | /*funcp->next = NULL;*/ |
| 2917 | skip: |
| 2918 | funcp->name = name; |
| 2919 | return funcp; |
| 2920 | } |
| 2921 | |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 2922 | static void unset_func(const char *name) |
| 2923 | { |
| 2924 | struct function *funcp; |
| 2925 | struct function **funcpp = &G.top_func; |
| 2926 | |
| 2927 | while ((funcp = *funcpp) != NULL) { |
| 2928 | if (strcmp(funcp->name, name) == 0) { |
| 2929 | *funcpp = funcp->next; |
root | 6245202 | 2009-05-04 12:00:19 +0200 | [diff] [blame] | 2930 | /* funcp is unlinked now, deleting it. |
| 2931 | * Note: if !funcp->body, the function was created by |
| 2932 | * "-F name body", do not free ->body_as_string |
| 2933 | * and ->name as they were not malloced. */ |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 2934 | if (funcp->body) { |
| 2935 | free_pipe_list(funcp->body); |
root | 6245202 | 2009-05-04 12:00:19 +0200 | [diff] [blame] | 2936 | free(funcp->name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2937 | # if !BB_MMU |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 2938 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2939 | # endif |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 2940 | } |
| 2941 | free(funcp); |
| 2942 | break; |
| 2943 | } |
Denis Vlasenko | 7301067 | 2009-04-18 11:25:18 +0000 | [diff] [blame] | 2944 | funcpp = &funcp->next; |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 2945 | } |
| 2946 | } |
| 2947 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2948 | # if BB_MMU |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2949 | #define exec_function(nommu_save, funcp, argv) \ |
| 2950 | exec_function(funcp, argv) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2951 | # endif |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2952 | static void exec_function(nommu_save_t *nommu_save, |
| 2953 | const struct function *funcp, |
| 2954 | char **argv) NORETURN; |
| 2955 | static void exec_function(nommu_save_t *nommu_save, |
| 2956 | const struct function *funcp, |
| 2957 | char **argv) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2958 | { |
| 2959 | # if BB_MMU |
| 2960 | int n = 1; |
| 2961 | |
| 2962 | argv[0] = G.global_argv[0]; |
| 2963 | G.global_argv = argv; |
| 2964 | while (*++argv) |
| 2965 | n++; |
| 2966 | G.global_argc = n; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2967 | /* On MMU, funcp->body is always non-NULL */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2968 | n = run_list(funcp->body); |
| 2969 | fflush(NULL); |
| 2970 | _exit(n); |
| 2971 | # else |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2972 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 2973 | funcp->body_as_string, |
| 2974 | G.global_argv[0], |
| 2975 | argv + 1); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 2976 | # endif |
| 2977 | } |
| 2978 | |
| 2979 | static int run_function(const struct function *funcp, char **argv) |
| 2980 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 2981 | int rc; |
| 2982 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 2983 | smallint sv_flg; |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 2984 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 2985 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 2986 | /* "we are in function, ok to use return" */ |
| 2987 | sv_flg = G.flag_return_in_progress; |
| 2988 | G.flag_return_in_progress = -1; |
| 2989 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2990 | /* On MMU, funcp->body is always non-NULL */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2991 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2992 | if (!funcp->body) { |
| 2993 | /* Function defined by -F */ |
| 2994 | parse_and_run_string(funcp->body_as_string); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 2995 | rc = G.last_exitcode; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2996 | } else |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2997 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2998 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 2999 | rc = run_list(funcp->body); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3000 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3001 | |
| 3002 | G.flag_return_in_progress = sv_flg; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3003 | restore_G_args(&sv, argv); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3004 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3005 | return rc; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3006 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3007 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3008 | |
| 3009 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 3010 | #if BB_MMU |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3011 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 3012 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 3013 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 3014 | pseudo_exec(command, argv_expanded) |
| 3015 | #endif |
| 3016 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3017 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3018 | * Never returns. |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3019 | * Don't exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3020 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3021 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3022 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 3023 | char **argv, int assignment_cnt, |
| 3024 | char **argv_expanded) NORETURN; |
| 3025 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 3026 | char **argv, int assignment_cnt, |
| 3027 | char **argv_expanded) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3028 | { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3029 | char **new_env; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3030 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3031 | /* Case when we are here: ... | var=val | ... */ |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3032 | if (!argv[assignment_cnt]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3033 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3034 | |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 3035 | new_env = expand_assignments(argv, assignment_cnt); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3036 | #if BB_MMU |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3037 | set_vars_and_save_old(new_env); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3038 | free(new_env); /* optional */ |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3039 | /* we can also destroy set_vars_and_save_old's return value, |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3040 | * to save memory */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3041 | #else |
| 3042 | nommu_save->new_env = new_env; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3043 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3044 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3045 | if (argv_expanded) { |
| 3046 | argv = argv_expanded; |
| 3047 | } else { |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 3048 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3049 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3050 | nommu_save->argv = argv; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3051 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3052 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 3053 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3054 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 3055 | if (strchr(argv[0], '/') != NULL) |
| 3056 | goto skip; |
| 3057 | #endif |
| 3058 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3059 | /* On NOMMU, we must never block! |
| 3060 | * Example: { sleep 99999 | read line } & echo Ok |
| 3061 | * read builtin will block on read syscall, leaving parent blocked |
| 3062 | * in vfork. Therefore we can't do this: |
| 3063 | */ |
| 3064 | #if BB_MMU |
| 3065 | /* Check if the command matches any of the builtins. |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3066 | * Depending on context, this might be redundant. But it's |
| 3067 | * easier to waste a few CPU cycles than it is to figure out |
| 3068 | * if this is one of those cases. |
| 3069 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3070 | { |
| 3071 | int rcode; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3072 | const struct built_in_command *x = find_builtin(argv[0]); |
| 3073 | if (x) { |
| 3074 | rcode = x->function(argv); |
| 3075 | fflush(NULL); |
| 3076 | _exit(rcode); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3077 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3078 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3079 | #endif |
| 3080 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3081 | /* Check if the command matches any functions */ |
| 3082 | { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3083 | const struct function *funcp = find_function(argv[0]); |
| 3084 | if (funcp) { |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3085 | exec_function(nommu_save, funcp, argv); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3086 | } |
| 3087 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3088 | #endif |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3089 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 3090 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3091 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3092 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3093 | int a = find_applet_by_name(argv[0]); |
| 3094 | if (a >= 0) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3095 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3096 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3097 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3098 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3099 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3100 | # endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3101 | /* Re-exec ourselves */ |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3102 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3103 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 3104 | execv(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3105 | /* If they called chroot or otherwise made the binary no longer |
| 3106 | * executable, fall through */ |
| 3107 | } |
| 3108 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 3109 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3110 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3111 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3112 | skip: |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3113 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3114 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3115 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3116 | execvp(argv[0], argv); |
Denis Vlasenko | f9d4fc3 | 2009-04-21 20:40:51 +0000 | [diff] [blame] | 3117 | bb_perror_msg("can't execute '%s'", argv[0]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 3118 | _exit(EXIT_FAILURE); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3119 | } |
| 3120 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3121 | /* Called after [v]fork() in run_pipe |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3122 | */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3123 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3124 | struct command *command, |
| 3125 | char **argv_expanded) NORETURN; |
| 3126 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3127 | struct command *command, |
| 3128 | char **argv_expanded) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3129 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3130 | if (command->argv) { |
| 3131 | pseudo_exec_argv(nommu_save, command->argv, |
| 3132 | command->assignment_cnt, argv_expanded); |
| 3133 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3134 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3135 | if (command->group) { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3136 | /* Cases when we are here: |
| 3137 | * ( list ) |
| 3138 | * { list } & |
| 3139 | * ... | ( list ) | ... |
| 3140 | * ... | { list } | ... |
| 3141 | */ |
| 3142 | #if BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 3143 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3144 | debug_printf_exec("pseudo_exec: run_list\n"); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3145 | reset_traps_to_defaults(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3146 | rcode = run_list(command->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 3147 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3148 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3149 | _exit(rcode); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3150 | #else |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3151 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 3152 | command->group_as_string, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3153 | G.global_argv[0], |
| 3154 | G.global_argv + 1); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3155 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3156 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3157 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3158 | /* Case when we are here: ... | >file */ |
| 3159 | debug_printf_exec("pseudo_exec'ed null command\n"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3160 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3163 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3164 | static const char *get_cmdtext(struct pipe *pi) |
| 3165 | { |
| 3166 | char **argv; |
| 3167 | char *p; |
| 3168 | int len; |
| 3169 | |
| 3170 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 3171 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3172 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3173 | if (pi->cmdtext) |
| 3174 | return pi->cmdtext; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3175 | argv = pi->cmds[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3176 | if (!argv || !argv[0]) { |
| 3177 | pi->cmdtext = xzalloc(1); |
| 3178 | return pi->cmdtext; |
| 3179 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3180 | |
| 3181 | len = 0; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3182 | do { |
| 3183 | len += strlen(*argv) + 1; |
| 3184 | } while (*++argv); |
| 3185 | p = xmalloc(len); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3186 | pi->cmdtext = p; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3187 | argv = pi->cmds[0].argv; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3188 | do { |
| 3189 | len = strlen(*argv); |
| 3190 | memcpy(p, *argv, len); |
| 3191 | p += len; |
| 3192 | *p++ = ' '; |
| 3193 | } while (*++argv); |
| 3194 | p[-1] = '\0'; |
| 3195 | return pi->cmdtext; |
| 3196 | } |
| 3197 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3198 | static void insert_bg_job(struct pipe *pi) |
| 3199 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3200 | struct pipe *job, **jobp; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3201 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3202 | |
| 3203 | /* Linear search for the ID of the job to use */ |
| 3204 | pi->jobid = 1; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3205 | for (job = G.job_list; job; job = job->next) |
| 3206 | if (job->jobid >= pi->jobid) |
| 3207 | pi->jobid = job->jobid + 1; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3208 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3209 | /* Add job to the list of running jobs */ |
| 3210 | jobp = &G.job_list; |
| 3211 | while ((job = *jobp) != NULL) |
| 3212 | jobp = &job->next; |
| 3213 | job = *jobp = xmalloc(sizeof(*job)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3214 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3215 | *job = *pi; /* physical copy */ |
| 3216 | job->next = NULL; |
| 3217 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 3218 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3219 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3220 | job->cmds[i].pid = pi->cmds[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3221 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3222 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3223 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3224 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3225 | if (G_interactive_fd) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3226 | printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext); |
| 3227 | /* Last command's pid goes to $! */ |
| 3228 | G.last_bg_pid = job->cmds[job->num_cmds - 1].pid; |
| 3229 | G.last_jobid = job->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3232 | static void remove_bg_job(struct pipe *pi) |
| 3233 | { |
| 3234 | struct pipe *prev_pipe; |
| 3235 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3236 | if (pi == G.job_list) { |
| 3237 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3238 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3239 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3240 | while (prev_pipe->next != pi) |
| 3241 | prev_pipe = prev_pipe->next; |
| 3242 | prev_pipe->next = pi->next; |
| 3243 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3244 | if (G.job_list) |
| 3245 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3246 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3247 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3248 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3249 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3250 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3251 | static void delete_finished_bg_job(struct pipe *pi) |
| 3252 | { |
| 3253 | remove_bg_job(pi); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3254 | pi->stopped_cmds = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3255 | free_pipe(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3256 | free(pi); |
| 3257 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3258 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3259 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3260 | /* Check to see if any processes have exited -- if they |
| 3261 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3262 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3263 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3264 | int attributes; |
| 3265 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3266 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3267 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3268 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3269 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3270 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3271 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3272 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 3273 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3274 | errno = 0; |
| 3275 | // if (G.handled_SIGCHLD == G.count_SIGCHLD) |
| 3276 | // /* avoid doing syscall, nothing there anyway */ |
| 3277 | // return rcode; |
| 3278 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3279 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3280 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3281 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3282 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3283 | /* Do we do this right? |
| 3284 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3285 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3286 | * [3]+ Stopped sleep 20 | false |
| 3287 | * bash-3.00# echo $? |
| 3288 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3289 | * [hush 1.14.0: yes we do it right] |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3290 | */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3291 | wait_more: |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3292 | while (1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3293 | int i; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3294 | int dead; |
| 3295 | |
| 3296 | // i = G.count_SIGCHLD; |
| 3297 | childpid = waitpid(-1, &status, attributes); |
| 3298 | if (childpid <= 0) { |
| 3299 | if (childpid && errno != ECHILD) |
| 3300 | bb_perror_msg("waitpid"); |
| 3301 | // else /* Until next SIGCHLD, waitpid's are useless */ |
| 3302 | // G.handled_SIGCHLD = i; |
| 3303 | break; |
| 3304 | } |
| 3305 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 3306 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3307 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3308 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3309 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3310 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 3311 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3312 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3313 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 3314 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3315 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3316 | childpid, WEXITSTATUS(status)); |
| 3317 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3318 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3319 | if (fg_pipe) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3320 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 3321 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 3322 | if (fg_pipe->cmds[i].pid != childpid) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3323 | continue; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3324 | if (dead) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3325 | fg_pipe->cmds[i].pid = 0; |
| 3326 | fg_pipe->alive_cmds--; |
| 3327 | if (i == fg_pipe->num_cmds - 1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3328 | /* last process gives overall exitstatus */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3329 | /* Note: is WIFSIGNALED, WEXITSTATUS = sig + 128 */ |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3330 | rcode = WEXITSTATUS(status); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3331 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3332 | /* bash prints killing signal's name for *last* |
| 3333 | * process in pipe (prints just newline for SIGINT). |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3334 | * Mimic this. Example: "sleep 5" + ^\ |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3335 | */ |
| 3336 | if (WIFSIGNALED(status)) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3337 | int sig = WTERMSIG(status); |
| 3338 | printf("%s\n", sig == SIGINT ? "" : get_signame(sig)); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3339 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3340 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3341 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3342 | fg_pipe->cmds[i].is_stopped = 1; |
| 3343 | fg_pipe->stopped_cmds++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3344 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3345 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 3346 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 3347 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3348 | /* All processes in fg pipe have exited or stopped */ |
| 3349 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 3350 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 3351 | * and "killall -STOP cat" */ |
| 3352 | if (G_interactive_fd) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3353 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3354 | if (fg_pipe->alive_cmds) |
| 3355 | insert_bg_job(fg_pipe); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3356 | #endif |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3357 | return rcode; |
| 3358 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3359 | } |
| 3360 | /* There are still running processes in the fg pipe */ |
| 3361 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3362 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3363 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3364 | } |
| 3365 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3366 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3367 | /* We asked to wait for bg or orphaned children */ |
| 3368 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3369 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3370 | for (i = 0; i < pi->num_cmds; i++) { |
| 3371 | if (pi->cmds[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3372 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3373 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3374 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3375 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 3376 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3377 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 3378 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3379 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3380 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3381 | /* child exited */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3382 | pi->cmds[i].pid = 0; |
| 3383 | pi->alive_cmds--; |
| 3384 | if (!pi->alive_cmds) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3385 | if (G_interactive_fd) |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 3386 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 3387 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3388 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3389 | } |
| 3390 | } else { |
| 3391 | /* child stopped */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3392 | pi->cmds[i].is_stopped = 1; |
| 3393 | pi->stopped_cmds++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3394 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3395 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3396 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3397 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3398 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3401 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3402 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 3403 | { |
| 3404 | pid_t p; |
| 3405 | int rcode = checkjobs(fg_pipe); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 3406 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3407 | /* Job finished, move the shell to the foreground */ |
| 3408 | p = getpgrp(); /* our process group id */ |
| 3409 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 3410 | tcsetpgrp(G_interactive_fd, p); |
| 3411 | } |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3412 | return rcode; |
| 3413 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3414 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3415 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3416 | /* Start all the jobs, but don't wait for anything to finish. |
| 3417 | * See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3418 | * |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3419 | * 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] | 3420 | * to finish to determine the exit status of the pipe. If the pipe |
| 3421 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3422 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3423 | * return value. |
| 3424 | * |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3425 | * Returns -1 only if started some children. IOW: we have to |
| 3426 | * mask out retvals of builtins etc with 0xff! |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3427 | * |
| 3428 | * The only case when we do not need to [v]fork is when the pipe |
| 3429 | * is single, non-backgrounded, non-subshell command. Examples: |
| 3430 | * cmd ; ... { list } ; ... |
| 3431 | * cmd && ... { list } && ... |
| 3432 | * cmd || ... { list } || ... |
| 3433 | * If it is, then we can run cmd as a builtin, NOFORK [do we do this?], |
| 3434 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3435 | * 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] | 3436 | * |
| 3437 | * Cases when we must fork: |
| 3438 | * non-single: cmd | cmd |
| 3439 | * backgrounded: cmd & { list } & |
| 3440 | * subshell: ( list ) [&] |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3441 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3442 | static int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3443 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3444 | static const char *const null_ptr = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3445 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3446 | int nextin; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3447 | struct command *command; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3448 | char **argv_expanded; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3449 | char **argv; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3450 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3451 | /* it is not always needed, but we aim to smaller code */ |
| 3452 | int squirrel[] = { -1, -1, -1 }; |
| 3453 | int rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3454 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3455 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3456 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 3457 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 3458 | IF_HUSH_JOB(pi->pgrp = -1;) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3459 | pi->stopped_cmds = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3460 | command = &(pi->cmds[0]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3461 | argv_expanded = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3462 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3463 | if (pi->num_cmds != 1 |
| 3464 | || pi->followup == PIPE_BG |
| 3465 | || command->grp_type == GRP_SUBSHELL |
| 3466 | ) { |
| 3467 | goto must_fork; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3468 | } |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3469 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3470 | pi->alive_cmds = 1; |
| 3471 | |
| 3472 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 3473 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 3474 | |
| 3475 | if (command->group) { |
| 3476 | #if ENABLE_HUSH_FUNCTIONS |
| 3477 | if (command->grp_type == GRP_FUNCTION) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3478 | /* "executing" func () { list } */ |
| 3479 | struct function *funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3480 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3481 | funcp = new_function(command->argv[0]); |
| 3482 | /* funcp->name is already set to argv[0] */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3483 | funcp->body = command->group; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3484 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3485 | funcp->body_as_string = command->group_as_string; |
| 3486 | command->group_as_string = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3487 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3488 | command->group = NULL; |
| 3489 | command->argv[0] = NULL; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3490 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 3491 | funcp->parent_cmd = command; |
| 3492 | command->child_func = funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3493 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3494 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 3495 | debug_leave(); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3496 | return EXIT_SUCCESS; |
| 3497 | } |
| 3498 | #endif |
| 3499 | /* { list } */ |
| 3500 | debug_printf("non-subshell group\n"); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3501 | rcode = 1; /* exitcode if redir failed */ |
| 3502 | if (setup_redirects(command, squirrel) == 0) { |
| 3503 | debug_printf_exec(": run_list\n"); |
| 3504 | rcode = run_list(command->group) & 0xff; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3505 | } |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 3506 | restore_redirects(squirrel); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3507 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3508 | debug_leave(); |
| 3509 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3510 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3511 | } |
| 3512 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3513 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 3514 | { |
| 3515 | const struct built_in_command *x; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3516 | #if ENABLE_HUSH_FUNCTIONS |
| 3517 | const struct function *funcp; |
| 3518 | #else |
| 3519 | enum { funcp = 0 }; |
| 3520 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3521 | char **new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3522 | struct variable *old_vars = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3523 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3524 | if (argv[command->assignment_cnt] == NULL) { |
| 3525 | /* Assignments, but no command */ |
| 3526 | /* Ensure redirects take effect. Try "a=t >file" */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3527 | rcode = setup_redirects(command, squirrel); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3528 | restore_redirects(squirrel); |
| 3529 | /* Set shell variables */ |
| 3530 | while (*argv) { |
| 3531 | p = expand_string_to_string(*argv); |
| 3532 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 3533 | *argv, p); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 3534 | set_local_var(p, 0, 0); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3535 | argv++; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3536 | } |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3537 | /* Do we need to flag set_local_var() errors? |
| 3538 | * "assignment to readonly var" and "putenv error" |
| 3539 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3540 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3541 | debug_leave(); |
| 3542 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3543 | return rcode; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3544 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3545 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3546 | /* Expand the rest into (possibly) many strings each */ |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3547 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3548 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3549 | x = find_builtin(argv_expanded[0]); |
| 3550 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3551 | funcp = NULL; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3552 | if (!x) |
| 3553 | funcp = find_function(argv_expanded[0]); |
| 3554 | #endif |
| 3555 | if (x || funcp) { |
| 3556 | if (!funcp) { |
| 3557 | if (x->function == builtin_exec && argv_expanded[1] == NULL) { |
| 3558 | debug_printf("exec with redirects only\n"); |
| 3559 | rcode = setup_redirects(command, NULL); |
| 3560 | goto clean_up_and_ret1; |
| 3561 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3562 | } |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3563 | /* setup_redirects acts on file descriptors, not FILEs. |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3564 | * This is perfect for work that comes after exec(). |
| 3565 | * Is it really safe for inline use? Experimentally, |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3566 | * things seem to work. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3567 | rcode = setup_redirects(command, squirrel); |
| 3568 | if (rcode == 0) { |
| 3569 | new_env = expand_assignments(argv, command->assignment_cnt); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3570 | old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3571 | if (!funcp) { |
| 3572 | debug_printf_exec(": builtin '%s' '%s'...\n", |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3573 | x->cmd, argv_expanded[1]); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3574 | rcode = x->function(argv_expanded) & 0xff; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3575 | fflush(NULL); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3576 | } |
| 3577 | #if ENABLE_HUSH_FUNCTIONS |
| 3578 | else { |
| 3579 | debug_printf_exec(": function '%s' '%s'...\n", |
| 3580 | funcp->name, argv_expanded[1]); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3581 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3582 | } |
| 3583 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3584 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3585 | #if ENABLE_FEATURE_SH_STANDALONE |
| 3586 | clean_up_and_ret: |
| 3587 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3588 | restore_redirects(squirrel); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3589 | unset_vars(new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3590 | add_vars(old_vars); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3591 | clean_up_and_ret1: |
| 3592 | free(argv_expanded); |
| 3593 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3594 | debug_leave(); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3595 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 3596 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3597 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3598 | |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3599 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3600 | i = find_applet_by_name(argv_expanded[0]); |
| 3601 | if (i >= 0 && APPLET_IS_NOFORK(i)) { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3602 | rcode = setup_redirects(command, squirrel); |
| 3603 | if (rcode == 0) { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3604 | new_env = expand_assignments(argv, command->assignment_cnt); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3605 | old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3606 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3607 | argv_expanded[0], argv_expanded[1]); |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 3608 | rcode = run_nofork_applet(i, argv_expanded); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3609 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3610 | goto clean_up_and_ret; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3611 | } |
| 3612 | #endif |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3613 | /* It is neither builtin nor applet. We must fork. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3614 | } |
| 3615 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3616 | must_fork: |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3617 | /* NB: argv_expanded may already be created, and that |
| 3618 | * might include `cmd` runs! Do not rerun it! We *must* |
| 3619 | * use argv_expanded if it's non-NULL */ |
| 3620 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3621 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3622 | pi->alive_cmds = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3623 | nextin = 0; |
| 3624 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3625 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3626 | struct fd_pair pipefds; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3627 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3628 | volatile nommu_save_t nommu_save; |
| 3629 | nommu_save.new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3630 | nommu_save.old_vars = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3631 | nommu_save.argv = NULL; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3632 | nommu_save.argv_from_re_execing = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3633 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3634 | command = &(pi->cmds[i]); |
| 3635 | if (command->argv) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3636 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 3637 | command->argv[0], command->argv[1]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3638 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3639 | debug_printf_exec(": pipe member with no argv\n"); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3640 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3641 | |
| 3642 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3643 | pipefds.rd = 0; |
| 3644 | pipefds.wr = 1; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3645 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3646 | xpiped_pair(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3647 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3648 | command->pid = BB_MMU ? fork() : vfork(); |
| 3649 | if (!command->pid) { /* child */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3650 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3651 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3652 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3653 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3654 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3655 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3656 | pid_t pgrp; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3657 | pgrp = pi->pgrp; |
| 3658 | if (pgrp < 0) /* true for 1st process only */ |
| 3659 | pgrp = getpid(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3660 | if (setpgid(0, pgrp) == 0 |
| 3661 | && pi->followup != PIPE_BG |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 3662 | && G_saved_tty_pgrp /* we have ctty */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3663 | ) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3664 | /* We do it in *every* child, not just first, |
| 3665 | * to avoid races */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3666 | tcsetpgrp(G_interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3667 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3668 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3669 | #endif |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3670 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 3671 | /* 1st cmd in backgrounded pipe |
| 3672 | * should have its stdin /dev/null'ed */ |
| 3673 | close(0); |
| 3674 | if (open(bb_dev_null, O_RDONLY)) |
| 3675 | xopen("/", O_RDONLY); |
| 3676 | } else { |
| 3677 | xmove_fd(nextin, 0); |
| 3678 | } |
| 3679 | xmove_fd(pipefds.wr, 1); |
| 3680 | if (pipefds.rd > 1) |
| 3681 | close(pipefds.rd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3682 | /* Like bash, explicit redirects override pipes, |
| 3683 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3684 | if (setup_redirects(command, NULL)) |
| 3685 | _exit(1); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3686 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3687 | /* Restore default handlers just prior to exec */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 3688 | /*signal(SIGCHLD, SIG_DFL); - so far we don't have any handlers */ |
| 3689 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3690 | /* Stores to nommu_save list of env vars putenv'ed |
| 3691 | * (NOMMU, on MMU we don't need that) */ |
| 3692 | /* cast away volatility... */ |
| 3693 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3694 | /* pseudo_exec() does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3695 | } |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 3696 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 3697 | /* parent or error */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3698 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3699 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3700 | /* Clean up after vforked child */ |
| 3701 | free(nommu_save.argv); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3702 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3703 | unset_vars(nommu_save.new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3704 | add_vars(nommu_save.old_vars); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3705 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3706 | free(argv_expanded); |
| 3707 | argv_expanded = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3708 | if (command->pid < 0) { /* [v]fork failed */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3709 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 3710 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3711 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3712 | pi->alive_cmds++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3713 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3714 | /* Second and next children need to know pid of first one */ |
| 3715 | if (pi->pgrp < 0) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3716 | pi->pgrp = command->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3717 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3718 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3719 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3720 | if (i) |
| 3721 | close(nextin); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3722 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3723 | close(pipefds.wr); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3724 | /* Pass read (output) pipe end to next iteration */ |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3725 | nextin = pipefds.rd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3726 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3727 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3728 | if (!pi->alive_cmds) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3729 | debug_leave(); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3730 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 3731 | return 1; |
| 3732 | } |
| 3733 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3734 | debug_leave(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3735 | 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] | 3736 | return -1; |
| 3737 | } |
| 3738 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 3739 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3740 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3741 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3742 | static const char *const PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3743 | [PIPE_SEQ] = "SEQ", |
| 3744 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3745 | [PIPE_OR ] = "OR" , |
| 3746 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3747 | }; |
| 3748 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3749 | [RES_NONE ] = "NONE" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3750 | #if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3751 | [RES_IF ] = "IF" , |
| 3752 | [RES_THEN ] = "THEN" , |
| 3753 | [RES_ELIF ] = "ELIF" , |
| 3754 | [RES_ELSE ] = "ELSE" , |
| 3755 | [RES_FI ] = "FI" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3756 | #endif |
| 3757 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3758 | [RES_FOR ] = "FOR" , |
| 3759 | [RES_WHILE] = "WHILE", |
| 3760 | [RES_UNTIL] = "UNTIL", |
| 3761 | [RES_DO ] = "DO" , |
| 3762 | [RES_DONE ] = "DONE" , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 3763 | #endif |
| 3764 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3765 | [RES_IN ] = "IN" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3766 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3767 | #if ENABLE_HUSH_CASE |
| 3768 | [RES_CASE ] = "CASE" , |
| 3769 | [RES_MATCH] = "MATCH", |
| 3770 | [RES_CASEI] = "CASEI", |
| 3771 | [RES_ESAC ] = "ESAC" , |
| 3772 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3773 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3774 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3775 | }; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3776 | static const char *const GRPTYPE[] = { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3777 | "{}", |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 3778 | "()", |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3779 | #if ENABLE_HUSH_FUNCTIONS |
| 3780 | "func()", |
| 3781 | #endif |
| 3782 | }; |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3783 | |
| 3784 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3785 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3786 | pin = 0; |
| 3787 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3788 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 3789 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3790 | prn = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3791 | while (prn < pi->num_cmds) { |
| 3792 | struct command *command = &pi->cmds[prn]; |
| 3793 | char **argv = command->argv; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3794 | |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3795 | fprintf(stderr, "%*s cmd %d assignment_cnt:%d", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3796 | lvl*2, "", prn, |
| 3797 | command->assignment_cnt); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3798 | if (command->group) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3799 | fprintf(stderr, " group %s: (argv=%p)\n", |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3800 | GRPTYPE[command->grp_type], |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3801 | argv); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3802 | debug_print_tree(command->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3803 | prn++; |
| 3804 | continue; |
| 3805 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3806 | if (argv) while (*argv) { |
| 3807 | fprintf(stderr, " '%s'", *argv); |
| 3808 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 3809 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3810 | fprintf(stderr, "\n"); |
| 3811 | prn++; |
| 3812 | } |
| 3813 | pi = pi->next; |
| 3814 | pin++; |
| 3815 | } |
| 3816 | } |
| 3817 | #endif |
| 3818 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3819 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 3820 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3821 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3822 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3823 | #if ENABLE_HUSH_CASE |
| 3824 | char *case_word = NULL; |
| 3825 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3826 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3827 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3828 | char *for_varname = NULL; |
| 3829 | char **for_lcur = NULL; |
| 3830 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3831 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3832 | smallint last_followup; |
| 3833 | smalluint rcode; |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 3834 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3835 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3836 | #else |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3837 | enum { cond_code = 0 }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3838 | #endif |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 3839 | #if HAS_KEYWORDS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3840 | smallint rword; /* enum reserved_style */ |
| 3841 | smallint last_rword; /* ditto */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 3842 | #endif |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 3843 | |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 3844 | 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] | 3845 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 3846 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3847 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3848 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 3849 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 3850 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3851 | continue; |
| 3852 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 3853 | if (cpipe->next == NULL) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3854 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3855 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3856 | 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] | 3857 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3858 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3859 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 3860 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3861 | continue; |
| 3862 | /* 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] | 3863 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 3864 | || 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] | 3865 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3866 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3867 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3868 | 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] | 3869 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3870 | } |
| 3871 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3872 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3873 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 3874 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 3875 | * in order to return, no direct "return" statements please. |
| 3876 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 3877 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3878 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 3879 | G.run_list_level++; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3880 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3881 | |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 3882 | #if HAS_KEYWORDS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3883 | rword = RES_NONE; |
| 3884 | last_rword = RES_XXXX; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 3885 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3886 | last_followup = PIPE_SEQ; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 3887 | rcode = G.last_exitcode; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3888 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3889 | /* Go through list of pipes, (maybe) executing them. */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 3890 | for (; pi; pi = IF_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 3891 | if (G.flag_SIGINT) |
| 3892 | break; |
| 3893 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3894 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3895 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 3896 | rword, cond_code, last_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3897 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 3898 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3899 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 3900 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3901 | /* start of a loop: remember where loop starts */ |
| 3902 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3903 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3904 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3905 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3906 | /* Still in the same "if...", "then..." or "do..." branch? */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 3907 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3908 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 3909 | || (rcode != 0 && last_followup == PIPE_AND) |
| 3910 | ) { |
| 3911 | /* It is "<true> || CMD" or "<false> && CMD" |
| 3912 | * and we should not execute CMD */ |
| 3913 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 3914 | last_followup = pi->followup; |
| 3915 | continue; |
| 3916 | } |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3917 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3918 | last_followup = pi->followup; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 3919 | IF_HAS_KEYWORDS(last_rword = rword;) |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3920 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3921 | if (cond_code) { |
| 3922 | if (rword == RES_THEN) { |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 3923 | /* if false; then ... fi has exitcode 0! */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 3924 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3925 | /* "if <false> THEN cmd": skip cmd */ |
| 3926 | continue; |
| 3927 | } |
| 3928 | } else { |
| 3929 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 3930 | /* "if <true> then ... ELSE/ELIF cmd": |
| 3931 | * skip cmd and all following ones */ |
| 3932 | break; |
| 3933 | } |
| 3934 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3935 | #endif |
| 3936 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3937 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3938 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 3939 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3940 | |
| 3941 | static const char encoded_dollar_at[] ALIGN1 = { |
| 3942 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 3943 | }; /* encoded representation of "$@" */ |
| 3944 | static const char *const encoded_dollar_at_argv[] = { |
| 3945 | encoded_dollar_at, NULL |
| 3946 | }; /* argv list with one element: "$@" */ |
| 3947 | char **vals; |
| 3948 | |
| 3949 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 3950 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3951 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3952 | if (!pi->next->cmds[0].argv) { |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 3953 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3954 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3955 | break; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3956 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3957 | vals = pi->next->cmds[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3958 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3959 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3960 | debug_print_strings("for_list made from", vals); |
| 3961 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3962 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3963 | debug_print_strings("for_list", for_list); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3964 | for_varname = pi->cmds[0].argv[0]; |
| 3965 | pi->cmds[0].argv[0] = NULL; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3966 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3967 | free(pi->cmds[0].argv[0]); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3968 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 3969 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3970 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 3971 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3972 | for_lcur = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3973 | pi->cmds[0].argv[0] = for_varname; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3974 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3975 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3976 | /* Insert next value from for_lcur */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3977 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3978 | pi->cmds[0].argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++); |
| 3979 | pi->cmds[0].assignment_cnt = 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3980 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3981 | if (rword == RES_IN) { |
| 3982 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 3983 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3984 | if (rword == RES_DONE) { |
| 3985 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3986 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3987 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3988 | #if ENABLE_HUSH_CASE |
| 3989 | if (rword == RES_CASE) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3990 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3991 | continue; |
| 3992 | } |
| 3993 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 3994 | char **argv; |
| 3995 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3996 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 3997 | break; |
| 3998 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3999 | argv = pi->cmds->argv; |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4000 | while (*argv) { |
| 4001 | char *pattern = expand_string_to_string(*argv); |
| 4002 | /* TODO: which FNM_xxx flags to use? */ |
| 4003 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 4004 | free(pattern); |
| 4005 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 4006 | free(case_word); /* make future "word)" stop */ |
| 4007 | case_word = NULL; |
| 4008 | break; |
| 4009 | } |
| 4010 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4011 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4012 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4013 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4014 | if (rword == RES_CASEI) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4015 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4016 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4017 | } |
| 4018 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4019 | /* Just pressing <enter> in shell should check for jobs. |
| 4020 | * OTOH, in non-interactive shell this is useless |
| 4021 | * and only leads to extra job checks */ |
| 4022 | if (pi->num_cmds == 0) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4023 | if (G_interactive_fd) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4024 | goto check_jobs_and_continue; |
| 4025 | continue; |
| 4026 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4027 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4028 | /* After analyzing all keywords and conditions, we decided |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4029 | * to execute this pipe. NB: have to do checkjobs(NULL) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4030 | * after run_pipe to collect any background children, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4031 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4032 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4033 | { |
| 4034 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4035 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4036 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4037 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4038 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 4039 | if (r != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4040 | /* We ran a builtin, function, or group. |
| 4041 | * rcode is already known |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4042 | * and we don't need to wait for anything. */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4043 | G.last_exitcode = rcode; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4044 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4045 | check_and_run_traps(0); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4046 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4047 | /* Was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4048 | if (G.flag_break_continue) { |
| 4049 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4050 | /* We might fall into outer *loop*, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4051 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4052 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4053 | G.depth_break_continue--; |
| 4054 | if (G.depth_break_continue == 0) |
| 4055 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4056 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 4057 | } /* 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] | 4058 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4059 | goto check_jobs_and_break; |
| 4060 | /* "continue": simulate end of loop */ |
| 4061 | rword = RES_DONE; |
| 4062 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4063 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4064 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4065 | #if ENABLE_HUSH_FUNCTIONS |
| 4066 | if (G.flag_return_in_progress == 1) |
| 4067 | goto check_jobs_and_break; |
| 4068 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4069 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4070 | /* What does bash do with attempts to background builtins? */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4071 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4072 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 4073 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4074 | check_and_run_traps(0); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4075 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4076 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4077 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4078 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4079 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4080 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4081 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4082 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4083 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4084 | /* Waits for completion, then fg's main shell */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4085 | rcode = checkjobs_and_fg_shell(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4086 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4087 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4088 | } else |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4089 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4090 | { /* This one just waits for completion */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4091 | rcode = checkjobs(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4092 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4093 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4094 | } |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4095 | G.last_exitcode = rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4096 | } |
| 4097 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4098 | |
| 4099 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4100 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4101 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4102 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4103 | #endif |
| 4104 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4105 | /* Beware of "while false; true; do ..."! */ |
| 4106 | if (pi->next && pi->next->res_word == RES_DO) { |
| 4107 | if (rword == RES_WHILE) { |
| 4108 | if (rcode) { |
| 4109 | /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4110 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4111 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
| 4112 | goto check_jobs_and_break; |
| 4113 | } |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 4114 | } |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4115 | if (rword == RES_UNTIL) { |
| 4116 | if (!rcode) { |
| 4117 | debug_printf_exec(": until expr is true: breaking\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4118 | check_jobs_and_break: |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4119 | checkjobs(NULL); |
| 4120 | break; |
| 4121 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4122 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4123 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4124 | #endif |
Mike Frysinger | 8ec1c9d | 2009-03-29 00:45:26 +0000 | [diff] [blame] | 4125 | |
| 4126 | check_jobs_and_continue: |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 4127 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4128 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4129 | |
| 4130 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4131 | G.run_list_level--; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4132 | #endif |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4133 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4134 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4135 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4136 | free(for_list); |
| 4137 | #endif |
| 4138 | #if ENABLE_HUSH_CASE |
| 4139 | free(case_word); |
| 4140 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4141 | debug_leave(); |
| 4142 | 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] | 4143 | return rcode; |
| 4144 | } |
| 4145 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4146 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4147 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4148 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4149 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4150 | debug_printf_exec("run_and_free_list entered\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4151 | if (!G.fake_mode) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4152 | 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] | 4153 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4154 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4155 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4156 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4157 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4158 | free_pipe_list(pi); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 4159 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4160 | return rcode; |
| 4161 | } |
| 4162 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4163 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 4164 | static struct pipe *new_pipe(void) |
| 4165 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4166 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4167 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4168 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4169 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4170 | return pi; |
| 4171 | } |
| 4172 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4173 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 4174 | * if ctx->command is NULL. |
| 4175 | * No errors possible here. |
| 4176 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4177 | static int done_command(struct parse_context *ctx) |
| 4178 | { |
| 4179 | /* The command is really already in the pipe structure, so |
| 4180 | * advance the pipe counter and make a new, null command. */ |
| 4181 | struct pipe *pi = ctx->pipe; |
| 4182 | struct command *command = ctx->command; |
| 4183 | |
| 4184 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4185 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4186 | 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] | 4187 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4188 | } |
| 4189 | pi->num_cmds++; |
| 4190 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4191 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4192 | } else { |
| 4193 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 4194 | } |
| 4195 | |
| 4196 | /* Only real trickiness here is that the uncommitted |
| 4197 | * command structure is not counted in pi->num_cmds. */ |
| 4198 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4199 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 4200 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4201 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4202 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 4203 | } |
| 4204 | |
| 4205 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 4206 | { |
| 4207 | int not_null; |
| 4208 | |
| 4209 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 4210 | /* Close previous command */ |
| 4211 | not_null = done_command(ctx); |
| 4212 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4213 | #if HAS_KEYWORDS |
| 4214 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 4215 | ctx->ctx_inverted = 0; |
| 4216 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 4217 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4218 | |
| 4219 | /* Without this check, even just <enter> on command line generates |
| 4220 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4221 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4222 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4223 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4224 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4225 | #endif |
| 4226 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4227 | || ctx->ctx_res_w == RES_DONE |
| 4228 | || ctx->ctx_res_w == RES_FOR |
| 4229 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4230 | #endif |
| 4231 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4232 | || ctx->ctx_res_w == RES_ESAC |
| 4233 | #endif |
| 4234 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4235 | struct pipe *new_p; |
| 4236 | debug_printf_parse("done_pipe: adding new pipe: " |
| 4237 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 4238 | not_null, ctx->ctx_res_w); |
| 4239 | new_p = new_pipe(); |
| 4240 | ctx->pipe->next = new_p; |
| 4241 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4242 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4243 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4244 | * This is used to control execution. |
| 4245 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 4246 | * cases where variable or value happens to match a keyword): |
| 4247 | */ |
| 4248 | #if ENABLE_HUSH_LOOPS |
| 4249 | if (ctx->ctx_res_w == RES_FOR |
| 4250 | || ctx->ctx_res_w == RES_IN) |
| 4251 | ctx->ctx_res_w = RES_NONE; |
| 4252 | #endif |
| 4253 | #if ENABLE_HUSH_CASE |
| 4254 | if (ctx->ctx_res_w == RES_MATCH) |
| 4255 | ctx->ctx_res_w = RES_CASEI; |
| 4256 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4257 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4258 | /* Create the memory for command, roughly: |
| 4259 | * ctx->pipe->cmds = new struct command; |
| 4260 | * ctx->command = &ctx->pipe->cmds[0]; |
| 4261 | */ |
| 4262 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4263 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4264 | } |
| 4265 | debug_printf_parse("done_pipe return\n"); |
| 4266 | } |
| 4267 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4268 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4269 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4270 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4271 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4272 | /* Create the memory for command, roughly: |
| 4273 | * ctx->pipe->cmds = new struct command; |
| 4274 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4275 | */ |
| 4276 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4277 | } |
| 4278 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4279 | /* If a reserved word is found and processed, parse context is modified |
| 4280 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4281 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4282 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4283 | struct reserved_combo { |
| 4284 | char literal[6]; |
| 4285 | unsigned char res; |
| 4286 | unsigned char assignment_flag; |
| 4287 | int flag; |
| 4288 | }; |
| 4289 | enum { |
| 4290 | FLAG_END = (1 << RES_NONE ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4291 | #if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4292 | FLAG_IF = (1 << RES_IF ), |
| 4293 | FLAG_THEN = (1 << RES_THEN ), |
| 4294 | FLAG_ELIF = (1 << RES_ELIF ), |
| 4295 | FLAG_ELSE = (1 << RES_ELSE ), |
| 4296 | FLAG_FI = (1 << RES_FI ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4297 | #endif |
| 4298 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4299 | FLAG_FOR = (1 << RES_FOR ), |
| 4300 | FLAG_WHILE = (1 << RES_WHILE), |
| 4301 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 4302 | FLAG_DO = (1 << RES_DO ), |
| 4303 | FLAG_DONE = (1 << RES_DONE ), |
| 4304 | FLAG_IN = (1 << RES_IN ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4305 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4306 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4307 | FLAG_MATCH = (1 << RES_MATCH), |
| 4308 | FLAG_ESAC = (1 << RES_ESAC ), |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4309 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4310 | FLAG_START = (1 << RES_XXXX ), |
| 4311 | }; |
| 4312 | |
| 4313 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 4314 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4315 | /* Mostly a list of accepted follow-up reserved words. |
| 4316 | * FLAG_END means we are done with the sequence, and are ready |
| 4317 | * to turn the compound list into a command. |
| 4318 | * FLAG_START means the word must start a new compound list. |
| 4319 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4320 | static const struct reserved_combo reserved_list[] = { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4321 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4322 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 4323 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 4324 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 4325 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 4326 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 4327 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4328 | #endif |
| 4329 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4330 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 4331 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 4332 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 4333 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 4334 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 4335 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4336 | #endif |
| 4337 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4338 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 4339 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4340 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4341 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4342 | const struct reserved_combo *r; |
| 4343 | |
| 4344 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 4345 | if (strcmp(word->data, r->literal) == 0) |
| 4346 | return r; |
| 4347 | } |
| 4348 | return NULL; |
| 4349 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4350 | /* Return 0: not a keyword, 1: keyword |
| 4351 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4352 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 4353 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4354 | #if ENABLE_HUSH_CASE |
| 4355 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4356 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4357 | }; |
| 4358 | #endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4359 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4360 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4361 | if (word->o_quoted) |
| 4362 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4363 | r = match_reserved_word(word); |
| 4364 | if (!r) |
| 4365 | return 0; |
| 4366 | |
| 4367 | 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] | 4368 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4369 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE) |
| 4370 | /* "case word IN ..." - IN part starts first match part */ |
| 4371 | r = &reserved_match; |
| 4372 | else |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4373 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4374 | if (r->flag == 0) { /* '!' */ |
| 4375 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4376 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4377 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4378 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4379 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4380 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4381 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4382 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4383 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4384 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4385 | old = xmalloc(sizeof(*old)); |
| 4386 | debug_printf_parse("push stack %p\n", old); |
| 4387 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4388 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4389 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4390 | } 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] | 4391 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4392 | ctx->ctx_res_w = RES_SNTX; |
| 4393 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4394 | } else { |
| 4395 | /* "{...} fi" is ok. "{...} if" is not |
| 4396 | * Example: |
| 4397 | * if { echo foo; } then { echo bar; } fi */ |
| 4398 | if (ctx->command->group) |
| 4399 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4400 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4401 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4402 | ctx->ctx_res_w = r->res; |
| 4403 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4404 | word->o_assignment = r->assignment_flag; |
| 4405 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4406 | if (ctx->old_flag & FLAG_END) { |
| 4407 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4408 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4409 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4410 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4411 | old = ctx->stack; |
| 4412 | old->command->group = ctx->list_head; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4413 | old->command->grp_type = GRP_NORMAL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4414 | #if !BB_MMU |
| 4415 | o_addstr(&old->as_string, ctx->as_string.data); |
| 4416 | o_free_unsafe(&ctx->as_string); |
| 4417 | old->command->group_as_string = xstrdup(old->as_string.data); |
| 4418 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 4419 | old->command->group_as_string); |
| 4420 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4421 | *ctx = *old; /* physical copy */ |
| 4422 | free(old); |
| 4423 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4424 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4425 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4426 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4427 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4428 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4429 | * Normal return is 0. Syntax errors return 1. |
| 4430 | * Note: on return, word is reset, but not o_free'd! |
| 4431 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4432 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4433 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4434 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4435 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4436 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4437 | if (word->length == 0 && word->o_quoted == 0) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4438 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 4439 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4440 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4441 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4442 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4443 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 4444 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4445 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4446 | * "2.7 Redirection |
| 4447 | * ...the word that follows the redirection operator |
| 4448 | * shall be subjected to tilde expansion, parameter expansion, |
| 4449 | * command substitution, arithmetic expansion, and quote |
| 4450 | * removal. Pathname expansion shall not be performed |
| 4451 | * on the word by a non-interactive shell; an interactive |
| 4452 | * shell may perform it, but shall do so only when |
| 4453 | * the expansion would result in one word." |
| 4454 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4455 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4456 | /* Cater for >\file case: |
| 4457 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 4458 | * Same with heredocs: |
| 4459 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 4460 | */ |
| 4461 | unbackslash(ctx->pending_redirect->rd_filename); |
| 4462 | /* Is it <<"HEREDOC"? */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4463 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC |
| 4464 | && word->o_quoted |
| 4465 | ) { |
| 4466 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 4467 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4468 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4469 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4470 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4471 | /* If this word wasn't an assignment, next ones definitely |
| 4472 | * can't be assignments. Even if they look like ones. */ |
| 4473 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 4474 | && word->o_assignment != WORD_IS_KEYWORD |
| 4475 | ) { |
| 4476 | word->o_assignment = NOT_ASSIGNMENT; |
| 4477 | } else { |
| 4478 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
| 4479 | command->assignment_cnt++; |
| 4480 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 4481 | } |
| 4482 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4483 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4484 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 4485 | if (ctx->ctx_dsemicolon |
| 4486 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 4487 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 4488 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4489 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 4490 | ctx->ctx_dsemicolon = 0; |
| 4491 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4492 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4493 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4494 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4495 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 4496 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4497 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4498 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4499 | debug_printf_parse("checking '%s' for reserved-ness\n", word->data); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4500 | if (reserved_word(word, ctx)) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4501 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4502 | debug_printf_parse("done_word return %d\n", |
| 4503 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4504 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4505 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4506 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4507 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4508 | if (command->group) { |
| 4509 | /* "{ echo foo; } echo bar" - bad */ |
| 4510 | syntax_error_at(word->data); |
| 4511 | debug_printf_parse("done_word return 1: syntax error, " |
| 4512 | "groups and arglists don't mix\n"); |
| 4513 | return 1; |
| 4514 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4515 | 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] | 4516 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 4517 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4518 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4519 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4520 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 4521 | char *p = word->data; |
| 4522 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 4523 | && (p[1] & 0x7f) == '@' |
| 4524 | && p[2] == SPECIAL_VAR_SYMBOL |
| 4525 | ) { |
| 4526 | p += 3; |
| 4527 | } |
| 4528 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4529 | /* saw no "$@", or not only "$@" but some |
| 4530 | * real text is there too */ |
| 4531 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 4532 | * e.g. "", $empty"" etc to not disappear */ |
| 4533 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 4534 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 4535 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 4536 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4537 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4538 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4539 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4540 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4541 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4542 | if (ctx->ctx_res_w == RES_FOR) { |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4543 | if (word->o_quoted |
| 4544 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 4545 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4546 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4547 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4548 | return 1; |
| 4549 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4550 | /* Force FOR to have just one word (variable name) */ |
| 4551 | /* NB: basically, this makes hush see "for v in ..." |
| 4552 | * syntax as if it is "for v; in ...". FOR and IN become |
| 4553 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4554 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4555 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4556 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4557 | #if ENABLE_HUSH_CASE |
| 4558 | /* Force CASE to have just one word */ |
| 4559 | if (ctx->ctx_res_w == RES_CASE) { |
| 4560 | done_pipe(ctx, PIPE_SEQ); |
| 4561 | } |
| 4562 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4563 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4564 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4565 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4566 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4567 | return 0; |
| 4568 | } |
| 4569 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4570 | |
| 4571 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 4572 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4573 | * Return: |
| 4574 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 4575 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 4576 | * REDIRFD_TO_FILE if no & was seen, |
| 4577 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4578 | */ |
| 4579 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4580 | #define parse_redir_right_fd(as_string, input) \ |
| 4581 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4582 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4583 | 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] | 4584 | { |
| 4585 | int ch, d, ok; |
| 4586 | |
| 4587 | ch = i_peek(input); |
| 4588 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4589 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4590 | |
| 4591 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4592 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4593 | ch = i_peek(input); |
| 4594 | if (ch == '-') { |
| 4595 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4596 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4597 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4598 | } |
| 4599 | d = 0; |
| 4600 | ok = 0; |
| 4601 | while (ch != EOF && isdigit(ch)) { |
| 4602 | d = d*10 + (ch-'0'); |
| 4603 | ok = 1; |
| 4604 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4605 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4606 | ch = i_peek(input); |
| 4607 | } |
| 4608 | if (ok) return d; |
| 4609 | |
| 4610 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 4611 | |
| 4612 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4613 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4614 | } |
| 4615 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4616 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4617 | */ |
| 4618 | static int parse_redirect(struct parse_context *ctx, |
| 4619 | int fd, |
| 4620 | redir_type style, |
| 4621 | struct in_str *input) |
| 4622 | { |
| 4623 | struct command *command = ctx->command; |
| 4624 | struct redir_struct *redir; |
| 4625 | struct redir_struct **redirp; |
| 4626 | int dup_num; |
| 4627 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4628 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4629 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4630 | /* Check for a '>&1' type redirect */ |
| 4631 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 4632 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 4633 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4634 | } else { |
| 4635 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4636 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4637 | if (dup_num) { /* <<-... */ |
| 4638 | ch = i_getch(input); |
| 4639 | nommu_addchr(&ctx->as_string, ch); |
| 4640 | ch = i_peek(input); |
| 4641 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4642 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4643 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4644 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4645 | int ch = i_peek(input); |
| 4646 | if (ch == '|') { |
| 4647 | /* >|FILE redirect ("clobbering" >). |
| 4648 | * Since we do not support "set -o noclobber" yet, |
| 4649 | * >| and > are the same for now. Just eat |. |
| 4650 | */ |
| 4651 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4652 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4653 | } |
| 4654 | } |
| 4655 | |
| 4656 | /* Create a new redir_struct and append it to the linked list */ |
| 4657 | redirp = &command->redirects; |
| 4658 | while ((redir = *redirp) != NULL) { |
| 4659 | redirp = &(redir->next); |
| 4660 | } |
| 4661 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 4662 | /* redir->next = NULL; */ |
| 4663 | /* redir->rd_filename = NULL; */ |
| 4664 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4665 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4666 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4667 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 4668 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4669 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4670 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4671 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4672 | /* Erik had a check here that the file descriptor in question |
| 4673 | * is legit; I postpone that to "run time" |
| 4674 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4675 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 4676 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4677 | } else { |
| 4678 | /* Set ctx->pending_redirect, so we know what to do at the |
| 4679 | * end of the next parsed word. */ |
| 4680 | ctx->pending_redirect = redir; |
| 4681 | } |
| 4682 | return 0; |
| 4683 | } |
| 4684 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4685 | /* If a redirect is immediately preceded by a number, that number is |
| 4686 | * supposed to tell which file descriptor to redirect. This routine |
| 4687 | * looks for such preceding numbers. In an ideal world this routine |
| 4688 | * needs to handle all the following classes of redirects... |
| 4689 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 4690 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 4691 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 4692 | * 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] | 4693 | * |
| 4694 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4695 | * "2.7 Redirection |
| 4696 | * ... If n is quoted, the number shall not be recognized as part of |
| 4697 | * the redirection expression. For example: |
| 4698 | * echo \2>a |
| 4699 | * writes the character 2 into file a" |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4700 | * We are getting it right by setting ->o_quoted on any \<char> |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4701 | * |
| 4702 | * A -1 return means no valid number was found, |
| 4703 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4704 | */ |
| 4705 | static int redirect_opt_num(o_string *o) |
| 4706 | { |
| 4707 | int num; |
| 4708 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4709 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4710 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4711 | num = bb_strtou(o->data, NULL, 10); |
| 4712 | if (errno || num < 0) |
| 4713 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4714 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4715 | return num; |
| 4716 | } |
| 4717 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4718 | #if BB_MMU |
| 4719 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 4720 | fetch_till_str(input, word, skip_tabs) |
| 4721 | #endif |
| 4722 | static char *fetch_till_str(o_string *as_string, |
| 4723 | struct in_str *input, |
| 4724 | const char *word, |
| 4725 | int skip_tabs) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4726 | { |
| 4727 | o_string heredoc = NULL_O_STRING; |
| 4728 | int past_EOL = 0; |
| 4729 | int ch; |
| 4730 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4731 | goto jump_in; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4732 | while (1) { |
| 4733 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4734 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4735 | if (ch == '\n') { |
| 4736 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 4737 | heredoc.data[past_EOL] = '\0'; |
| 4738 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 4739 | return heredoc.data; |
| 4740 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4741 | do { |
| 4742 | o_addchr(&heredoc, ch); |
| 4743 | past_EOL = heredoc.length; |
| 4744 | jump_in: |
| 4745 | do { |
| 4746 | ch = i_getch(input); |
| 4747 | nommu_addchr(as_string, ch); |
| 4748 | } while (skip_tabs && ch == '\t'); |
| 4749 | } while (ch == '\n'); |
| 4750 | } |
| 4751 | if (ch == EOF) { |
| 4752 | o_free_unsafe(&heredoc); |
| 4753 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4754 | } |
| 4755 | o_addchr(&heredoc, ch); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4756 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4757 | } |
| 4758 | } |
| 4759 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4760 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 4761 | * and load them all. There should be exactly heredoc_cnt of them. |
| 4762 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4763 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 4764 | { |
| 4765 | struct pipe *pi = ctx->list_head; |
| 4766 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4767 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4768 | int i; |
| 4769 | struct command *cmd = pi->cmds; |
| 4770 | |
| 4771 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 4772 | pi->num_cmds, |
| 4773 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 4774 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4775 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4776 | |
| 4777 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 4778 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4779 | while (redir) { |
| 4780 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4781 | char *p; |
| 4782 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4783 | redir->rd_type = REDIRECT_HEREDOC2; |
| 4784 | /* redir->dup is (ab)used to indicate <<- */ |
| 4785 | p = fetch_till_str(&ctx->as_string, input, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4786 | redir->rd_filename, redir->rd_dup & HEREDOC_SKIPTABS); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4787 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4788 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4789 | return 1; |
| 4790 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4791 | free(redir->rd_filename); |
| 4792 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4793 | heredoc_cnt--; |
| 4794 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4795 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4796 | } |
| 4797 | cmd++; |
| 4798 | } |
| 4799 | pi = pi->next; |
| 4800 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4801 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4802 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4803 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4804 | bb_error_msg_and_die("heredoc BUG 2"); |
| 4805 | #endif |
| 4806 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
| 4809 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4810 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4811 | static FILE *generate_stream_from_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4812 | { |
| 4813 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4814 | int pid, channel[2]; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4815 | #if !BB_MMU |
| 4816 | char **to_free; |
| 4817 | #endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 4818 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 4819 | xpipe(channel); |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 4820 | pid = BB_MMU ? fork() : vfork(); |
| 4821 | if (pid < 0) |
| 4822 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4823 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4824 | if (pid == 0) { /* child */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4825 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 4826 | /* Process substitution is not considered to be usual |
| 4827 | * 'command execution'. |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 4828 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 4829 | */ |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 4830 | bb_signals(0 |
| 4831 | + (1 << SIGTSTP) |
| 4832 | + (1 << SIGTTIN) |
| 4833 | + (1 << SIGTTOU) |
| 4834 | , SIG_IGN); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4835 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 4836 | xmove_fd(channel[1], 1); |
| 4837 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4838 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4839 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4840 | reset_traps_to_defaults(); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4841 | parse_and_run_string(s); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4842 | _exit(G.last_exitcode); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4843 | #else |
| 4844 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 4845 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 4846 | * huge=`cat BIG` # was blocking here forever |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4847 | * echo OK |
| 4848 | */ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4849 | re_execute_shell(&to_free, |
| 4850 | s, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4851 | G.global_argv[0], |
| 4852 | G.global_argv + 1); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4853 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4854 | } |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4855 | |
| 4856 | /* parent */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4857 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4858 | #if !BB_MMU |
| 4859 | free(to_free); |
| 4860 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4861 | close(channel[1]); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 4862 | pf = fdopen(channel[0], "r"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4863 | return pf; |
| 4864 | } |
| 4865 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4866 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4867 | static int process_command_subs(o_string *dest, const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4868 | { |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4869 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4870 | struct in_str pipe_str; |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4871 | int ch, eol_cnt; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 4872 | |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4873 | pf = generate_stream_from_string(s); |
| 4874 | if (pf == NULL) |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4875 | return 1; |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4876 | close_on_exec_on(fileno(pf)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4877 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4878 | /* Now send results of command back into original context */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4879 | setup_file_in_str(&pipe_str, pf); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 4880 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4881 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 4882 | if (ch == '\n') { |
| 4883 | eol_cnt++; |
| 4884 | continue; |
| 4885 | } |
| 4886 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4887 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 4888 | eol_cnt--; |
| 4889 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 4890 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4891 | } |
| 4892 | |
| 4893 | debug_printf("done reading from pipe, pclose()ing\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4894 | /* Note: we got EOF, and we just close the read end of the pipe. |
| 4895 | * 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] | 4896 | * Try these: |
| 4897 | * echo `echo Hi; exec 1>&-; sleep 2` - bash waits 2 sec |
| 4898 | * `false`; echo $? - bash outputs "1" |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4899 | */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4900 | fclose(pf); |
| 4901 | debug_printf("closed FILE from child. return 0\n"); |
| 4902 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4903 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4904 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4905 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4906 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4907 | struct in_str *input, int ch) |
| 4908 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4909 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4910 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4911 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4912 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4913 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4914 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4915 | |
| 4916 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4917 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4918 | if (ch == '(' && !dest->o_quoted) { |
| 4919 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4920 | if (done_word(dest, ctx)) |
| 4921 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4922 | if (!command->argv) |
| 4923 | goto skip; /* (... */ |
| 4924 | if (command->argv[1]) { /* word word ... (... */ |
| 4925 | syntax_error_unexpected_ch('('); |
| 4926 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4927 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4928 | /* it is "word(..." or "word (..." */ |
| 4929 | do |
| 4930 | ch = i_getch(input); |
| 4931 | while (ch == ' ' || ch == '\t'); |
| 4932 | if (ch != ')') { |
| 4933 | syntax_error_unexpected_ch(ch); |
| 4934 | return 1; |
| 4935 | } |
| 4936 | nommu_addchr(&ctx->as_string, ch); |
| 4937 | do |
| 4938 | ch = i_getch(input); |
| 4939 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 4940 | if (ch != '{') { |
| 4941 | syntax_error_unexpected_ch(ch); |
| 4942 | return 1; |
| 4943 | } |
| 4944 | nommu_addchr(&ctx->as_string, ch); |
| 4945 | command->grp_type = GRP_FUNCTION; |
| 4946 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4947 | } |
| 4948 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4949 | if (command->argv /* word [word]{... */ |
| 4950 | || dest->length /* word{... */ |
| 4951 | || dest->o_quoted /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4952 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4953 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4954 | debug_printf_parse("parse_group return 1: " |
| 4955 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4956 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4957 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4958 | |
| 4959 | #if ENABLE_HUSH_FUNCTIONS |
| 4960 | skip: |
| 4961 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4962 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4963 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4964 | endch = ')'; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4965 | command->grp_type = GRP_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4966 | } else { |
| 4967 | /* bash does not allow "{echo...", requires whitespace */ |
| 4968 | ch = i_getch(input); |
| 4969 | if (ch != ' ' && ch != '\t' && ch != '\n') { |
| 4970 | syntax_error_unexpected_ch(ch); |
| 4971 | return 1; |
| 4972 | } |
| 4973 | nommu_addchr(&ctx->as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4974 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4975 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4976 | { |
| 4977 | #if !BB_MMU |
| 4978 | char *as_string = NULL; |
| 4979 | #endif |
| 4980 | pipe_list = parse_stream(&as_string, input, endch); |
| 4981 | #if !BB_MMU |
| 4982 | if (as_string) |
| 4983 | o_addstr(&ctx->as_string, as_string); |
| 4984 | #endif |
| 4985 | /* empty ()/{} or parse error? */ |
| 4986 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4987 | /* parse_stream already emitted error msg */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4988 | #if !BB_MMU |
| 4989 | free(as_string); |
| 4990 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4991 | debug_printf_parse("parse_group return 1: " |
| 4992 | "parse_stream returned %p\n", pipe_list); |
| 4993 | return 1; |
| 4994 | } |
| 4995 | command->group = pipe_list; |
| 4996 | #if !BB_MMU |
| 4997 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 4998 | command->group_as_string = as_string; |
| 4999 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 5000 | command->group_as_string); |
| 5001 | #endif |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5002 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5003 | debug_printf_parse("parse_group return 0\n"); |
| 5004 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5005 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5006 | } |
| 5007 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5008 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5009 | /* Subroutines for copying $(...) and `...` things */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5010 | static void add_till_backquote(o_string *dest, struct in_str *input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5011 | /* '...' */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5012 | 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] | 5013 | { |
| 5014 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5015 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5016 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5017 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5018 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5019 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5020 | if (ch == '\'') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5021 | return; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5022 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5023 | } |
| 5024 | } |
| 5025 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5026 | 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] | 5027 | { |
| 5028 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5029 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5030 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5031 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5032 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5033 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5034 | if (ch == '"') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5035 | return; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5036 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5037 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5038 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5039 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5040 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5041 | if (ch == '`') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5042 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5043 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5044 | continue; |
| 5045 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 5046 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5047 | } |
| 5048 | } |
| 5049 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 5050 | * \` quoting. |
| 5051 | * "Within the backquoted style of command substitution, backslash |
| 5052 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 5053 | * The search for the matching backquote shall be satisfied by the first |
| 5054 | * backquote found without a preceding backslash; during this search, |
| 5055 | * if a non-escaped backquote is encountered within a shell comment, |
| 5056 | * a here-document, an embedded command substitution of the $(command) |
| 5057 | * form, or a quoted string, undefined results occur. A single-quoted |
| 5058 | * or double-quoted string that begins, but does not end, within the |
| 5059 | * "`...`" sequence produces undefined results." |
| 5060 | * Example Output |
| 5061 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 5062 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5063 | static void add_till_backquote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5064 | { |
| 5065 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5066 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5067 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5068 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5069 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5070 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5071 | if (ch == '`') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5072 | return; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5073 | if (ch == '\\') { |
| 5074 | /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5075 | int ch2 = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5076 | if (ch2 == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5077 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5078 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5079 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5080 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5081 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5082 | ch = ch2; |
| 5083 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5084 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5085 | } |
| 5086 | } |
| 5087 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 5088 | * quoting and nested ()s. |
| 5089 | * "With the $(command) style of command substitution, all characters |
| 5090 | * following the open parenthesis to the matching closing parenthesis |
| 5091 | * constitute the command. Any valid shell script can be used for command, |
| 5092 | * except a script consisting solely of redirections which produces |
| 5093 | * unspecified results." |
| 5094 | * Example Output |
| 5095 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 5096 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 5097 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 5098 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5099 | 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] | 5100 | { |
| 5101 | int count = 0; |
| 5102 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5103 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5104 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5105 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5106 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5107 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5108 | if (ch == '(') |
| 5109 | count++; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5110 | if (ch == ')') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5111 | if (--count < 0) { |
| 5112 | if (!dbl) |
| 5113 | break; |
| 5114 | if (i_peek(input) == ')') { |
| 5115 | i_getch(input); |
| 5116 | break; |
| 5117 | } |
| 5118 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5119 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5120 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5121 | if (ch == '\'') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5122 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5123 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5124 | continue; |
| 5125 | } |
| 5126 | if (ch == '"') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5127 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5128 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5129 | continue; |
| 5130 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5131 | if (ch == '\\') { |
| 5132 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5133 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5134 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5135 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5136 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5137 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5138 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5139 | continue; |
| 5140 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5141 | } |
| 5142 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5143 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5144 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5145 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5146 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5147 | #define handle_dollar(as_string, dest, input) \ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5148 | handle_dollar(dest, input) |
| 5149 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5150 | static int handle_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5151 | o_string *dest, |
| 5152 | struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5153 | { |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 5154 | int expansion; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5155 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5156 | unsigned char quote_mask = dest->o_escape ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5157 | |
| 5158 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5159 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5160 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5161 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 5162 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5163 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5164 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5165 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5166 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5167 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5168 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5169 | if (!isalnum(ch) && ch != '_') |
| 5170 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5171 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5172 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5173 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5174 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5175 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5176 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5177 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5178 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5179 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5180 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5181 | o_addchr(dest, ch | quote_mask); |
| 5182 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5183 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5184 | case '$': /* pid */ |
| 5185 | case '!': /* last bg pid */ |
| 5186 | case '?': /* last exit code */ |
| 5187 | case '#': /* number of args */ |
| 5188 | case '*': /* args */ |
| 5189 | case '@': /* args */ |
| 5190 | goto make_one_char_var; |
| 5191 | case '{': { |
| 5192 | bool first_char, all_digits; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 5193 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5194 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5195 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5196 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 5197 | /* TODO: maybe someone will try to escape the '}' */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5198 | expansion = 0; |
| 5199 | first_char = true; |
| 5200 | all_digits = false; |
| 5201 | while (1) { |
| 5202 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5203 | nommu_addchr(as_string, ch); |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5204 | if (ch == '}') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5205 | break; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5206 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5207 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5208 | if (first_char) { |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5209 | if (ch == '#') { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5210 | /* ${#var}: length of var contents */ |
| 5211 | goto char_ok; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5212 | } |
| 5213 | if (isdigit(ch)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5214 | all_digits = true; |
| 5215 | goto char_ok; |
| 5216 | } |
| 5217 | } |
| 5218 | |
| 5219 | if (expansion < 2 |
| 5220 | && ( (all_digits && !isdigit(ch)) |
| 5221 | || (!all_digits && !isalnum(ch) && ch != '_') |
| 5222 | ) |
| 5223 | ) { |
| 5224 | /* handle parameter expansions |
| 5225 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 5226 | */ |
| 5227 | if (first_char) |
| 5228 | goto case_default; |
| 5229 | switch (ch) { |
| 5230 | case ':': /* null modifier */ |
| 5231 | if (expansion == 0) { |
| 5232 | debug_printf_parse(": null modifier\n"); |
| 5233 | ++expansion; |
| 5234 | break; |
| 5235 | } |
| 5236 | goto case_default; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5237 | case '#': /* remove prefix */ |
| 5238 | case '%': /* remove suffix */ |
| 5239 | if (expansion == 0) { |
| 5240 | debug_printf_parse(": remove suffix/prefix\n"); |
| 5241 | expansion = 2; |
| 5242 | break; |
| 5243 | } |
| 5244 | goto case_default; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5245 | case '-': /* default value */ |
| 5246 | case '=': /* assign default */ |
| 5247 | case '+': /* alternative */ |
| 5248 | case '?': /* error indicate */ |
| 5249 | debug_printf_parse(": parameter expansion\n"); |
| 5250 | expansion = 2; |
| 5251 | break; |
| 5252 | default: |
| 5253 | case_default: |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5254 | syntax_error_unterm_str("${name}"); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5255 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 5256 | return 1; |
| 5257 | } |
| 5258 | } |
| 5259 | char_ok: |
| 5260 | debug_printf_parse(": '%c'\n", ch); |
| 5261 | o_addchr(dest, ch | quote_mask); |
| 5262 | quote_mask = 0; |
| 5263 | first_char = false; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5264 | } /* while (1) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5265 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5266 | break; |
| 5267 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5268 | #if (ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK) |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5269 | case '(': { |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5270 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5271 | int pos; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5272 | # endif |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5273 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5274 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5275 | # if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5276 | if (i_peek(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5277 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5278 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5279 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5280 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5281 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5282 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5283 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5284 | add_till_closing_paren(dest, input, true); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5285 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5286 | if (as_string) { |
| 5287 | o_addstr(as_string, dest->data + pos); |
| 5288 | o_addchr(as_string, ')'); |
| 5289 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5290 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5291 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5292 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5293 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5294 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5295 | # endif |
| 5296 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5297 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5298 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5299 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5300 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5301 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5302 | add_till_closing_paren(dest, input, false); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5303 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5304 | if (as_string) { |
| 5305 | o_addstr(as_string, dest->data + pos); |
| 5306 | o_addchr(as_string, '`'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5307 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5308 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5309 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5310 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5311 | break; |
| 5312 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5313 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5314 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5315 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5316 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5317 | ch = i_peek(input); |
| 5318 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 5319 | ch = '_'; |
| 5320 | goto make_var; |
| 5321 | } |
| 5322 | /* else: it's $_ */ |
| 5323 | /* TODO: */ |
| 5324 | /* $_ Shell or shell script name; or last cmd name */ |
| 5325 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 5326 | default: |
| 5327 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5328 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5329 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5330 | return 0; |
| 5331 | } |
| 5332 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5333 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5334 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5335 | parse_stream_dquoted(dest, input, dquote_end) |
| 5336 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5337 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5338 | o_string *dest, |
| 5339 | struct in_str *input, |
| 5340 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5341 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5342 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5343 | int next; |
| 5344 | |
| 5345 | again: |
| 5346 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5347 | if (ch != EOF) |
| 5348 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5349 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5350 | if (dest->o_assignment == NOT_ASSIGNMENT) |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5351 | dest->o_escape ^= 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5352 | debug_printf_parse("parse_stream_dquoted return 0\n"); |
| 5353 | return 0; |
| 5354 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5355 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5356 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5357 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5358 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5359 | } |
| 5360 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5361 | if (ch != '\n') { |
| 5362 | next = i_peek(input); |
| 5363 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5364 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 5365 | ch, ch, dest->o_escape); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5366 | if (ch == '\\') { |
| 5367 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5368 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5369 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5370 | } |
| 5371 | /* bash: |
| 5372 | * "The backslash retains its special meaning [in "..."] |
| 5373 | * only when followed by one of the following characters: |
| 5374 | * $, `, ", \, or <newline>. A double quote may be quoted |
| 5375 | * within double quotes by preceding it with a backslash. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5376 | */ |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 5377 | if (strchr("$`\"\\\n", next) != NULL) { |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 5378 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 5379 | if (ch != '\n') { |
| 5380 | o_addqchr(dest, ch); |
| 5381 | nommu_addchr(as_string, ch); |
| 5382 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5383 | } else { |
| 5384 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 5385 | nommu_addchr(as_string, '\\'); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5386 | } |
| 5387 | goto again; |
| 5388 | } |
| 5389 | if (ch == '$') { |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5390 | if (handle_dollar(as_string, dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5391 | debug_printf_parse("parse_stream_dquoted return 1: " |
| 5392 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5393 | return 1; |
| 5394 | } |
| 5395 | goto again; |
| 5396 | } |
| 5397 | #if ENABLE_HUSH_TICK |
| 5398 | if (ch == '`') { |
| 5399 | //int pos = dest->length; |
| 5400 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5401 | o_addchr(dest, 0x80 | '`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5402 | add_till_backquote(dest, input); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5403 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5404 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5405 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5406 | } |
| 5407 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5408 | o_addQchr(dest, ch); |
| 5409 | if (ch == '=' |
| 5410 | && (dest->o_assignment == MAYBE_ASSIGNMENT |
| 5411 | || dest->o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5412 | && is_well_formed_var_name(dest->data, '=') |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5413 | ) { |
| 5414 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 5415 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5416 | goto again; |
| 5417 | } |
| 5418 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5419 | /* |
| 5420 | * Scan input until EOF or end_trigger char. |
| 5421 | * Return a list of pipes to execute, or NULL on EOF |
| 5422 | * or if end_trigger character is met. |
| 5423 | * On syntax error, exit is shell is not interactive, |
| 5424 | * reset parsing machinery and start parsing anew, |
| 5425 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5426 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5427 | static struct pipe *parse_stream(char **pstring, |
| 5428 | struct in_str *input, |
| 5429 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5430 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5431 | struct parse_context ctx; |
| 5432 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5433 | int is_in_dquote; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5434 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5435 | |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5436 | /* Double-quote state is handled in the state variable is_in_dquote. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5437 | * 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] | 5438 | * found. When recursing, quote state is passed in via dest->o_escape. |
| 5439 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5440 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
| 5441 | end_trigger ? : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5442 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5443 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5444 | G.ifs = get_local_var_value("IFS"); |
| 5445 | if (G.ifs == NULL) |
| 5446 | G.ifs = " \t\n"; |
| 5447 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5448 | reset: |
| 5449 | #if ENABLE_HUSH_INTERACTIVE |
| 5450 | input->promptmode = 0; /* PS1 */ |
| 5451 | #endif |
| 5452 | /* dest.o_assignment = MAYBE_ASSIGNMENT; - already is */ |
| 5453 | initialize_context(&ctx); |
| 5454 | is_in_dquote = 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5455 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5456 | while (1) { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5457 | const char *is_ifs; |
| 5458 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5459 | int ch; |
| 5460 | int next; |
| 5461 | int redir_fd; |
| 5462 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5463 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5464 | if (is_in_dquote) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5465 | /* dest.o_quoted = 1; - already is (see below) */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5466 | if (parse_stream_dquoted(&ctx.as_string, &dest, input, '"')) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5467 | goto parse_error; |
| 5468 | } |
| 5469 | /* We reached closing '"' */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5470 | is_in_dquote = 0; |
| 5471 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5472 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5473 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 5474 | ch, ch, dest.o_escape); |
| 5475 | if (ch == EOF) { |
| 5476 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5477 | |
| 5478 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5479 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5480 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5481 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5482 | /* end_trigger == '}' case errors out earlier, |
| 5483 | * checking only ')' */ |
| 5484 | if (end_trigger == ')') { |
| 5485 | syntax_error_unterm_ch('('); /* exits */ |
| 5486 | /* goto parse_error; */ |
| 5487 | } |
| 5488 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5489 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5490 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5491 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5492 | o_free(&dest); |
| 5493 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5494 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5495 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5496 | /* (this makes bare "&" cmd a no-op. |
| 5497 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5498 | if (pi->num_cmds == 0 |
| 5499 | IF_HAS_KEYWORDS( && pi->res_word == RES_NONE) |
| 5500 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5501 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5502 | pi = NULL; |
| 5503 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5504 | #if !BB_MMU |
| 5505 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 5506 | if (pstring) |
| 5507 | *pstring = ctx.as_string.data; |
| 5508 | else |
| 5509 | o_free_unsafe(&ctx.as_string); |
| 5510 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5511 | debug_leave(); |
| 5512 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5513 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5514 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5515 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5516 | is_ifs = strchr(G.ifs, ch); |
| 5517 | is_special = strchr("<>;&|(){}#'" /* special outside of "str" */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 5518 | "\\$\"" IF_HUSH_TICK("`") /* always special */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5519 | , ch); |
| 5520 | |
| 5521 | if (!is_special && !is_ifs) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5522 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5523 | o_addQchr(&dest, ch); |
| 5524 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 5525 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5526 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5527 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5528 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5529 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5530 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5531 | continue; |
| 5532 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5533 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5534 | if (is_ifs) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5535 | if (done_word(&dest, &ctx)) { |
| 5536 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 5537 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5538 | if (ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5539 | #if ENABLE_HUSH_CASE |
| 5540 | /* "case ... in <newline> word) ..." - |
| 5541 | * newlines are ignored (but ';' wouldn't be) */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5542 | if (ctx.command->argv == NULL |
| 5543 | && ctx.ctx_res_w == RES_MATCH |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5544 | ) { |
| 5545 | continue; |
| 5546 | } |
| 5547 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5548 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5549 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5550 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 5551 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5552 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5553 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5554 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5555 | heredoc_cnt = 0; |
| 5556 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5557 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5558 | ch = ';'; |
Denis Vlasenko | 7c98612 | 2009-04-04 12:15:42 +0000 | [diff] [blame] | 5559 | /* note: if (is_ifs) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5560 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5561 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5562 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5563 | |
| 5564 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 5565 | * } is an ordinary char in this case, even inside { cmd; } |
| 5566 | * Pathological example: { ""}; } should exec "}" cmd |
| 5567 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5568 | if (ch == '}') { |
| 5569 | if (!IS_NULL_CMD(ctx.command) /* cmd } */ |
| 5570 | || dest.length != 0 /* word} */ |
| 5571 | || dest.o_quoted /* ""} */ |
| 5572 | ) { |
| 5573 | goto ordinary_char; |
| 5574 | } |
| 5575 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
| 5576 | goto skip_end_trigger; |
| 5577 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5578 | } |
| 5579 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5580 | if (end_trigger && end_trigger == ch |
| 5581 | && (heredoc_cnt == 0 || end_trigger != ';') |
| 5582 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5583 | if (heredoc_cnt) { |
| 5584 | /* This is technically valid: |
| 5585 | * { cat <<HERE; }; echo Ok |
| 5586 | * heredoc |
| 5587 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5588 | * HERE |
| 5589 | * but we don't support this. |
| 5590 | * We require heredoc to be in enclosing {}/(), |
| 5591 | * if any. |
| 5592 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5593 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5594 | goto parse_error; |
| 5595 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5596 | if (done_word(&dest, &ctx)) { |
| 5597 | goto parse_error; |
| 5598 | } |
| 5599 | done_pipe(&ctx, PIPE_SEQ); |
| 5600 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5601 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5602 | if (!HAS_KEYWORDS |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5603 | 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] | 5604 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5605 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5606 | #if !BB_MMU |
| 5607 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 5608 | if (pstring) |
| 5609 | *pstring = ctx.as_string.data; |
| 5610 | else |
| 5611 | o_free_unsafe(&ctx.as_string); |
| 5612 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5613 | debug_leave(); |
| 5614 | debug_printf_parse("parse_stream return %p: " |
| 5615 | "end_trigger char found\n", |
| 5616 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5617 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5618 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5619 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5620 | skip_end_trigger: |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5621 | if (is_ifs) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5622 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5623 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5624 | next = '\0'; |
| 5625 | if (ch != '\n') { |
| 5626 | next = i_peek(input); |
| 5627 | } |
| 5628 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5629 | /* Catch <, > before deciding whether this word is |
| 5630 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 5631 | switch (ch) { |
| 5632 | case '>': |
| 5633 | redir_fd = redirect_opt_num(&dest); |
| 5634 | if (done_word(&dest, &ctx)) { |
| 5635 | goto parse_error; |
| 5636 | } |
| 5637 | redir_style = REDIRECT_OVERWRITE; |
| 5638 | if (next == '>') { |
| 5639 | redir_style = REDIRECT_APPEND; |
| 5640 | ch = i_getch(input); |
| 5641 | nommu_addchr(&ctx.as_string, ch); |
| 5642 | } |
| 5643 | #if 0 |
| 5644 | else if (next == '(') { |
| 5645 | syntax_error(">(process) not supported"); |
| 5646 | goto parse_error; |
| 5647 | } |
| 5648 | #endif |
| 5649 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5650 | goto parse_error; |
| 5651 | continue; /* back to top of while (1) */ |
| 5652 | case '<': |
| 5653 | redir_fd = redirect_opt_num(&dest); |
| 5654 | if (done_word(&dest, &ctx)) { |
| 5655 | goto parse_error; |
| 5656 | } |
| 5657 | redir_style = REDIRECT_INPUT; |
| 5658 | if (next == '<') { |
| 5659 | redir_style = REDIRECT_HEREDOC; |
| 5660 | heredoc_cnt++; |
| 5661 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 5662 | ch = i_getch(input); |
| 5663 | nommu_addchr(&ctx.as_string, ch); |
| 5664 | } else if (next == '>') { |
| 5665 | redir_style = REDIRECT_IO; |
| 5666 | ch = i_getch(input); |
| 5667 | nommu_addchr(&ctx.as_string, ch); |
| 5668 | } |
| 5669 | #if 0 |
| 5670 | else if (next == '(') { |
| 5671 | syntax_error("<(process) not supported"); |
| 5672 | goto parse_error; |
| 5673 | } |
| 5674 | #endif |
| 5675 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5676 | goto parse_error; |
| 5677 | continue; /* back to top of while (1) */ |
| 5678 | } |
| 5679 | |
| 5680 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 5681 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 5682 | && !ctx.pending_redirect |
| 5683 | ) { |
| 5684 | /* ch is a special char and thus this word |
| 5685 | * cannot be an assignment */ |
| 5686 | dest.o_assignment = NOT_ASSIGNMENT; |
| 5687 | } |
| 5688 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5689 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5690 | case '#': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5691 | if (dest.length == 0) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5692 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5693 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5694 | if (ch == EOF || ch == '\n') |
| 5695 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5696 | i_getch(input); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5697 | /* note: we do not add it to &ctx.as_string */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5698 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5699 | nommu_addchr(&ctx.as_string, '\n'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5700 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5701 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5702 | } |
| 5703 | break; |
| 5704 | case '\\': |
| 5705 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5706 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5707 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5708 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5709 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 5710 | if (ch != '\n') { |
| 5711 | o_addchr(&dest, '\\'); |
| 5712 | nommu_addchr(&ctx.as_string, '\\'); |
| 5713 | o_addchr(&dest, ch); |
| 5714 | nommu_addchr(&ctx.as_string, ch); |
| 5715 | /* Example: echo Hello \2>file |
| 5716 | * we need to know that word 2 is quoted */ |
| 5717 | dest.o_quoted = 1; |
| 5718 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5719 | break; |
| 5720 | case '$': |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5721 | if (handle_dollar(&ctx.as_string, &dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5722 | debug_printf_parse("parse_stream parse error: " |
| 5723 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5724 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5725 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5726 | break; |
| 5727 | case '\'': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5728 | dest.o_quoted = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5729 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5730 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5731 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5732 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5733 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5734 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5735 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5736 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5737 | break; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5738 | if (dest.o_assignment == NOT_ASSIGNMENT) |
| 5739 | o_addqchr(&dest, ch); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5740 | else |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5741 | o_addchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5742 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5743 | break; |
| 5744 | case '"': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5745 | dest.o_quoted = 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5746 | is_in_dquote ^= 1; /* invert */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5747 | if (dest.o_assignment == NOT_ASSIGNMENT) |
| 5748 | dest.o_escape ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5749 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5750 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5751 | case '`': { |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5752 | #if !BB_MMU |
| 5753 | int pos; |
| 5754 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5755 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5756 | o_addchr(&dest, '`'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5757 | #if !BB_MMU |
| 5758 | pos = dest.length; |
| 5759 | #endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5760 | add_till_backquote(&dest, input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5761 | #if !BB_MMU |
| 5762 | o_addstr(&ctx.as_string, dest.data + pos); |
| 5763 | o_addchr(&ctx.as_string, '`'); |
| 5764 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5765 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5766 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5767 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5768 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5769 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5770 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5771 | #if ENABLE_HUSH_CASE |
| 5772 | case_semi: |
| 5773 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5774 | if (done_word(&dest, &ctx)) { |
| 5775 | goto parse_error; |
| 5776 | } |
| 5777 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5778 | #if ENABLE_HUSH_CASE |
| 5779 | /* Eat multiple semicolons, detect |
| 5780 | * whether it means something special */ |
| 5781 | while (1) { |
| 5782 | ch = i_peek(input); |
| 5783 | if (ch != ';') |
| 5784 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5785 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5786 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5787 | if (ctx.ctx_res_w == RES_CASEI) { |
| 5788 | ctx.ctx_dsemicolon = 1; |
| 5789 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5790 | break; |
| 5791 | } |
| 5792 | } |
| 5793 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5794 | new_cmd: |
| 5795 | /* We just finished a cmd. New one may start |
| 5796 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5797 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5798 | break; |
| 5799 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5800 | if (done_word(&dest, &ctx)) { |
| 5801 | goto parse_error; |
| 5802 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5803 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5804 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5805 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5806 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5807 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5808 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5809 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5810 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5811 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5812 | if (done_word(&dest, &ctx)) { |
| 5813 | goto parse_error; |
| 5814 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5815 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5816 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5817 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5818 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5819 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5820 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5821 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5822 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5823 | } else { |
| 5824 | /* we could pick up a file descriptor choice here |
| 5825 | * with redirect_opt_num(), but bash doesn't do it. |
| 5826 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5827 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5828 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5829 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5830 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5831 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5832 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5833 | if (ctx.ctx_res_w == RES_MATCH |
| 5834 | && ctx.command->argv == NULL /* not (word|(... */ |
| 5835 | && dest.length == 0 /* not word(... */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5836 | && dest.o_quoted == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5837 | ) { |
| 5838 | continue; |
| 5839 | } |
| 5840 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5841 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5842 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 5843 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5844 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5845 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5846 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5847 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5848 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5849 | goto case_semi; |
| 5850 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5851 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5852 | /* proper use of this character is caught by end_trigger: |
| 5853 | * if we see {, we call parse_group(..., end_trigger='}') |
| 5854 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5855 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5856 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5857 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5858 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 5859 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5860 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5861 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5862 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5863 | parse_error: |
| 5864 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5865 | struct parse_context *pctx; |
| 5866 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5867 | |
| 5868 | /* Clean up allocated tree. |
| 5869 | * Samples for finding leaks on syntax error recovery path. |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5870 | * Run them from interactive shell, watch pmap `pidof hush`. |
| 5871 | * while if false; then false; fi do break; done |
| 5872 | * (bash accepts it) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5873 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 5874 | * Samples to catch leaks at execution: |
| 5875 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 5876 | * 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] | 5877 | */ |
| 5878 | pctx = &ctx; |
| 5879 | do { |
| 5880 | /* Update pipe/command counts, |
| 5881 | * otherwise freeing may miss some */ |
| 5882 | done_pipe(pctx, PIPE_SEQ); |
| 5883 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 5884 | pctx->list_head, pctx); |
| 5885 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5886 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5887 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5888 | #if !BB_MMU |
| 5889 | o_free_unsafe(&pctx->as_string); |
| 5890 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5891 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5892 | if (pctx != &ctx) { |
| 5893 | free(pctx); |
| 5894 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5895 | IF_HAS_KEYWORDS(pctx = p2;) |
| 5896 | } while (HAS_KEYWORDS && pctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5897 | /* Free text, clear all dest fields */ |
| 5898 | o_free(&dest); |
| 5899 | /* If we are not in top-level parse, we return, |
| 5900 | * our caller will propagate error. |
| 5901 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5902 | if (end_trigger != ';') { |
| 5903 | #if !BB_MMU |
| 5904 | if (pstring) |
| 5905 | *pstring = NULL; |
| 5906 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5907 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5908 | return ERR_PTR; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5909 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5910 | /* Discard cached input, force prompt */ |
| 5911 | input->p = NULL; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 5912 | IF_HUSH_INTERACTIVE(input->promptme = 1;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5913 | goto reset; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5914 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5915 | } |
| 5916 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5917 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5918 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 5919 | * end_trigger controls how often we stop parsing |
| 5920 | * NUL: parse all, execute, return |
| 5921 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 5922 | */ |
| 5923 | 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] | 5924 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5925 | while (1) { |
| 5926 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5927 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5928 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5929 | if (!pipe_list) /* EOF */ |
| 5930 | break; |
| 5931 | debug_print_tree(pipe_list, 0); |
| 5932 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 5933 | run_and_free_list(pipe_list); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5934 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5935 | } |
| 5936 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5937 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5938 | { |
| 5939 | struct in_str input; |
| 5940 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5941 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5942 | } |
| 5943 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5944 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5945 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5946 | struct in_str input; |
| 5947 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5948 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5949 | } |
| 5950 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5951 | /* Called a few times only (or even once if "sh -c") */ |
| 5952 | static void block_signals(int second_time) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 5953 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5954 | unsigned sig; |
| 5955 | unsigned mask; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 5956 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5957 | mask = (1 << SIGQUIT); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 5958 | if (G_interactive_fd) { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 5959 | mask = (1 << SIGQUIT) | SPECIAL_INTERACTIVE_SIGS; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 5960 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 5961 | mask |= SPECIAL_JOB_SIGS; |
| 5962 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5963 | G.non_DFL_mask = mask; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 5964 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5965 | if (!second_time) |
| 5966 | sigprocmask(SIG_SETMASK, NULL, &G.blocked_set); |
| 5967 | sig = 0; |
| 5968 | while (mask) { |
| 5969 | if (mask & 1) |
| 5970 | sigaddset(&G.blocked_set, sig); |
| 5971 | mask >>= 1; |
| 5972 | sig++; |
| 5973 | } |
| 5974 | sigdelset(&G.blocked_set, SIGCHLD); |
| 5975 | |
| 5976 | sigprocmask(SIG_SETMASK, &G.blocked_set, |
| 5977 | second_time ? NULL : &G.inherited_set); |
| 5978 | /* POSIX allows shell to re-enable SIGCHLD |
| 5979 | * even if it was SIG_IGN on entry */ |
| 5980 | // G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 5981 | if (!second_time) |
| 5982 | signal(SIGCHLD, SIG_DFL); // SIGCHLD_handler); |
| 5983 | } |
| 5984 | |
| 5985 | #if ENABLE_HUSH_JOB |
| 5986 | /* helper */ |
| 5987 | static void maybe_set_to_sigexit(int sig) |
| 5988 | { |
| 5989 | void (*handler)(int); |
| 5990 | /* non_DFL_mask'ed signals are, well, masked, |
| 5991 | * no need to set handler for them. |
| 5992 | */ |
| 5993 | if (!((G.non_DFL_mask >> sig) & 1)) { |
| 5994 | handler = signal(sig, sigexit); |
| 5995 | if (handler == SIG_IGN) /* oops... restore back to IGN! */ |
| 5996 | signal(sig, handler); |
| 5997 | } |
| 5998 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 5999 | /* Set handlers to restore tty pgrp and exit */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6000 | static void set_fatal_handlers(void) |
| 6001 | { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 6002 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6003 | if (HUSH_DEBUG) { |
| 6004 | maybe_set_to_sigexit(SIGILL ); |
| 6005 | maybe_set_to_sigexit(SIGFPE ); |
| 6006 | maybe_set_to_sigexit(SIGBUS ); |
| 6007 | maybe_set_to_sigexit(SIGSEGV); |
| 6008 | maybe_set_to_sigexit(SIGTRAP); |
| 6009 | } /* else: hush is perfect. what SEGV? */ |
| 6010 | maybe_set_to_sigexit(SIGABRT); |
| 6011 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 6012 | maybe_set_to_sigexit(SIGPIPE); |
| 6013 | maybe_set_to_sigexit(SIGALRM); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 6014 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are masked. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6015 | * if we aren't interactive... but in this case |
| 6016 | * we never want to restore pgrp on exit, and this fn is not called */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 6017 | /*maybe_set_to_sigexit(SIGHUP );*/ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6018 | /*maybe_set_to_sigexit(SIGTERM);*/ |
| 6019 | /*maybe_set_to_sigexit(SIGINT );*/ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 6020 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 6021 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 6022 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6023 | static int set_mode(const char cstate, const char mode) |
| 6024 | { |
| 6025 | int state = (cstate == '-' ? 1 : 0); |
| 6026 | switch (mode) { |
| 6027 | case 'n': G.fake_mode = state; break; |
| 6028 | case 'x': /*G.debug_mode = state;*/ break; |
| 6029 | default: return EXIT_FAILURE; |
| 6030 | } |
| 6031 | return EXIT_SUCCESS; |
| 6032 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6033 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 6034 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 6035 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6036 | { |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6037 | static const struct variable const_shell_ver = { |
| 6038 | .next = NULL, |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 6039 | .varstr = (char*)hush_version_str, |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6040 | .max_len = 1, /* 0 can provoke free(name) */ |
| 6041 | .flg_export = 1, |
| 6042 | .flg_read_only = 1, |
| 6043 | }; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6044 | int signal_mask_is_inited = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6045 | int opt; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6046 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6047 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 6048 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 6049 | INIT_G(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6050 | if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6051 | G.last_exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6052 | #if !BB_MMU |
| 6053 | G.argv0_for_re_execing = argv[0]; |
| 6054 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6055 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6056 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 6057 | G.top_var = &G.shell_ver; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6058 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6059 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 6060 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6061 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6062 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6063 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6064 | if (e) while (*e) { |
| 6065 | char *value = strchr(*e, '='); |
| 6066 | if (value) { /* paranoia */ |
| 6067 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 6068 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 6069 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6070 | cur_var->max_len = strlen(*e); |
| 6071 | cur_var->flg_export = 1; |
| 6072 | } |
| 6073 | e++; |
| 6074 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6075 | debug_printf_env("putenv '%s'\n", hush_version_str); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 6076 | putenv((char *)hush_version_str); /* reinstate HUSH_VERSION */ |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 6077 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6078 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 6079 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6080 | G.global_argc = argc; |
| 6081 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 6082 | /* Initialize some more globals to non-zero values */ |
| 6083 | set_cwd(); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 6084 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 6085 | |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 6086 | if (setjmp(die_jmp)) { |
| 6087 | /* xfunc has failed! die die die */ |
| 6088 | /* no EXIT traps, this is an escape hatch! */ |
| 6089 | G.exiting = 1; |
| 6090 | hush_exit(xfunc_error_retval); |
| 6091 | } |
| 6092 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6093 | /* Shell is non-interactive at first. We need to call |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6094 | * block_signals(0) if we are going to execute "sh <script>", |
| 6095 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6096 | * If we later decide that we are interactive, we run block_signals(0) |
| 6097 | * (or re-run block_signals(1) if we ran block_signals(0) before) |
| 6098 | * in order to intercept (more) signals. |
| 6099 | */ |
| 6100 | |
| 6101 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 6102 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6103 | while (1) { |
| 6104 | opt = getopt(argc, argv, "c:xins" |
| 6105 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 6106 | "<:$:R:V:" |
| 6107 | # if ENABLE_HUSH_FUNCTIONS |
| 6108 | "F:" |
| 6109 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6110 | #endif |
| 6111 | ); |
| 6112 | if (opt <= 0) |
| 6113 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6114 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6115 | case 'c': |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6116 | if (!G.root_pid) |
| 6117 | G.root_pid = getpid(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6118 | G.global_argv = argv + optind; |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 6119 | if (!argv[optind]) { |
| 6120 | /* -c 'script' (no params): prevent empty $0 */ |
| 6121 | *--G.global_argv = argv[0]; |
| 6122 | optind--; |
| 6123 | } /* else -c 'script' PAR0 PAR1: $0 is PAR0 */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6124 | G.global_argc = argc - optind; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6125 | block_signals(0); /* 0: called 1st time */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6126 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6127 | goto final_return; |
| 6128 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 6129 | /* Well, we cannot just declare interactiveness, |
| 6130 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6131 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6132 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 6133 | case 's': |
| 6134 | /* "-s" means "read from stdin", but this is how we always |
| 6135 | * operate, so simply do nothing here. */ |
| 6136 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6137 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 6138 | case '<': /* "big heredoc" support */ |
| 6139 | full_write(STDOUT_FILENO, optarg, strlen(optarg)); |
| 6140 | _exit(0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6141 | case '$': |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6142 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 6143 | optarg++; |
| 6144 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 6145 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6146 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6147 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6148 | optarg++; |
| 6149 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6150 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6151 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6152 | case 'R': |
| 6153 | case 'V': |
| 6154 | set_local_var(xstrdup(optarg), 0, opt == 'R'); |
| 6155 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 6156 | # if ENABLE_HUSH_FUNCTIONS |
| 6157 | case 'F': { |
| 6158 | struct function *funcp = new_function(optarg); |
| 6159 | /* funcp->name is already set to optarg */ |
| 6160 | /* funcp->body is set to NULL. It's a special case. */ |
| 6161 | funcp->body_as_string = argv[optind]; |
| 6162 | optind++; |
| 6163 | break; |
| 6164 | } |
| 6165 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6166 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6167 | case 'n': |
| 6168 | case 'x': |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6169 | if (!set_mode('-', opt)) |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6170 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6171 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6172 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6173 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 6174 | " or: sh -c command [args]...\n\n"); |
| 6175 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6176 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6177 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6178 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6179 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6180 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6181 | |
| 6182 | if (!G.root_pid) |
| 6183 | G.root_pid = getpid(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6184 | |
| 6185 | /* If we are login shell... */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6186 | if (argv[0] && argv[0][0] == '-') { |
| 6187 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6188 | debug_printf("sourcing /etc/profile\n"); |
| 6189 | input = fopen_for_read("/etc/profile"); |
| 6190 | if (input != NULL) { |
| 6191 | close_on_exec_on(fileno(input)); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6192 | block_signals(0); /* 0: called 1st time */ |
| 6193 | signal_mask_is_inited = 1; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6194 | parse_and_run_file(input); |
| 6195 | fclose(input); |
| 6196 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6197 | /* bash: after sourcing /etc/profile, |
| 6198 | * tries to source (in the given order): |
| 6199 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
| 6200 | * stopping of first found. --noprofile turns this off. |
| 6201 | * bash also sources ~/.bash_logout on exit. |
| 6202 | * If called as sh, skips .bash_XXX files. |
| 6203 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6204 | } |
| 6205 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6206 | if (argv[optind]) { |
| 6207 | FILE *input; |
| 6208 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6209 | * "bash <script>" (which is never interactive (unless -i?)) |
| 6210 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6211 | * If called as sh, does the same but with $ENV. |
| 6212 | */ |
| 6213 | debug_printf("running script '%s'\n", argv[optind]); |
| 6214 | G.global_argv = argv + optind; |
| 6215 | G.global_argc = argc - optind; |
| 6216 | input = xfopen_for_read(argv[optind]); |
| 6217 | close_on_exec_on(fileno(input)); |
| 6218 | if (!signal_mask_is_inited) |
| 6219 | block_signals(0); /* 0: called 1st time */ |
| 6220 | parse_and_run_file(input); |
| 6221 | #if ENABLE_FEATURE_CLEAN_UP |
| 6222 | fclose(input); |
| 6223 | #endif |
| 6224 | goto final_return; |
| 6225 | } |
| 6226 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6227 | /* Up to here, shell was non-interactive. Now it may become one. |
| 6228 | * NB: don't forget to (re)run block_signals(0/1) as needed. |
| 6229 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6230 | |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6231 | /* 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] | 6232 | * the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 6233 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6234 | * no arguments remaining or the -s flag given |
| 6235 | * standard input is a terminal |
| 6236 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6237 | * Refer to Posix.2, the description of the 'sh' utility. |
| 6238 | */ |
| 6239 | #if ENABLE_HUSH_JOB |
| 6240 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6241 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 6242 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 6243 | if (G_saved_tty_pgrp < 0) |
| 6244 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6245 | |
| 6246 | /* try to dup stdin to high fd#, >= 255 */ |
| 6247 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 6248 | if (G_interactive_fd < 0) { |
| 6249 | /* try to dup to any fd */ |
| 6250 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6251 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6252 | /* give up */ |
| 6253 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6254 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 6255 | } |
| 6256 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6257 | // TODO: track & disallow any attempts of user |
| 6258 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6259 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6260 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6261 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6262 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6263 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6264 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6265 | /* If we were run as 'hush &', sleep until we are |
| 6266 | * in the foreground (tty pgrp == our pgrp). |
| 6267 | * If we get started under a job aware app (like bash), |
| 6268 | * make sure we are now in charge so we don't fight over |
| 6269 | * who gets the foreground */ |
| 6270 | while (1) { |
| 6271 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6272 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 6273 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6274 | break; |
| 6275 | /* send TTIN to ourself (should stop us) */ |
| 6276 | kill(- shell_pgrp, SIGTTIN); |
| 6277 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6278 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6279 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6280 | /* Block some signals */ |
| 6281 | block_signals(signal_mask_is_inited); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6282 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6283 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6284 | /* Set other signals to restore saved_tty_pgrp */ |
| 6285 | set_fatal_handlers(); |
| 6286 | /* Put ourselves in our own process group |
| 6287 | * (bash, too, does this only if ctty is available) */ |
| 6288 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 6289 | /* Grab control of the terminal */ |
| 6290 | tcsetpgrp(G_interactive_fd, getpid()); |
| 6291 | } |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 6292 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 6293 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6294 | enable_restore_tty_pgrp_on_exit(); /* sets die_sleep = -1 */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6295 | } else if (!signal_mask_is_inited) { |
| 6296 | block_signals(0); /* 0: called 1st time */ |
| 6297 | } /* else: block_signals(0) was done before */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6298 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6299 | /* No job control compiled in, only prompt/line editing */ |
| 6300 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6301 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 6302 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6303 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6304 | G_interactive_fd = dup(STDIN_FILENO); |
| 6305 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6306 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6307 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6308 | } |
| 6309 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6310 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6311 | close_on_exec_on(G_interactive_fd); |
| 6312 | block_signals(signal_mask_is_inited); |
| 6313 | } else if (!signal_mask_is_inited) { |
| 6314 | block_signals(0); |
| 6315 | } |
| 6316 | #else |
| 6317 | /* We have interactiveness code disabled */ |
| 6318 | if (!signal_mask_is_inited) { |
| 6319 | block_signals(0); |
| 6320 | } |
| 6321 | #endif |
| 6322 | /* bash: |
| 6323 | * if interactive but not a login shell, sources ~/.bashrc |
| 6324 | * (--norc turns this off, --rcfile <file> overrides) |
| 6325 | */ |
| 6326 | |
| 6327 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Mike Frysinger | 258275d | 2009-04-05 21:19:43 +0000 | [diff] [blame] | 6328 | printf("\n\n%s hush - the humble shell\n", bb_banner); |
Mike Frysinger | 26cf283 | 2009-04-24 06:40:30 +0000 | [diff] [blame] | 6329 | if (ENABLE_HUSH_HELP) |
| 6330 | puts("Enter 'help' for a list of built-in commands."); |
| 6331 | puts(""); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 6332 | } |
| 6333 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6334 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6335 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6336 | final_return: |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 6337 | #if ENABLE_FEATURE_CLEAN_UP |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6338 | if (G.cwd != bb_msg_unknown) |
| 6339 | free((char*)G.cwd); |
| 6340 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6341 | while (cur_var) { |
| 6342 | struct variable *tmp = cur_var; |
| 6343 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 6344 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6345 | cur_var = cur_var->next; |
| 6346 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 6347 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6348 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6349 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6350 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 6351 | |
| 6352 | |
| 6353 | #if ENABLE_LASH |
| 6354 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 6355 | int lash_main(int argc, char **argv) |
| 6356 | { |
| 6357 | //bb_error_msg("lash is deprecated, please use hush instead"); |
| 6358 | return hush_main(argc, argv); |
| 6359 | } |
| 6360 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6361 | |
| 6362 | |
| 6363 | /* |
| 6364 | * Built-ins |
| 6365 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6366 | static int builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6367 | { |
| 6368 | return 0; |
| 6369 | } |
| 6370 | |
| 6371 | static int builtin_test(char **argv) |
| 6372 | { |
| 6373 | int argc = 0; |
| 6374 | while (*argv) { |
| 6375 | argc++; |
| 6376 | argv++; |
| 6377 | } |
| 6378 | return test_main(argc, argv - argc); |
| 6379 | } |
| 6380 | |
| 6381 | static int builtin_echo(char **argv) |
| 6382 | { |
| 6383 | int argc = 0; |
| 6384 | while (*argv) { |
| 6385 | argc++; |
| 6386 | argv++; |
| 6387 | } |
| 6388 | return echo_main(argc, argv - argc); |
| 6389 | } |
| 6390 | |
| 6391 | static int builtin_eval(char **argv) |
| 6392 | { |
| 6393 | int rcode = EXIT_SUCCESS; |
| 6394 | |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6395 | if (*++argv) { |
| 6396 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6397 | /* bash: |
| 6398 | * eval "echo Hi; done" ("done" is syntax error): |
| 6399 | * "echo Hi" will not execute too. |
| 6400 | */ |
| 6401 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6402 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6403 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6404 | } |
| 6405 | return rcode; |
| 6406 | } |
| 6407 | |
| 6408 | static int builtin_cd(char **argv) |
| 6409 | { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6410 | const char *newdir = argv[1]; |
| 6411 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6412 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6413 | * bash says "bash: cd: HOME not set" and does nothing |
| 6414 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6415 | */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 6416 | newdir = get_local_var_value("HOME") ? : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6417 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6418 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6419 | /* Mimic bash message exactly */ |
| 6420 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6421 | return EXIT_FAILURE; |
| 6422 | } |
| 6423 | set_cwd(); |
| 6424 | return EXIT_SUCCESS; |
| 6425 | } |
| 6426 | |
| 6427 | static int builtin_exec(char **argv) |
| 6428 | { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6429 | if (*++argv == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6430 | return EXIT_SUCCESS; /* bash does this */ |
| 6431 | { |
| 6432 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6433 | nommu_save_t dummy; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6434 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 6435 | /* TODO: if exec fails, bash does NOT exit! We do... */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6436 | pseudo_exec_argv(&dummy, argv, 0, NULL); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6437 | /* never returns */ |
| 6438 | } |
| 6439 | } |
| 6440 | |
| 6441 | static int builtin_exit(char **argv) |
| 6442 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 6443 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 6444 | |
| 6445 | /* interactive bash: |
| 6446 | * # trap "echo EEE" EXIT |
| 6447 | * # exit |
| 6448 | * exit |
| 6449 | * There are stopped jobs. |
| 6450 | * (if there are _stopped_ jobs, running ones don't count) |
| 6451 | * # exit |
| 6452 | * exit |
| 6453 | # EEE (then bash exits) |
| 6454 | * |
| 6455 | * we can use G.exiting = -1 as indicator "last cmd was exit" |
| 6456 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 6457 | |
| 6458 | /* note: EXIT trap is run by hush_exit */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6459 | if (*++argv == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6460 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6461 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 6462 | xfunc_error_retval = 255; |
| 6463 | /* bash: exit -2 == exit 254, no error msg */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6464 | hush_exit(xatoi(*argv) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6465 | } |
| 6466 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6467 | static void print_escaped(const char *s) |
| 6468 | { |
| 6469 | do { |
| 6470 | if (*s != '\'') { |
| 6471 | const char *p; |
| 6472 | |
| 6473 | p = strchrnul(s, '\''); |
| 6474 | /* print 'xxxx', possibly just '' */ |
| 6475 | printf("'%.*s'", (int)(p - s), s); |
| 6476 | if (*p == '\0') |
| 6477 | break; |
| 6478 | s = p; |
| 6479 | } |
| 6480 | /* s points to '; print "'''...'''" */ |
| 6481 | putchar('"'); |
| 6482 | do putchar('\''); while (*++s == '\''); |
| 6483 | putchar('"'); |
| 6484 | } while (*s); |
| 6485 | } |
| 6486 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6487 | static int builtin_export(char **argv) |
| 6488 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 6489 | unsigned opt_unexport; |
| 6490 | |
| 6491 | if (argv[1] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6492 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6493 | if (e) { |
| 6494 | while (*e) { |
| 6495 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6496 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6497 | #else |
| 6498 | /* ash emits: export VAR='VAL' |
| 6499 | * bash: declare -x VAR="VAL" |
| 6500 | * we follow ash example */ |
| 6501 | const char *s = *e++; |
| 6502 | const char *p = strchr(s, '='); |
| 6503 | |
| 6504 | if (!p) /* wtf? take next variable */ |
| 6505 | continue; |
| 6506 | /* export var= */ |
| 6507 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6508 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6509 | putchar('\n'); |
| 6510 | #endif |
| 6511 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6512 | /*fflush(stdout); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6513 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6514 | return EXIT_SUCCESS; |
| 6515 | } |
| 6516 | |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 6517 | #if ENABLE_HUSH_EXPORT_N |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 6518 | /* "!": do not abort on errors */ |
| 6519 | /* "+": stop at 1st non-option */ |
| 6520 | opt_unexport = getopt32(argv, "!+n"); |
| 6521 | if (opt_unexport == (unsigned)-1) |
| 6522 | return EXIT_FAILURE; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 6523 | argv += optind; |
| 6524 | #else |
| 6525 | opt_unexport = 0; |
| 6526 | argv++; |
| 6527 | #endif |
| 6528 | |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6529 | do { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6530 | char *name = *argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6531 | |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 6532 | /* So far we do not check that name is valid (TODO?) */ |
| 6533 | |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 6534 | if (strchr(name, '=') == NULL) { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6535 | struct variable *var; |
| 6536 | |
| 6537 | var = get_local_var(name); |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 6538 | if (opt_unexport) { |
| 6539 | /* export -n NAME (without =VALUE) */ |
| 6540 | if (var) { |
| 6541 | var->flg_export = 0; |
| 6542 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 6543 | unsetenv(name); |
| 6544 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 6545 | continue; |
| 6546 | } |
| 6547 | /* export NAME (without =VALUE) */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6548 | if (var) { |
| 6549 | var->flg_export = 1; |
| 6550 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 6551 | putenv(var->varstr); |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 6552 | continue; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6553 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 6554 | /* Exporting non-existing variable. |
| 6555 | * bash does not put it in environment, |
| 6556 | * but remembers that it is exported, |
| 6557 | * and does put it in env when it is set later. |
| 6558 | * We just set it to "" and export. */ |
| 6559 | name = xasprintf("%s=", name); |
| 6560 | } else { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 6561 | /* (Un)exporting NAME=VALUE */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 6562 | name = xstrdup(name); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6563 | } |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 6564 | set_local_var(name, |
| 6565 | /*export:*/ (opt_unexport ? -1 : 1), |
| 6566 | /*readonly:*/ 0 |
| 6567 | ); |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6568 | } while (*++argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6569 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6570 | return EXIT_SUCCESS; |
| 6571 | } |
| 6572 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6573 | static int builtin_trap(char **argv) |
| 6574 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6575 | int sig; |
| 6576 | char *new_cmd; |
| 6577 | |
| 6578 | if (!G.traps) |
| 6579 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 6580 | |
| 6581 | argv++; |
| 6582 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 6583 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6584 | /* No args: print all trapped */ |
| 6585 | for (i = 0; i < NSIG; ++i) { |
| 6586 | if (G.traps[i]) { |
| 6587 | printf("trap -- "); |
| 6588 | print_escaped(G.traps[i]); |
| 6589 | printf(" %s\n", get_signame(i)); |
| 6590 | } |
| 6591 | } |
| 6592 | /*fflush(stdout); - done after each builtin anyway */ |
| 6593 | return EXIT_SUCCESS; |
| 6594 | } |
| 6595 | |
| 6596 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6597 | /* If first arg is a number: reset all specified signals */ |
| 6598 | sig = bb_strtou(*argv, NULL, 10); |
| 6599 | if (errno == 0) { |
| 6600 | int ret; |
| 6601 | process_sig_list: |
| 6602 | ret = EXIT_SUCCESS; |
| 6603 | while (*argv) { |
| 6604 | sig = get_signum(*argv++); |
| 6605 | if (sig < 0 || sig >= NSIG) { |
| 6606 | ret = EXIT_FAILURE; |
| 6607 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 6608 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6609 | continue; |
| 6610 | } |
| 6611 | |
| 6612 | free(G.traps[sig]); |
| 6613 | G.traps[sig] = xstrdup(new_cmd); |
| 6614 | |
| 6615 | debug_printf("trap: setting SIG%s (%i) to '%s'", |
| 6616 | get_signame(sig), sig, G.traps[sig]); |
| 6617 | |
| 6618 | /* There is no signal for 0 (EXIT) */ |
| 6619 | if (sig == 0) |
| 6620 | continue; |
| 6621 | |
| 6622 | if (new_cmd) { |
| 6623 | sigaddset(&G.blocked_set, sig); |
| 6624 | } else { |
| 6625 | /* There was a trap handler, we are removing it |
| 6626 | * (if sig has non-DFL handling, |
| 6627 | * we don't need to do anything) */ |
| 6628 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 6629 | continue; |
| 6630 | sigdelset(&G.blocked_set, sig); |
| 6631 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6632 | } |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 6633 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6634 | return ret; |
| 6635 | } |
| 6636 | |
| 6637 | if (!argv[1]) { /* no second arg */ |
| 6638 | bb_error_msg("trap: invalid arguments"); |
| 6639 | return EXIT_FAILURE; |
| 6640 | } |
| 6641 | |
| 6642 | /* First arg is "-": reset all specified to default */ |
| 6643 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 6644 | /* Everything else: set arg as signal handler |
| 6645 | * (includes "" case, which ignores signal) */ |
| 6646 | if (argv[0][0] == '-') { |
| 6647 | if (argv[0][1] == '\0') { /* "-" */ |
| 6648 | /* new_cmd remains NULL: "reset these sigs" */ |
| 6649 | goto reset_traps; |
| 6650 | } |
| 6651 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 6652 | argv++; |
| 6653 | } |
| 6654 | /* else: "-something", no special meaning */ |
| 6655 | } |
| 6656 | new_cmd = *argv; |
| 6657 | reset_traps: |
| 6658 | argv++; |
| 6659 | goto process_sig_list; |
| 6660 | } |
| 6661 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6662 | #if ENABLE_HUSH_JOB |
| 6663 | /* built-in 'fg' and 'bg' handler */ |
| 6664 | static int builtin_fg_bg(char **argv) |
| 6665 | { |
| 6666 | int i, jobnum; |
| 6667 | struct pipe *pi; |
| 6668 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6669 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6670 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6671 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6672 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 6673 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6674 | for (pi = G.job_list; pi; pi = pi->next) { |
| 6675 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6676 | goto found; |
| 6677 | } |
| 6678 | } |
| 6679 | bb_error_msg("%s: no current job", argv[0]); |
| 6680 | return EXIT_FAILURE; |
| 6681 | } |
| 6682 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 6683 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 6684 | return EXIT_FAILURE; |
| 6685 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6686 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6687 | if (pi->jobid == jobnum) { |
| 6688 | goto found; |
| 6689 | } |
| 6690 | } |
| 6691 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 6692 | return EXIT_FAILURE; |
| 6693 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 6694 | /* TODO: bash prints a string representation |
| 6695 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6696 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6697 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6698 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6699 | } |
| 6700 | |
| 6701 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 6702 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 6703 | for (i = 0; i < pi->num_cmds; i++) { |
| 6704 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 6705 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6706 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 6707 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6708 | |
| 6709 | i = kill(- pi->pgrp, SIGCONT); |
| 6710 | if (i < 0) { |
| 6711 | if (errno == ESRCH) { |
| 6712 | delete_finished_bg_job(pi); |
| 6713 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6714 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 6715 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6716 | } |
| 6717 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 6718 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6719 | remove_bg_job(pi); |
| 6720 | return checkjobs_and_fg_shell(pi); |
| 6721 | } |
| 6722 | return EXIT_SUCCESS; |
| 6723 | } |
| 6724 | #endif |
| 6725 | |
| 6726 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6727 | static int builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6728 | { |
| 6729 | const struct built_in_command *x; |
| 6730 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 6731 | printf("\n" |
| 6732 | "Built-in commands:\n" |
| 6733 | "------------------\n"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6734 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
| 6735 | printf("%s\t%s\n", x->cmd, x->descr); |
| 6736 | } |
| 6737 | printf("\n\n"); |
| 6738 | return EXIT_SUCCESS; |
| 6739 | } |
| 6740 | #endif |
| 6741 | |
| 6742 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6743 | static int builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6744 | { |
| 6745 | struct pipe *job; |
| 6746 | const char *status_string; |
| 6747 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6748 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 6749 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6750 | status_string = "Stopped"; |
| 6751 | else |
| 6752 | status_string = "Running"; |
| 6753 | |
| 6754 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 6755 | } |
| 6756 | return EXIT_SUCCESS; |
| 6757 | } |
| 6758 | #endif |
| 6759 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 6760 | #if HUSH_DEBUG |
| 6761 | static int builtin_memleak(char **argv UNUSED_PARAM) |
| 6762 | { |
| 6763 | void *p; |
| 6764 | unsigned long l; |
| 6765 | |
| 6766 | /* Crude attempt to find where "free memory" starts, |
| 6767 | * sans fragmentation. */ |
| 6768 | p = malloc(240); |
| 6769 | l = (unsigned long)p; |
| 6770 | free(p); |
| 6771 | p = malloc(3400); |
| 6772 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 6773 | free(p); |
| 6774 | |
| 6775 | if (!G.memleak_value) |
| 6776 | G.memleak_value = l; |
| 6777 | |
| 6778 | l -= G.memleak_value; |
| 6779 | if ((long)l < 0) |
| 6780 | l = 0; |
| 6781 | l /= 1024; |
| 6782 | if (l > 127) |
| 6783 | l = 127; |
| 6784 | |
| 6785 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 6786 | return l; |
| 6787 | } |
| 6788 | #endif |
| 6789 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6790 | static int builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6791 | { |
| 6792 | puts(set_cwd()); |
| 6793 | return EXIT_SUCCESS; |
| 6794 | } |
| 6795 | |
| 6796 | static int builtin_read(char **argv) |
| 6797 | { |
| 6798 | char *string; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6799 | const char *name = "REPLY"; |
| 6800 | |
| 6801 | if (argv[1]) { |
| 6802 | name = argv[1]; |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 6803 | /* bash (3.2.33(1)) bug: "read 0abcd" will execute, |
| 6804 | * and _after_ that_ it will complain */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6805 | if (!is_well_formed_var_name(name, '\0')) { |
| 6806 | /* Mimic bash message */ |
| 6807 | bb_error_msg("read: '%s': not a valid identifier", name); |
| 6808 | return 1; |
| 6809 | } |
| 6810 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6811 | |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 6812 | //TODO: bash unbackslashes input, splits words and puts them in argv[i] |
| 6813 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6814 | string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6815 | return set_local_var(string, 0, 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6816 | } |
| 6817 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6818 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 6819 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 6820 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6821 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 6822 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 6823 | * set -- [argument...] |
| 6824 | * set -o |
| 6825 | * set +o |
| 6826 | * Implementations shall support the options in both their hyphen and |
| 6827 | * plus-sign forms. These options can also be specified as options to sh. |
| 6828 | * Examples: |
| 6829 | * Write out all variables and their values: set |
| 6830 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 6831 | * Turn on the -x and -v options: set -xv |
| 6832 | * Unset all positional parameters: set -- |
| 6833 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 6834 | * Set the positional parameters to the expansion of x, even if x expands |
| 6835 | * with a leading '-' or '+': set -- $x |
| 6836 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6837 | * 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] | 6838 | */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6839 | static int builtin_set(char **argv) |
| 6840 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 6841 | int n; |
| 6842 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 6843 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6844 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 6845 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 6846 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6847 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6848 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 6849 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 6850 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6851 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6852 | do { |
| 6853 | if (!strcmp(arg, "--")) { |
| 6854 | ++argv; |
| 6855 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 6856 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 6857 | if (arg[0] != '+' && arg[0] != '-') |
| 6858 | break; |
| 6859 | for (n = 1; arg[n]; ++n) |
| 6860 | if (set_mode(arg[0], arg[n])) |
| 6861 | goto error; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 6862 | } while ((arg = *++argv) != NULL); |
| 6863 | /* Now argv[0] is 1st argument */ |
| 6864 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6865 | if (arg == NULL) |
| 6866 | return EXIT_SUCCESS; |
| 6867 | set_argv: |
| 6868 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 6869 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 6870 | g_argv = G.global_argv; |
| 6871 | if (G.global_args_malloced) { |
| 6872 | pp = g_argv; |
| 6873 | while (*++pp) |
| 6874 | free(*pp); |
| 6875 | g_argv[1] = NULL; |
| 6876 | } else { |
| 6877 | G.global_args_malloced = 1; |
| 6878 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 6879 | pp[0] = g_argv[0]; /* retain $0 */ |
| 6880 | g_argv = pp; |
| 6881 | } |
| 6882 | /* This realloc's G.global_argv */ |
| 6883 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 6884 | |
| 6885 | n = 1; |
| 6886 | while (*++pp) |
| 6887 | n++; |
| 6888 | G.global_argc = n; |
| 6889 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6890 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6891 | |
| 6892 | /* Nothing known, so abort */ |
| 6893 | error: |
| 6894 | bb_error_msg("set: %s: invalid option", arg); |
| 6895 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6896 | } |
| 6897 | |
| 6898 | static int builtin_shift(char **argv) |
| 6899 | { |
| 6900 | int n = 1; |
| 6901 | if (argv[1]) { |
| 6902 | n = atoi(argv[1]); |
| 6903 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6904 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 6905 | if (G.global_args_malloced) { |
| 6906 | int m = 1; |
| 6907 | while (m <= n) |
| 6908 | free(G.global_argv[m++]); |
| 6909 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6910 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 6911 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 6912 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6913 | return EXIT_SUCCESS; |
| 6914 | } |
| 6915 | return EXIT_FAILURE; |
| 6916 | } |
| 6917 | |
| 6918 | static int builtin_source(char **argv) |
| 6919 | { |
Denys Vlasenko | 54e0843 | 2009-05-02 02:34:19 +0200 | [diff] [blame] | 6920 | const char *PATH; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6921 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 6922 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 6923 | #if ENABLE_HUSH_FUNCTIONS |
| 6924 | smallint sv_flg; |
| 6925 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6926 | |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6927 | if (*++argv == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6928 | return EXIT_FAILURE; |
| 6929 | |
Denys Vlasenko | 54e0843 | 2009-05-02 02:34:19 +0200 | [diff] [blame] | 6930 | if (strchr(*argv, '/') == NULL |
| 6931 | && (PATH = get_local_var_value("PATH")) != NULL |
| 6932 | ) { |
| 6933 | /* Search through $PATH */ |
| 6934 | while (1) { |
| 6935 | const char *end = strchrnul(PATH, ':'); |
| 6936 | int sz = end - PATH; /* must be int! */ |
| 6937 | |
| 6938 | if (sz != 0) { |
| 6939 | char *tmp = xasprintf("%.*s/%s", sz, PATH, *argv); |
| 6940 | input = fopen_for_read(tmp); |
| 6941 | free(tmp); |
| 6942 | } else { |
| 6943 | /* We have xxx::yyyy in $PATH, |
| 6944 | * it means "use current dir" */ |
| 6945 | input = fopen_for_read(*argv); |
| 6946 | } |
| 6947 | if (input) |
| 6948 | goto opened_ok; |
| 6949 | if (*end == '\0') |
| 6950 | break; |
| 6951 | PATH = end + 1; |
| 6952 | } |
| 6953 | } |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6954 | input = fopen_or_warn(*argv, "r"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6955 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6956 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6957 | return EXIT_FAILURE; |
| 6958 | } |
Denys Vlasenko | 54e0843 | 2009-05-02 02:34:19 +0200 | [diff] [blame] | 6959 | opened_ok: |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6960 | close_on_exec_on(fileno(input)); |
| 6961 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 6962 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 6963 | sv_flg = G.flag_return_in_progress; |
| 6964 | /* "we are inside sourced file, ok to use return" */ |
| 6965 | G.flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 6966 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 6967 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 6968 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6969 | parse_and_run_file(input); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6970 | fclose(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 6971 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 6972 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 6973 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 6974 | G.flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 6975 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 6976 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6977 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6978 | } |
| 6979 | |
| 6980 | static int builtin_umask(char **argv) |
| 6981 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 6982 | int rc; |
| 6983 | mode_t mask; |
| 6984 | |
| 6985 | mask = umask(0); |
| 6986 | if (argv[1]) { |
| 6987 | mode_t old_mask = mask; |
| 6988 | |
| 6989 | mask ^= 0777; |
| 6990 | rc = bb_parse_mode(argv[1], &mask); |
| 6991 | mask ^= 0777; |
| 6992 | if (rc == 0) { |
| 6993 | mask = old_mask; |
| 6994 | /* bash messages: |
| 6995 | * bash: umask: 'q': invalid symbolic mode operator |
| 6996 | * bash: umask: 999: octal number out of range |
| 6997 | */ |
| 6998 | bb_error_msg("%s: '%s' invalid mode", argv[0], argv[1]); |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6999 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7000 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7001 | rc = 1; |
| 7002 | /* Mimic bash */ |
| 7003 | printf("%04o\n", (unsigned) mask); |
| 7004 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7005 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7006 | umask(mask); |
| 7007 | |
| 7008 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7009 | } |
| 7010 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7011 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7012 | static int builtin_unset(char **argv) |
| 7013 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7014 | int ret; |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7015 | unsigned opts; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7016 | |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7017 | /* "!": do not abort on errors */ |
| 7018 | /* "+": stop at 1st non-option */ |
| 7019 | opts = getopt32(argv, "!+vf"); |
| 7020 | if (opts == (unsigned)-1) |
| 7021 | return EXIT_FAILURE; |
| 7022 | if (opts == 3) { |
| 7023 | bb_error_msg("unset: -v and -f are exclusive"); |
| 7024 | return EXIT_FAILURE; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7025 | } |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7026 | argv += optind; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7027 | |
| 7028 | ret = EXIT_SUCCESS; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7029 | while (*argv) { |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7030 | if (!(opts & 2)) { /* not -f */ |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7031 | if (unset_local_var(*argv)) { |
| 7032 | /* unset <nonexistent_var> doesn't fail. |
| 7033 | * Error is when one tries to unset RO var. |
| 7034 | * Message was printed by unset_local_var. */ |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7035 | ret = EXIT_FAILURE; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7036 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7037 | } |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 7038 | #if ENABLE_HUSH_FUNCTIONS |
| 7039 | else { |
| 7040 | unset_func(*argv); |
| 7041 | } |
| 7042 | #endif |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7043 | argv++; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7044 | } |
| 7045 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7046 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7047 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7048 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
| 7049 | static int builtin_wait(char **argv) |
| 7050 | { |
| 7051 | int ret = EXIT_SUCCESS; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7052 | int status, sig; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7053 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7054 | if (*++argv == NULL) { |
| 7055 | /* Don't care about wait results */ |
| 7056 | /* Note 1: must wait until there are no more children */ |
| 7057 | /* Note 2: must be interruptible */ |
| 7058 | /* Examples: |
| 7059 | * $ sleep 3 & sleep 6 & wait |
| 7060 | * [1] 30934 sleep 3 |
| 7061 | * [2] 30935 sleep 6 |
| 7062 | * [1] Done sleep 3 |
| 7063 | * [2] Done sleep 6 |
| 7064 | * $ sleep 3 & sleep 6 & wait |
| 7065 | * [1] 30936 sleep 3 |
| 7066 | * [2] 30937 sleep 6 |
| 7067 | * [1] Done sleep 3 |
| 7068 | * ^C <-- after ~4 sec from keyboard |
| 7069 | * $ |
| 7070 | */ |
| 7071 | sigaddset(&G.blocked_set, SIGCHLD); |
| 7072 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7073 | while (1) { |
| 7074 | checkjobs(NULL); |
| 7075 | if (errno == ECHILD) |
| 7076 | break; |
| 7077 | /* Wait for SIGCHLD or any other signal of interest */ |
| 7078 | /* sigtimedwait with infinite timeout: */ |
| 7079 | sig = sigwaitinfo(&G.blocked_set, NULL); |
| 7080 | if (sig > 0) { |
| 7081 | sig = check_and_run_traps(sig); |
| 7082 | if (sig && sig != SIGCHLD) { /* see note 2 */ |
| 7083 | ret = 128 + sig; |
| 7084 | break; |
| 7085 | } |
| 7086 | } |
| 7087 | } |
| 7088 | sigdelset(&G.blocked_set, SIGCHLD); |
| 7089 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7090 | return ret; |
| 7091 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7092 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7093 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7094 | while (*argv) { |
| 7095 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 7096 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7097 | /* mimic bash message */ |
| 7098 | 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] | 7099 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7100 | } |
| 7101 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7102 | if (WIFSIGNALED(status)) |
| 7103 | ret = 128 + WTERMSIG(status); |
| 7104 | else if (WIFEXITED(status)) |
| 7105 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7106 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7107 | ret = EXIT_FAILURE; |
| 7108 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7109 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7110 | ret = 127; |
| 7111 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7112 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7113 | } |
| 7114 | |
| 7115 | return ret; |
| 7116 | } |
| 7117 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7118 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 7119 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 7120 | { |
| 7121 | if (argv[1]) { |
| 7122 | def = bb_strtou(argv[1], NULL, 10); |
| 7123 | if (errno || def < def_min || argv[2]) { |
| 7124 | bb_error_msg("%s: bad arguments", argv[0]); |
| 7125 | def = UINT_MAX; |
| 7126 | } |
| 7127 | } |
| 7128 | return def; |
| 7129 | } |
| 7130 | #endif |
| 7131 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 7132 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 7133 | static int builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7134 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7135 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7136 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 7137 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 7138 | return EXIT_SUCCESS; /* bash compat */ |
| 7139 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7140 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7141 | |
| 7142 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 7143 | if (depth == UINT_MAX) |
| 7144 | G.flag_break_continue = BC_BREAK; |
| 7145 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7146 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7147 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7148 | return EXIT_SUCCESS; |
| 7149 | } |
| 7150 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 7151 | static int builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7152 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 7153 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 7154 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7155 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 7156 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7157 | |
| 7158 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 7b9e5c5 | 2009-04-17 23:53:15 +0000 | [diff] [blame] | 7159 | static int builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7160 | { |
| 7161 | int rc; |
| 7162 | |
| 7163 | if (G.flag_return_in_progress != -1) { |
| 7164 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 7165 | return EXIT_FAILURE; /* bash compat */ |
| 7166 | } |
| 7167 | |
| 7168 | G.flag_return_in_progress = 1; |
| 7169 | |
| 7170 | /* bash: |
| 7171 | * out of range: wraps around at 256, does not error out |
| 7172 | * non-numeric param: |
| 7173 | * f() { false; return qwe; }; f; echo $? |
| 7174 | * bash: return: qwe: numeric argument required <== we do this |
| 7175 | * 255 <== we also do this |
| 7176 | */ |
| 7177 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 7178 | return rc; |
| 7179 | } |
| 7180 | #endif |