Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * ash shell port for busybox |
| 4 | * |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 5 | * This code is derived from software contributed to Berkeley by |
| 6 | * Kenneth Almquist. |
| 7 | * |
| 8 | * Original BSD copyright notice is retained at the end of this file. |
| 9 | * |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 10 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12 | * |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13 | * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au> |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 14 | * was re-ported from NetBSD and debianized. |
| 15 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 16 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 19 | /* |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 20 | * The following should be set to reflect the type of system you have: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 21 | * JOBS -> 1 if you have Berkeley job control, 0 otherwise. |
| 22 | * define SYSV if you are running under System V. |
| 23 | * define DEBUG=1 to compile in debugging ('set -o debug' to turn on) |
| 24 | * define DEBUG=2 to compile in and turn on debugging. |
| 25 | * |
| 26 | * When debugging is on, debugging info will be written to ./trace and |
| 27 | * a quit signal will generate a core dump. |
| 28 | */ |
Denis Vlasenko | f173395 | 2009-03-19 23:21:55 +0000 | [diff] [blame] | 29 | #define DEBUG 0 |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 30 | /* Tweak debug output verbosity here */ |
| 31 | #define DEBUG_TIME 0 |
| 32 | #define DEBUG_PID 1 |
| 33 | #define DEBUG_SIG 1 |
| 34 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 35 | #define PROFILE 0 |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 37 | #define JOBS ENABLE_ASH_JOB_CONTROL |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 39 | #if DEBUG |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 40 | # ifndef _GNU_SOURCE |
| 41 | # define _GNU_SOURCE |
| 42 | # endif |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 43 | #endif |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 44 | |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 45 | #include "busybox.h" /* for applet_names */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 46 | #include <paths.h> |
| 47 | #include <setjmp.h> |
| 48 | #include <fnmatch.h> |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 49 | |
| 50 | #include "shell_common.h" |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 51 | #include "math.h" |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 52 | #if ENABLE_ASH_RANDOM_SUPPORT |
| 53 | # include "random.h" |
Denys Vlasenko | 36df048 | 2009-10-19 16:07:28 +0200 | [diff] [blame] | 54 | #else |
| 55 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 56 | #endif |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 57 | |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 58 | #define SKIP_definitions 1 |
| 59 | #include "applet_tables.h" |
| 60 | #undef SKIP_definitions |
| 61 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 62 | /* STANDALONE does not make sense, and won't compile */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 63 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 64 | # undef ENABLE_FEATURE_SH_STANDALONE |
| 65 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 66 | # undef IF_NOT_FEATURE_SH_STANDALONE |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 67 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
| 68 | # define IF_FEATURE_SH_STANDALONE(...) |
| 69 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 70 | #endif |
| 71 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 72 | #ifndef PIPE_BUF |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 73 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 74 | #endif |
| 75 | |
Denys Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 76 | #if !BB_MMU |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 77 | # error "Do not even bother, ash will not run on NOMMU machine" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 78 | #endif |
| 79 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 81 | /* ============ Hash table sizes. Configurable. */ |
| 82 | |
| 83 | #define VTABSIZE 39 |
| 84 | #define ATABSIZE 39 |
| 85 | #define CMDTABLESIZE 31 /* should be prime */ |
| 86 | |
| 87 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 88 | /* ============ Shell options */ |
| 89 | |
| 90 | static const char *const optletters_optnames[] = { |
| 91 | "e" "errexit", |
| 92 | "f" "noglob", |
| 93 | "I" "ignoreeof", |
| 94 | "i" "interactive", |
| 95 | "m" "monitor", |
| 96 | "n" "noexec", |
| 97 | "s" "stdin", |
| 98 | "x" "xtrace", |
| 99 | "v" "verbose", |
| 100 | "C" "noclobber", |
| 101 | "a" "allexport", |
| 102 | "b" "notify", |
| 103 | "u" "nounset", |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 104 | "\0" "vi" |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 105 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 106 | ,"\0" "pipefail" |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 107 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 108 | #if DEBUG |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 109 | ,"\0" "nolog" |
| 110 | ,"\0" "debug" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 111 | #endif |
| 112 | }; |
| 113 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 114 | #define optletters(n) optletters_optnames[n][0] |
| 115 | #define optnames(n) (optletters_optnames[n] + 1) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 116 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 117 | enum { NOPTS = ARRAY_SIZE(optletters_optnames) }; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 118 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 119 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 120 | /* ============ Misc data */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 121 | |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 122 | #define msg_illnum "Illegal number: %s" |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 123 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 124 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 125 | * We enclose jmp_buf in a structure so that we can declare pointers to |
| 126 | * jump locations. The global variable handler contains the location to |
Denis Vlasenko | f173395 | 2009-03-19 23:21:55 +0000 | [diff] [blame] | 127 | * jump to when an exception occurs, and the global variable exception_type |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 128 | * contains a code identifying the exception. To implement nested |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 129 | * exception handlers, the user should save the value of handler on entry |
| 130 | * to an inner scope, set handler to point to a jmploc structure for the |
| 131 | * inner scope, and restore handler on exit from the scope. |
| 132 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 133 | struct jmploc { |
| 134 | jmp_buf loc; |
| 135 | }; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 136 | |
| 137 | struct globals_misc { |
| 138 | /* pid of main shell */ |
| 139 | int rootpid; |
| 140 | /* shell level: 0 for the main shell, 1 for its children, and so on */ |
| 141 | int shlvl; |
| 142 | #define rootshell (!shlvl) |
| 143 | char *minusc; /* argument to -c option */ |
| 144 | |
| 145 | char *curdir; // = nullstr; /* current working directory */ |
| 146 | char *physdir; // = nullstr; /* physical working directory */ |
| 147 | |
| 148 | char *arg0; /* value of $0 */ |
| 149 | |
| 150 | struct jmploc *exception_handler; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 151 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 152 | volatile int suppress_int; /* counter */ |
| 153 | volatile /*sig_atomic_t*/ smallint pending_int; /* 1 = got SIGINT */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 154 | /* last pending signal */ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 155 | volatile /*sig_atomic_t*/ smallint pending_sig; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 156 | smallint exception_type; /* kind of exception (0..5) */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 157 | /* exceptions */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 158 | #define EXINT 0 /* SIGINT received */ |
| 159 | #define EXERROR 1 /* a generic error */ |
| 160 | #define EXSHELLPROC 2 /* execute a shell procedure */ |
| 161 | #define EXEXEC 3 /* command execution failed */ |
| 162 | #define EXEXIT 4 /* exit the shell */ |
| 163 | #define EXSIG 5 /* trapped signal in wait(1) */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 164 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 165 | smallint isloginsh; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 166 | char nullstr[1]; /* zero length string */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 167 | |
| 168 | char optlist[NOPTS]; |
| 169 | #define eflag optlist[0] |
| 170 | #define fflag optlist[1] |
| 171 | #define Iflag optlist[2] |
| 172 | #define iflag optlist[3] |
| 173 | #define mflag optlist[4] |
| 174 | #define nflag optlist[5] |
| 175 | #define sflag optlist[6] |
| 176 | #define xflag optlist[7] |
| 177 | #define vflag optlist[8] |
| 178 | #define Cflag optlist[9] |
| 179 | #define aflag optlist[10] |
| 180 | #define bflag optlist[11] |
| 181 | #define uflag optlist[12] |
| 182 | #define viflag optlist[13] |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 183 | #if ENABLE_ASH_BASH_COMPAT |
| 184 | # define pipefail optlist[14] |
| 185 | #else |
| 186 | # define pipefail 0 |
| 187 | #endif |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 188 | #if DEBUG |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 189 | # define nolog optlist[14 + ENABLE_ASH_BASH_COMPAT] |
| 190 | # define debug optlist[15 + ENABLE_ASH_BASH_COMPAT] |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 191 | #endif |
| 192 | |
| 193 | /* trap handler commands */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 194 | /* |
| 195 | * Sigmode records the current value of the signal handlers for the various |
| 196 | * modes. A value of zero means that the current handler is not known. |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 197 | * S_HARD_IGN indicates that the signal was ignored on entry to the shell. |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 198 | */ |
| 199 | char sigmode[NSIG - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 200 | #define S_DFL 1 /* default signal handling (SIG_DFL) */ |
| 201 | #define S_CATCH 2 /* signal is caught */ |
| 202 | #define S_IGN 3 /* signal is ignored (SIG_IGN) */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 203 | #define S_HARD_IGN 4 /* signal is ignored permenantly */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 204 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 205 | /* indicates specified signal received */ |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 206 | uint8_t gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 207 | uint8_t may_have_traps; /* 0: definitely no traps are set, 1: some traps may be set */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 208 | char *trap[NSIG]; |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 209 | char **trap_ptr; /* used only by "trap hack" */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 210 | |
| 211 | /* Rarely referenced stuff */ |
| 212 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 213 | random_t random_gen; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 214 | #endif |
| 215 | pid_t backgndpid; /* pid of last background process */ |
| 216 | smallint job_warning; /* user was warned about stopped jobs (can be 2, 1 or 0). */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 217 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 218 | extern struct globals_misc *const ash_ptr_to_globals_misc; |
| 219 | #define G_misc (*ash_ptr_to_globals_misc) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 220 | #define rootpid (G_misc.rootpid ) |
| 221 | #define shlvl (G_misc.shlvl ) |
| 222 | #define minusc (G_misc.minusc ) |
| 223 | #define curdir (G_misc.curdir ) |
| 224 | #define physdir (G_misc.physdir ) |
| 225 | #define arg0 (G_misc.arg0 ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 226 | #define exception_handler (G_misc.exception_handler) |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 227 | #define exception_type (G_misc.exception_type ) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 228 | #define suppress_int (G_misc.suppress_int ) |
| 229 | #define pending_int (G_misc.pending_int ) |
| 230 | #define pending_sig (G_misc.pending_sig ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 231 | #define isloginsh (G_misc.isloginsh ) |
| 232 | #define nullstr (G_misc.nullstr ) |
| 233 | #define optlist (G_misc.optlist ) |
| 234 | #define sigmode (G_misc.sigmode ) |
| 235 | #define gotsig (G_misc.gotsig ) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 236 | #define may_have_traps (G_misc.may_have_traps ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 237 | #define trap (G_misc.trap ) |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 238 | #define trap_ptr (G_misc.trap_ptr ) |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 239 | #define random_gen (G_misc.random_gen ) |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 240 | #define backgndpid (G_misc.backgndpid ) |
| 241 | #define job_warning (G_misc.job_warning) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 242 | #define INIT_G_misc() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 243 | (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \ |
| 244 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 245 | curdir = nullstr; \ |
| 246 | physdir = nullstr; \ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 247 | trap_ptr = trap; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 248 | } while (0) |
| 249 | |
| 250 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 251 | /* ============ DEBUG */ |
| 252 | #if DEBUG |
| 253 | static void trace_printf(const char *fmt, ...); |
| 254 | static void trace_vprintf(const char *fmt, va_list va); |
| 255 | # define TRACE(param) trace_printf param |
| 256 | # define TRACEV(param) trace_vprintf param |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 257 | # define close(fd) do { \ |
| 258 | int dfd = (fd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 259 | if (close(dfd) < 0) \ |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 260 | bb_error_msg("bug on %d: closing %d(0x%x)", \ |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 261 | __LINE__, dfd, dfd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 262 | } while (0) |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 263 | #else |
| 264 | # define TRACE(param) |
| 265 | # define TRACEV(param) |
| 266 | #endif |
| 267 | |
| 268 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 269 | /* ============ Utility functions */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 270 | #define xbarrier() do { __asm__ __volatile__ ("": : :"memory"); } while (0) |
| 271 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 272 | static int isdigit_str9(const char *str) |
| 273 | { |
| 274 | int maxlen = 9 + 1; /* max 9 digits: 999999999 */ |
| 275 | while (--maxlen && isdigit(*str)) |
| 276 | str++; |
| 277 | return (*str == '\0'); |
| 278 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 279 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 280 | static const char *var_end(const char *var) |
| 281 | { |
| 282 | while (*var) |
| 283 | if (*var++ == '=') |
| 284 | break; |
| 285 | return var; |
| 286 | } |
| 287 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 288 | |
| 289 | /* ============ Interrupts / exceptions */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 290 | /* |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 291 | * These macros allow the user to suspend the handling of interrupt signals |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 292 | * over a period of time. This is similar to SIGHOLD or to sigblock, but |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 293 | * much more efficient and portable. (But hacking the kernel is so much |
| 294 | * more fun than worrying about efficiency and portability. :-)) |
| 295 | */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 296 | #define INT_OFF do { \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 297 | suppress_int++; \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 298 | xbarrier(); \ |
| 299 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 300 | |
| 301 | /* |
| 302 | * Called to raise an exception. Since C doesn't include exceptions, we |
| 303 | * just do a longjmp to the exception handler. The type of exception is |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 304 | * stored in the global variable "exception_type". |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 305 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 306 | static void raise_exception(int) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 307 | static void |
| 308 | raise_exception(int e) |
| 309 | { |
| 310 | #if DEBUG |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 311 | if (exception_handler == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 312 | abort(); |
| 313 | #endif |
| 314 | INT_OFF; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 315 | exception_type = e; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 316 | longjmp(exception_handler->loc, 1); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 317 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 318 | #if DEBUG |
| 319 | #define raise_exception(e) do { \ |
| 320 | TRACE(("raising exception %d on line %d\n", (e), __LINE__)); \ |
| 321 | raise_exception(e); \ |
| 322 | } while (0) |
| 323 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 324 | |
| 325 | /* |
| 326 | * Called from trap.c when a SIGINT is received. (If the user specifies |
| 327 | * that SIGINT is to be trapped or ignored using the trap builtin, then |
| 328 | * this routine is not called.) Suppressint is nonzero when interrupts |
| 329 | * are held using the INT_OFF macro. (The test for iflag is just |
| 330 | * defensive programming.) |
| 331 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 332 | static void raise_interrupt(void) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 333 | static void |
| 334 | raise_interrupt(void) |
| 335 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 336 | int ex_type; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 337 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 338 | pending_int = 0; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 339 | /* Signal is not automatically unmasked after it is raised, |
| 340 | * do it ourself - unmask all signals */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 341 | sigprocmask_allsigs(SIG_UNBLOCK); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 342 | /* pending_sig = 0; - now done in signal_handler() */ |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 343 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 344 | ex_type = EXSIG; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 345 | if (gotsig[SIGINT - 1] && !trap[SIGINT]) { |
| 346 | if (!(rootshell && iflag)) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 347 | /* Kill ourself with SIGINT */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 348 | signal(SIGINT, SIG_DFL); |
| 349 | raise(SIGINT); |
| 350 | } |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 351 | ex_type = EXINT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 352 | } |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 353 | raise_exception(ex_type); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 354 | /* NOTREACHED */ |
| 355 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 356 | #if DEBUG |
| 357 | #define raise_interrupt() do { \ |
| 358 | TRACE(("raising interrupt on line %d\n", __LINE__)); \ |
| 359 | raise_interrupt(); \ |
| 360 | } while (0) |
| 361 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 362 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 363 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 364 | int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 365 | { |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 366 | xbarrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 367 | if (--suppress_int == 0 && pending_int) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 368 | raise_interrupt(); |
| 369 | } |
| 370 | } |
| 371 | #define INT_ON int_on() |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 372 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 373 | force_int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 374 | { |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 375 | xbarrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 376 | suppress_int = 0; |
| 377 | if (pending_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 378 | raise_interrupt(); |
| 379 | } |
| 380 | #define FORCE_INT_ON force_int_on() |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 381 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 382 | #define SAVE_INT(v) ((v) = suppress_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 383 | |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 384 | #define RESTORE_INT(v) do { \ |
| 385 | xbarrier(); \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 386 | suppress_int = (v); \ |
| 387 | if (suppress_int == 0 && pending_int) \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 388 | raise_interrupt(); \ |
| 389 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 390 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 391 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 392 | /* ============ Stdout/stderr output */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 393 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 394 | static void |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 395 | outstr(const char *p, FILE *file) |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 396 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 397 | INT_OFF; |
| 398 | fputs(p, file); |
| 399 | INT_ON; |
| 400 | } |
| 401 | |
| 402 | static void |
| 403 | flush_stdout_stderr(void) |
| 404 | { |
| 405 | INT_OFF; |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 406 | fflush_all(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 407 | INT_ON; |
| 408 | } |
| 409 | |
| 410 | static void |
| 411 | outcslow(int c, FILE *dest) |
| 412 | { |
| 413 | INT_OFF; |
| 414 | putc(c, dest); |
| 415 | fflush(dest); |
| 416 | INT_ON; |
| 417 | } |
| 418 | |
| 419 | static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2))); |
| 420 | static int |
| 421 | out1fmt(const char *fmt, ...) |
| 422 | { |
| 423 | va_list ap; |
| 424 | int r; |
| 425 | |
| 426 | INT_OFF; |
| 427 | va_start(ap, fmt); |
| 428 | r = vprintf(fmt, ap); |
| 429 | va_end(ap); |
| 430 | INT_ON; |
| 431 | return r; |
| 432 | } |
| 433 | |
| 434 | static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4))); |
| 435 | static int |
| 436 | fmtstr(char *outbuf, size_t length, const char *fmt, ...) |
| 437 | { |
| 438 | va_list ap; |
| 439 | int ret; |
| 440 | |
| 441 | va_start(ap, fmt); |
| 442 | INT_OFF; |
| 443 | ret = vsnprintf(outbuf, length, fmt, ap); |
| 444 | va_end(ap); |
| 445 | INT_ON; |
| 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | static void |
| 450 | out1str(const char *p) |
| 451 | { |
| 452 | outstr(p, stdout); |
| 453 | } |
| 454 | |
| 455 | static void |
| 456 | out2str(const char *p) |
| 457 | { |
| 458 | outstr(p, stderr); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 459 | flush_stdout_stderr(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 463 | /* ============ Parser structures */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 464 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 465 | /* control characters in argument strings */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 466 | #define CTL_FIRST CTLESC |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 467 | #define CTLESC ((unsigned char)'\201') /* escape next character */ |
| 468 | #define CTLVAR ((unsigned char)'\202') /* variable defn */ |
| 469 | #define CTLENDVAR ((unsigned char)'\203') |
| 470 | #define CTLBACKQ ((unsigned char)'\204') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 471 | #define CTLQUOTE 01 /* ored with CTLBACKQ code if in quotes */ |
| 472 | /* CTLBACKQ | CTLQUOTE == '\205' */ |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 473 | #define CTLARI ((unsigned char)'\206') /* arithmetic expression */ |
| 474 | #define CTLENDARI ((unsigned char)'\207') |
| 475 | #define CTLQUOTEMARK ((unsigned char)'\210') |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 476 | #define CTL_LAST CTLQUOTEMARK |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 477 | |
| 478 | /* variable substitution byte (follows CTLVAR) */ |
| 479 | #define VSTYPE 0x0f /* type of variable substitution */ |
| 480 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ |
| 481 | #define VSQUOTE 0x80 /* inside double quotes--suppress splitting */ |
| 482 | |
| 483 | /* values of VSTYPE field */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 484 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ |
| 485 | #define VSMINUS 0x2 /* ${var-text} */ |
| 486 | #define VSPLUS 0x3 /* ${var+text} */ |
| 487 | #define VSQUESTION 0x4 /* ${var?message} */ |
| 488 | #define VSASSIGN 0x5 /* ${var=text} */ |
| 489 | #define VSTRIMRIGHT 0x6 /* ${var%pattern} */ |
| 490 | #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */ |
| 491 | #define VSTRIMLEFT 0x8 /* ${var#pattern} */ |
| 492 | #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */ |
| 493 | #define VSLENGTH 0xa /* ${#var} */ |
| 494 | #if ENABLE_ASH_BASH_COMPAT |
| 495 | #define VSSUBSTR 0xc /* ${var:position:length} */ |
| 496 | #define VSREPLACE 0xd /* ${var/pattern/replacement} */ |
| 497 | #define VSREPLACEALL 0xe /* ${var//pattern/replacement} */ |
| 498 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 499 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 500 | static const char dolatstr[] ALIGN1 = { |
| 501 | CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' |
| 502 | }; |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 503 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 504 | #define NCMD 0 |
| 505 | #define NPIPE 1 |
| 506 | #define NREDIR 2 |
| 507 | #define NBACKGND 3 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 508 | #define NSUBSHELL 4 |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 509 | #define NAND 5 |
| 510 | #define NOR 6 |
| 511 | #define NSEMI 7 |
| 512 | #define NIF 8 |
| 513 | #define NWHILE 9 |
| 514 | #define NUNTIL 10 |
| 515 | #define NFOR 11 |
| 516 | #define NCASE 12 |
| 517 | #define NCLIST 13 |
| 518 | #define NDEFUN 14 |
| 519 | #define NARG 15 |
| 520 | #define NTO 16 |
| 521 | #if ENABLE_ASH_BASH_COMPAT |
| 522 | #define NTO2 17 |
| 523 | #endif |
| 524 | #define NCLOBBER 18 |
| 525 | #define NFROM 19 |
| 526 | #define NFROMTO 20 |
| 527 | #define NAPPEND 21 |
| 528 | #define NTOFD 22 |
| 529 | #define NFROMFD 23 |
| 530 | #define NHERE 24 |
| 531 | #define NXHERE 25 |
| 532 | #define NNOT 26 |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 533 | #define N_NUMBER 27 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 534 | |
| 535 | union node; |
| 536 | |
| 537 | struct ncmd { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 538 | smallint type; /* Nxxxx */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 539 | union node *assign; |
| 540 | union node *args; |
| 541 | union node *redirect; |
| 542 | }; |
| 543 | |
| 544 | struct npipe { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 545 | smallint type; |
| 546 | smallint pipe_backgnd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 547 | struct nodelist *cmdlist; |
| 548 | }; |
| 549 | |
| 550 | struct nredir { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 551 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 552 | union node *n; |
| 553 | union node *redirect; |
| 554 | }; |
| 555 | |
| 556 | struct nbinary { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 557 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 558 | union node *ch1; |
| 559 | union node *ch2; |
| 560 | }; |
| 561 | |
| 562 | struct nif { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 563 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 564 | union node *test; |
| 565 | union node *ifpart; |
| 566 | union node *elsepart; |
| 567 | }; |
| 568 | |
| 569 | struct nfor { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 570 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 571 | union node *args; |
| 572 | union node *body; |
| 573 | char *var; |
| 574 | }; |
| 575 | |
| 576 | struct ncase { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 577 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 578 | union node *expr; |
| 579 | union node *cases; |
| 580 | }; |
| 581 | |
| 582 | struct nclist { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 583 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 584 | union node *next; |
| 585 | union node *pattern; |
| 586 | union node *body; |
| 587 | }; |
| 588 | |
| 589 | struct narg { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 590 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 591 | union node *next; |
| 592 | char *text; |
| 593 | struct nodelist *backquote; |
| 594 | }; |
| 595 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 596 | /* nfile and ndup layout must match! |
| 597 | * NTOFD (>&fdnum) uses ndup structure, but we may discover mid-flight |
| 598 | * that it is actually NTO2 (>&file), and change its type. |
| 599 | */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 600 | struct nfile { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 601 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 602 | union node *next; |
| 603 | int fd; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 604 | int _unused_dupfd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 605 | union node *fname; |
| 606 | char *expfname; |
| 607 | }; |
| 608 | |
| 609 | struct ndup { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 610 | smallint type; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 611 | union node *next; |
| 612 | int fd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 613 | int dupfd; |
| 614 | union node *vname; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 615 | char *_unused_expfname; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 616 | }; |
| 617 | |
| 618 | struct nhere { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 619 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 620 | union node *next; |
| 621 | int fd; |
| 622 | union node *doc; |
| 623 | }; |
| 624 | |
| 625 | struct nnot { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 626 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 627 | union node *com; |
| 628 | }; |
| 629 | |
| 630 | union node { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 631 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 632 | struct ncmd ncmd; |
| 633 | struct npipe npipe; |
| 634 | struct nredir nredir; |
| 635 | struct nbinary nbinary; |
| 636 | struct nif nif; |
| 637 | struct nfor nfor; |
| 638 | struct ncase ncase; |
| 639 | struct nclist nclist; |
| 640 | struct narg narg; |
| 641 | struct nfile nfile; |
| 642 | struct ndup ndup; |
| 643 | struct nhere nhere; |
| 644 | struct nnot nnot; |
| 645 | }; |
| 646 | |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 647 | /* |
| 648 | * NODE_EOF is returned by parsecmd when it encounters an end of file. |
| 649 | * It must be distinct from NULL. |
| 650 | */ |
| 651 | #define NODE_EOF ((union node *) -1L) |
| 652 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 653 | struct nodelist { |
| 654 | struct nodelist *next; |
| 655 | union node *n; |
| 656 | }; |
| 657 | |
| 658 | struct funcnode { |
| 659 | int count; |
| 660 | union node n; |
| 661 | }; |
| 662 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 663 | /* |
| 664 | * Free a parse tree. |
| 665 | */ |
| 666 | static void |
| 667 | freefunc(struct funcnode *f) |
| 668 | { |
| 669 | if (f && --f->count < 0) |
| 670 | free(f); |
| 671 | } |
| 672 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 673 | |
| 674 | /* ============ Debugging output */ |
| 675 | |
| 676 | #if DEBUG |
| 677 | |
| 678 | static FILE *tracefile; |
| 679 | |
| 680 | static void |
| 681 | trace_printf(const char *fmt, ...) |
| 682 | { |
| 683 | va_list va; |
| 684 | |
| 685 | if (debug != 1) |
| 686 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 687 | if (DEBUG_TIME) |
| 688 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 689 | if (DEBUG_PID) |
| 690 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 691 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 692 | fprintf(tracefile, "pending s:%d i:%d(supp:%d) ", pending_sig, pending_int, suppress_int); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 693 | va_start(va, fmt); |
| 694 | vfprintf(tracefile, fmt, va); |
| 695 | va_end(va); |
| 696 | } |
| 697 | |
| 698 | static void |
| 699 | trace_vprintf(const char *fmt, va_list va) |
| 700 | { |
| 701 | if (debug != 1) |
| 702 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 703 | if (DEBUG_TIME) |
| 704 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 705 | if (DEBUG_PID) |
| 706 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 707 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 708 | fprintf(tracefile, "pending s:%d i:%d(supp:%d) ", pending_sig, pending_int, suppress_int); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 709 | vfprintf(tracefile, fmt, va); |
| 710 | } |
| 711 | |
| 712 | static void |
| 713 | trace_puts(const char *s) |
| 714 | { |
| 715 | if (debug != 1) |
| 716 | return; |
| 717 | fputs(s, tracefile); |
| 718 | } |
| 719 | |
| 720 | static void |
| 721 | trace_puts_quoted(char *s) |
| 722 | { |
| 723 | char *p; |
| 724 | char c; |
| 725 | |
| 726 | if (debug != 1) |
| 727 | return; |
| 728 | putc('"', tracefile); |
| 729 | for (p = s; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 730 | switch ((unsigned char)*p) { |
| 731 | case '\n': c = 'n'; goto backslash; |
| 732 | case '\t': c = 't'; goto backslash; |
| 733 | case '\r': c = 'r'; goto backslash; |
| 734 | case '\"': c = '\"'; goto backslash; |
| 735 | case '\\': c = '\\'; goto backslash; |
| 736 | case CTLESC: c = 'e'; goto backslash; |
| 737 | case CTLVAR: c = 'v'; goto backslash; |
| 738 | case CTLVAR+CTLQUOTE: c = 'V'; goto backslash; |
| 739 | case CTLBACKQ: c = 'q'; goto backslash; |
| 740 | case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 741 | backslash: |
| 742 | putc('\\', tracefile); |
| 743 | putc(c, tracefile); |
| 744 | break; |
| 745 | default: |
| 746 | if (*p >= ' ' && *p <= '~') |
| 747 | putc(*p, tracefile); |
| 748 | else { |
| 749 | putc('\\', tracefile); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 750 | putc((*p >> 6) & 03, tracefile); |
| 751 | putc((*p >> 3) & 07, tracefile); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 752 | putc(*p & 07, tracefile); |
| 753 | } |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | putc('"', tracefile); |
| 758 | } |
| 759 | |
| 760 | static void |
| 761 | trace_puts_args(char **ap) |
| 762 | { |
| 763 | if (debug != 1) |
| 764 | return; |
| 765 | if (!*ap) |
| 766 | return; |
| 767 | while (1) { |
| 768 | trace_puts_quoted(*ap); |
| 769 | if (!*++ap) { |
| 770 | putc('\n', tracefile); |
| 771 | break; |
| 772 | } |
| 773 | putc(' ', tracefile); |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | static void |
| 778 | opentrace(void) |
| 779 | { |
| 780 | char s[100]; |
| 781 | #ifdef O_APPEND |
| 782 | int flags; |
| 783 | #endif |
| 784 | |
| 785 | if (debug != 1) { |
| 786 | if (tracefile) |
| 787 | fflush(tracefile); |
| 788 | /* leave open because libedit might be using it */ |
| 789 | return; |
| 790 | } |
| 791 | strcpy(s, "./trace"); |
| 792 | if (tracefile) { |
| 793 | if (!freopen(s, "a", tracefile)) { |
| 794 | fprintf(stderr, "Can't re-open %s\n", s); |
| 795 | debug = 0; |
| 796 | return; |
| 797 | } |
| 798 | } else { |
| 799 | tracefile = fopen(s, "a"); |
| 800 | if (tracefile == NULL) { |
| 801 | fprintf(stderr, "Can't open %s\n", s); |
| 802 | debug = 0; |
| 803 | return; |
| 804 | } |
| 805 | } |
| 806 | #ifdef O_APPEND |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 807 | flags = fcntl(fileno(tracefile), F_GETFL); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 808 | if (flags >= 0) |
| 809 | fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); |
| 810 | #endif |
| 811 | setlinebuf(tracefile); |
| 812 | fputs("\nTracing started.\n", tracefile); |
| 813 | } |
| 814 | |
| 815 | static void |
| 816 | indent(int amount, char *pfx, FILE *fp) |
| 817 | { |
| 818 | int i; |
| 819 | |
| 820 | for (i = 0; i < amount; i++) { |
| 821 | if (pfx && i == amount - 1) |
| 822 | fputs(pfx, fp); |
| 823 | putc('\t', fp); |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | /* little circular references here... */ |
| 828 | static void shtree(union node *n, int ind, char *pfx, FILE *fp); |
| 829 | |
| 830 | static void |
| 831 | sharg(union node *arg, FILE *fp) |
| 832 | { |
| 833 | char *p; |
| 834 | struct nodelist *bqlist; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 835 | unsigned char subtype; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 836 | |
| 837 | if (arg->type != NARG) { |
| 838 | out1fmt("<node type %d>\n", arg->type); |
| 839 | abort(); |
| 840 | } |
| 841 | bqlist = arg->narg.backquote; |
| 842 | for (p = arg->narg.text; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 843 | switch ((unsigned char)*p) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 844 | case CTLESC: |
| 845 | putc(*++p, fp); |
| 846 | break; |
| 847 | case CTLVAR: |
| 848 | putc('$', fp); |
| 849 | putc('{', fp); |
| 850 | subtype = *++p; |
| 851 | if (subtype == VSLENGTH) |
| 852 | putc('#', fp); |
| 853 | |
| 854 | while (*p != '=') |
| 855 | putc(*p++, fp); |
| 856 | |
| 857 | if (subtype & VSNUL) |
| 858 | putc(':', fp); |
| 859 | |
| 860 | switch (subtype & VSTYPE) { |
| 861 | case VSNORMAL: |
| 862 | putc('}', fp); |
| 863 | break; |
| 864 | case VSMINUS: |
| 865 | putc('-', fp); |
| 866 | break; |
| 867 | case VSPLUS: |
| 868 | putc('+', fp); |
| 869 | break; |
| 870 | case VSQUESTION: |
| 871 | putc('?', fp); |
| 872 | break; |
| 873 | case VSASSIGN: |
| 874 | putc('=', fp); |
| 875 | break; |
| 876 | case VSTRIMLEFT: |
| 877 | putc('#', fp); |
| 878 | break; |
| 879 | case VSTRIMLEFTMAX: |
| 880 | putc('#', fp); |
| 881 | putc('#', fp); |
| 882 | break; |
| 883 | case VSTRIMRIGHT: |
| 884 | putc('%', fp); |
| 885 | break; |
| 886 | case VSTRIMRIGHTMAX: |
| 887 | putc('%', fp); |
| 888 | putc('%', fp); |
| 889 | break; |
| 890 | case VSLENGTH: |
| 891 | break; |
| 892 | default: |
| 893 | out1fmt("<subtype %d>", subtype); |
| 894 | } |
| 895 | break; |
| 896 | case CTLENDVAR: |
| 897 | putc('}', fp); |
| 898 | break; |
| 899 | case CTLBACKQ: |
| 900 | case CTLBACKQ|CTLQUOTE: |
| 901 | putc('$', fp); |
| 902 | putc('(', fp); |
| 903 | shtree(bqlist->n, -1, NULL, fp); |
| 904 | putc(')', fp); |
| 905 | break; |
| 906 | default: |
| 907 | putc(*p, fp); |
| 908 | break; |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 913 | static void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 914 | shcmd(union node *cmd, FILE *fp) |
| 915 | { |
| 916 | union node *np; |
| 917 | int first; |
| 918 | const char *s; |
| 919 | int dftfd; |
| 920 | |
| 921 | first = 1; |
| 922 | for (np = cmd->ncmd.args; np; np = np->narg.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 923 | if (!first) |
| 924 | putc(' ', fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 925 | sharg(np, fp); |
| 926 | first = 0; |
| 927 | } |
| 928 | for (np = cmd->ncmd.redirect; np; np = np->nfile.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 929 | if (!first) |
| 930 | putc(' ', fp); |
| 931 | dftfd = 0; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 932 | switch (np->nfile.type) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 933 | case NTO: s = ">>"+1; dftfd = 1; break; |
| 934 | case NCLOBBER: s = ">|"; dftfd = 1; break; |
| 935 | case NAPPEND: s = ">>"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 936 | #if ENABLE_ASH_BASH_COMPAT |
| 937 | case NTO2: |
| 938 | #endif |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 939 | case NTOFD: s = ">&"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 940 | case NFROM: s = "<"; break; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 941 | case NFROMFD: s = "<&"; break; |
| 942 | case NFROMTO: s = "<>"; break; |
| 943 | default: s = "*error*"; break; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 944 | } |
| 945 | if (np->nfile.fd != dftfd) |
| 946 | fprintf(fp, "%d", np->nfile.fd); |
| 947 | fputs(s, fp); |
| 948 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) { |
| 949 | fprintf(fp, "%d", np->ndup.dupfd); |
| 950 | } else { |
| 951 | sharg(np->nfile.fname, fp); |
| 952 | } |
| 953 | first = 0; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | static void |
| 958 | shtree(union node *n, int ind, char *pfx, FILE *fp) |
| 959 | { |
| 960 | struct nodelist *lp; |
| 961 | const char *s; |
| 962 | |
| 963 | if (n == NULL) |
| 964 | return; |
| 965 | |
| 966 | indent(ind, pfx, fp); |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 967 | |
| 968 | if (n == NODE_EOF) { |
| 969 | fputs("<EOF>", fp); |
| 970 | return; |
| 971 | } |
| 972 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 973 | switch (n->type) { |
| 974 | case NSEMI: |
| 975 | s = "; "; |
| 976 | goto binop; |
| 977 | case NAND: |
| 978 | s = " && "; |
| 979 | goto binop; |
| 980 | case NOR: |
| 981 | s = " || "; |
| 982 | binop: |
| 983 | shtree(n->nbinary.ch1, ind, NULL, fp); |
| 984 | /* if (ind < 0) */ |
| 985 | fputs(s, fp); |
| 986 | shtree(n->nbinary.ch2, ind, NULL, fp); |
| 987 | break; |
| 988 | case NCMD: |
| 989 | shcmd(n, fp); |
| 990 | if (ind >= 0) |
| 991 | putc('\n', fp); |
| 992 | break; |
| 993 | case NPIPE: |
| 994 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 995 | shtree(lp->n, 0, NULL, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 996 | if (lp->next) |
| 997 | fputs(" | ", fp); |
| 998 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 999 | if (n->npipe.pipe_backgnd) |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1000 | fputs(" &", fp); |
| 1001 | if (ind >= 0) |
| 1002 | putc('\n', fp); |
| 1003 | break; |
| 1004 | default: |
| 1005 | fprintf(fp, "<node type %d>", n->type); |
| 1006 | if (ind >= 0) |
| 1007 | putc('\n', fp); |
| 1008 | break; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | static void |
| 1013 | showtree(union node *n) |
| 1014 | { |
| 1015 | trace_puts("showtree called\n"); |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 1016 | shtree(n, 1, NULL, stderr); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1019 | #endif /* DEBUG */ |
| 1020 | |
| 1021 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1022 | /* ============ Parser data */ |
| 1023 | |
| 1024 | /* |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1025 | * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up. |
| 1026 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1027 | struct strlist { |
| 1028 | struct strlist *next; |
| 1029 | char *text; |
| 1030 | }; |
| 1031 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1032 | struct alias; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1033 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1034 | struct strpush { |
| 1035 | struct strpush *prev; /* preceding string on stack */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1036 | char *prev_string; |
| 1037 | int prev_left_in_line; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1038 | #if ENABLE_ASH_ALIAS |
| 1039 | struct alias *ap; /* if push was associated with an alias */ |
| 1040 | #endif |
| 1041 | char *string; /* remember the string since it may change */ |
| 1042 | }; |
| 1043 | |
| 1044 | struct parsefile { |
| 1045 | struct parsefile *prev; /* preceding file on stack */ |
| 1046 | int linno; /* current line */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 1047 | int pf_fd; /* file descriptor (or -1 if string) */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1048 | int left_in_line; /* number of chars left in this line */ |
| 1049 | int left_in_buffer; /* number of chars left in this buffer past the line */ |
| 1050 | char *next_to_pgetc; /* next char in buffer */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1051 | char *buf; /* input buffer */ |
| 1052 | struct strpush *strpush; /* for pushing strings at this level */ |
| 1053 | struct strpush basestrpush; /* so pushing one is fast */ |
| 1054 | }; |
| 1055 | |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1056 | static struct parsefile basepf; /* top level input file */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 1057 | static struct parsefile *g_parsefile = &basepf; /* current input file */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1058 | static int startlinno; /* line # where last token started */ |
| 1059 | static char *commandname; /* currently executing command */ |
| 1060 | static struct strlist *cmdenviron; /* environment for builtin command */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1061 | static uint8_t exitstatus; /* exit status of last command */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1062 | |
| 1063 | |
| 1064 | /* ============ Message printing */ |
| 1065 | |
| 1066 | static void |
| 1067 | ash_vmsg(const char *msg, va_list ap) |
| 1068 | { |
| 1069 | fprintf(stderr, "%s: ", arg0); |
| 1070 | if (commandname) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1071 | if (strcmp(arg0, commandname)) |
| 1072 | fprintf(stderr, "%s: ", commandname); |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 1073 | if (!iflag || g_parsefile->pf_fd > 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1074 | fprintf(stderr, "line %d: ", startlinno); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1075 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1076 | vfprintf(stderr, msg, ap); |
| 1077 | outcslow('\n', stderr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1078 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1079 | |
| 1080 | /* |
| 1081 | * Exverror is called to raise the error exception. If the second argument |
| 1082 | * is not NULL then error prints an error message using printf style |
| 1083 | * formatting. It then raises the error exception. |
| 1084 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1085 | static void ash_vmsg_and_raise(int, const char *, va_list) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1086 | static void |
| 1087 | ash_vmsg_and_raise(int cond, const char *msg, va_list ap) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1088 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1089 | #if DEBUG |
| 1090 | if (msg) { |
| 1091 | TRACE(("ash_vmsg_and_raise(%d, \"", cond)); |
| 1092 | TRACEV((msg, ap)); |
| 1093 | TRACE(("\") pid=%d\n", getpid())); |
| 1094 | } else |
| 1095 | TRACE(("ash_vmsg_and_raise(%d, NULL) pid=%d\n", cond, getpid())); |
| 1096 | if (msg) |
| 1097 | #endif |
| 1098 | ash_vmsg(msg, ap); |
| 1099 | |
| 1100 | flush_stdout_stderr(); |
| 1101 | raise_exception(cond); |
| 1102 | /* NOTREACHED */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1103 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1104 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1105 | static void ash_msg_and_raise_error(const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1106 | static void |
| 1107 | ash_msg_and_raise_error(const char *msg, ...) |
| 1108 | { |
| 1109 | va_list ap; |
| 1110 | |
| 1111 | va_start(ap, msg); |
| 1112 | ash_vmsg_and_raise(EXERROR, msg, ap); |
| 1113 | /* NOTREACHED */ |
| 1114 | va_end(ap); |
| 1115 | } |
| 1116 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1117 | static void raise_error_syntax(const char *) NORETURN; |
| 1118 | static void |
| 1119 | raise_error_syntax(const char *msg) |
| 1120 | { |
| 1121 | ash_msg_and_raise_error("syntax error: %s", msg); |
| 1122 | /* NOTREACHED */ |
| 1123 | } |
| 1124 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1125 | static void ash_msg_and_raise(int, const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1126 | static void |
| 1127 | ash_msg_and_raise(int cond, const char *msg, ...) |
| 1128 | { |
| 1129 | va_list ap; |
| 1130 | |
| 1131 | va_start(ap, msg); |
| 1132 | ash_vmsg_and_raise(cond, msg, ap); |
| 1133 | /* NOTREACHED */ |
| 1134 | va_end(ap); |
| 1135 | } |
| 1136 | |
| 1137 | /* |
| 1138 | * error/warning routines for external builtins |
| 1139 | */ |
| 1140 | static void |
| 1141 | ash_msg(const char *fmt, ...) |
| 1142 | { |
| 1143 | va_list ap; |
| 1144 | |
| 1145 | va_start(ap, fmt); |
| 1146 | ash_vmsg(fmt, ap); |
| 1147 | va_end(ap); |
| 1148 | } |
| 1149 | |
| 1150 | /* |
| 1151 | * Return a string describing an error. The returned string may be a |
| 1152 | * pointer to a static buffer that will be overwritten on the next call. |
| 1153 | * Action describes the operation that got the error. |
| 1154 | */ |
| 1155 | static const char * |
| 1156 | errmsg(int e, const char *em) |
| 1157 | { |
| 1158 | if (e == ENOENT || e == ENOTDIR) { |
| 1159 | return em; |
| 1160 | } |
| 1161 | return strerror(e); |
| 1162 | } |
| 1163 | |
| 1164 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1165 | /* ============ Memory allocation */ |
| 1166 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1167 | #if 0 |
| 1168 | /* I consider these wrappers nearly useless: |
| 1169 | * ok, they return you to nearest exception handler, but |
| 1170 | * how much memory do you leak in the process, making |
| 1171 | * memory starvation worse? |
| 1172 | */ |
| 1173 | static void * |
| 1174 | ckrealloc(void * p, size_t nbytes) |
| 1175 | { |
| 1176 | p = realloc(p, nbytes); |
| 1177 | if (!p) |
| 1178 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1179 | return p; |
| 1180 | } |
| 1181 | |
| 1182 | static void * |
| 1183 | ckmalloc(size_t nbytes) |
| 1184 | { |
| 1185 | return ckrealloc(NULL, nbytes); |
| 1186 | } |
| 1187 | |
| 1188 | static void * |
| 1189 | ckzalloc(size_t nbytes) |
| 1190 | { |
| 1191 | return memset(ckmalloc(nbytes), 0, nbytes); |
| 1192 | } |
| 1193 | |
| 1194 | static char * |
| 1195 | ckstrdup(const char *s) |
| 1196 | { |
| 1197 | char *p = strdup(s); |
| 1198 | if (!p) |
| 1199 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1200 | return p; |
| 1201 | } |
| 1202 | #else |
| 1203 | /* Using bbox equivalents. They exit if out of memory */ |
| 1204 | # define ckrealloc xrealloc |
| 1205 | # define ckmalloc xmalloc |
| 1206 | # define ckzalloc xzalloc |
| 1207 | # define ckstrdup xstrdup |
| 1208 | #endif |
| 1209 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1210 | /* |
| 1211 | * It appears that grabstackstr() will barf with such alignments |
| 1212 | * because stalloc() will return a string allocated in a new stackblock. |
| 1213 | */ |
| 1214 | #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE) |
| 1215 | enum { |
| 1216 | /* Most machines require the value returned from malloc to be aligned |
| 1217 | * in some way. The following macro will get this right |
| 1218 | * on many machines. */ |
Denys Vlasenko | 0e5e4ea | 2009-10-11 00:36:20 +0200 | [diff] [blame] | 1219 | SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1, |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1220 | /* Minimum size of a block */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1221 | MINSIZE = SHELL_ALIGN(504), |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1222 | }; |
| 1223 | |
| 1224 | struct stack_block { |
| 1225 | struct stack_block *prev; |
| 1226 | char space[MINSIZE]; |
| 1227 | }; |
| 1228 | |
| 1229 | struct stackmark { |
| 1230 | struct stack_block *stackp; |
| 1231 | char *stacknxt; |
| 1232 | size_t stacknleft; |
| 1233 | struct stackmark *marknext; |
| 1234 | }; |
| 1235 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1236 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1237 | struct globals_memstack { |
| 1238 | struct stack_block *g_stackp; // = &stackbase; |
| 1239 | struct stackmark *markp; |
| 1240 | char *g_stacknxt; // = stackbase.space; |
| 1241 | char *sstrend; // = stackbase.space + MINSIZE; |
| 1242 | size_t g_stacknleft; // = MINSIZE; |
| 1243 | int herefd; // = -1; |
| 1244 | struct stack_block stackbase; |
| 1245 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1246 | extern struct globals_memstack *const ash_ptr_to_globals_memstack; |
| 1247 | #define G_memstack (*ash_ptr_to_globals_memstack) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1248 | #define g_stackp (G_memstack.g_stackp ) |
| 1249 | #define markp (G_memstack.markp ) |
| 1250 | #define g_stacknxt (G_memstack.g_stacknxt ) |
| 1251 | #define sstrend (G_memstack.sstrend ) |
| 1252 | #define g_stacknleft (G_memstack.g_stacknleft) |
| 1253 | #define herefd (G_memstack.herefd ) |
| 1254 | #define stackbase (G_memstack.stackbase ) |
| 1255 | #define INIT_G_memstack() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1256 | (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \ |
| 1257 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1258 | g_stackp = &stackbase; \ |
| 1259 | g_stacknxt = stackbase.space; \ |
| 1260 | g_stacknleft = MINSIZE; \ |
| 1261 | sstrend = stackbase.space + MINSIZE; \ |
| 1262 | herefd = -1; \ |
| 1263 | } while (0) |
| 1264 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1265 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1266 | #define stackblock() ((void *)g_stacknxt) |
| 1267 | #define stackblocksize() g_stacknleft |
| 1268 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1269 | /* |
| 1270 | * Parse trees for commands are allocated in lifo order, so we use a stack |
| 1271 | * to make this more efficient, and also to avoid all sorts of exception |
| 1272 | * handling code to handle interrupts in the middle of a parse. |
| 1273 | * |
| 1274 | * The size 504 was chosen because the Ultrix malloc handles that size |
| 1275 | * well. |
| 1276 | */ |
| 1277 | static void * |
| 1278 | stalloc(size_t nbytes) |
| 1279 | { |
| 1280 | char *p; |
| 1281 | size_t aligned; |
| 1282 | |
| 1283 | aligned = SHELL_ALIGN(nbytes); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1284 | if (aligned > g_stacknleft) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1285 | size_t len; |
| 1286 | size_t blocksize; |
| 1287 | struct stack_block *sp; |
| 1288 | |
| 1289 | blocksize = aligned; |
| 1290 | if (blocksize < MINSIZE) |
| 1291 | blocksize = MINSIZE; |
| 1292 | len = sizeof(struct stack_block) - MINSIZE + blocksize; |
| 1293 | if (len < blocksize) |
| 1294 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1295 | INT_OFF; |
| 1296 | sp = ckmalloc(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1297 | sp->prev = g_stackp; |
| 1298 | g_stacknxt = sp->space; |
| 1299 | g_stacknleft = blocksize; |
| 1300 | sstrend = g_stacknxt + blocksize; |
| 1301 | g_stackp = sp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1302 | INT_ON; |
| 1303 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1304 | p = g_stacknxt; |
| 1305 | g_stacknxt += aligned; |
| 1306 | g_stacknleft -= aligned; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1307 | return p; |
| 1308 | } |
| 1309 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 1310 | static void * |
| 1311 | stzalloc(size_t nbytes) |
| 1312 | { |
| 1313 | return memset(stalloc(nbytes), 0, nbytes); |
| 1314 | } |
| 1315 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1316 | static void |
| 1317 | stunalloc(void *p) |
| 1318 | { |
| 1319 | #if DEBUG |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1320 | if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 1321 | write(STDERR_FILENO, "stunalloc\n", 10); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1322 | abort(); |
| 1323 | } |
| 1324 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1325 | g_stacknleft += g_stacknxt - (char *)p; |
| 1326 | g_stacknxt = p; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1329 | /* |
| 1330 | * Like strdup but works with the ash stack. |
| 1331 | */ |
| 1332 | static char * |
| 1333 | ststrdup(const char *p) |
| 1334 | { |
| 1335 | size_t len = strlen(p) + 1; |
| 1336 | return memcpy(stalloc(len), p, len); |
| 1337 | } |
| 1338 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1339 | static void |
| 1340 | setstackmark(struct stackmark *mark) |
| 1341 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1342 | mark->stackp = g_stackp; |
| 1343 | mark->stacknxt = g_stacknxt; |
| 1344 | mark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1345 | mark->marknext = markp; |
| 1346 | markp = mark; |
| 1347 | } |
| 1348 | |
| 1349 | static void |
| 1350 | popstackmark(struct stackmark *mark) |
| 1351 | { |
| 1352 | struct stack_block *sp; |
| 1353 | |
Denis Vlasenko | 93ebd4f | 2007-03-13 20:55:36 +0000 | [diff] [blame] | 1354 | if (!mark->stackp) |
| 1355 | return; |
| 1356 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1357 | INT_OFF; |
| 1358 | markp = mark->marknext; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1359 | while (g_stackp != mark->stackp) { |
| 1360 | sp = g_stackp; |
| 1361 | g_stackp = sp->prev; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1362 | free(sp); |
| 1363 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1364 | g_stacknxt = mark->stacknxt; |
| 1365 | g_stacknleft = mark->stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1366 | sstrend = mark->stacknxt + mark->stacknleft; |
| 1367 | INT_ON; |
| 1368 | } |
| 1369 | |
| 1370 | /* |
| 1371 | * When the parser reads in a string, it wants to stick the string on the |
| 1372 | * stack and only adjust the stack pointer when it knows how big the |
| 1373 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
| 1374 | * of space on top of the stack and stackblocklen returns the length of |
| 1375 | * this block. Growstackblock will grow this space by at least one byte, |
| 1376 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
| 1377 | * part of the block that has been used. |
| 1378 | */ |
| 1379 | static void |
| 1380 | growstackblock(void) |
| 1381 | { |
| 1382 | size_t newlen; |
| 1383 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1384 | newlen = g_stacknleft * 2; |
| 1385 | if (newlen < g_stacknleft) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1386 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1387 | if (newlen < 128) |
| 1388 | newlen += 128; |
| 1389 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1390 | if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1391 | struct stack_block *oldstackp; |
| 1392 | struct stackmark *xmark; |
| 1393 | struct stack_block *sp; |
| 1394 | struct stack_block *prevstackp; |
| 1395 | size_t grosslen; |
| 1396 | |
| 1397 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1398 | oldstackp = g_stackp; |
| 1399 | sp = g_stackp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1400 | prevstackp = sp->prev; |
| 1401 | grosslen = newlen + sizeof(struct stack_block) - MINSIZE; |
| 1402 | sp = ckrealloc(sp, grosslen); |
| 1403 | sp->prev = prevstackp; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1404 | g_stackp = sp; |
| 1405 | g_stacknxt = sp->space; |
| 1406 | g_stacknleft = newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1407 | sstrend = sp->space + newlen; |
| 1408 | |
| 1409 | /* |
| 1410 | * Stack marks pointing to the start of the old block |
| 1411 | * must be relocated to point to the new block |
| 1412 | */ |
| 1413 | xmark = markp; |
| 1414 | while (xmark != NULL && xmark->stackp == oldstackp) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1415 | xmark->stackp = g_stackp; |
| 1416 | xmark->stacknxt = g_stacknxt; |
| 1417 | xmark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1418 | xmark = xmark->marknext; |
| 1419 | } |
| 1420 | INT_ON; |
| 1421 | } else { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1422 | char *oldspace = g_stacknxt; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1423 | size_t oldlen = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1424 | char *p = stalloc(newlen); |
| 1425 | |
| 1426 | /* free the space we just allocated */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1427 | g_stacknxt = memcpy(p, oldspace, oldlen); |
| 1428 | g_stacknleft += newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | static void |
| 1433 | grabstackblock(size_t len) |
| 1434 | { |
| 1435 | len = SHELL_ALIGN(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1436 | g_stacknxt += len; |
| 1437 | g_stacknleft -= len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | /* |
| 1441 | * The following routines are somewhat easier to use than the above. |
| 1442 | * The user declares a variable of type STACKSTR, which may be declared |
| 1443 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
| 1444 | * the user uses the macro STPUTC to add characters to the string. In |
| 1445 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
| 1446 | * grown as necessary. When the user is done, she can just leave the |
| 1447 | * string there and refer to it using stackblock(). Or she can allocate |
| 1448 | * the space for it using grabstackstr(). If it is necessary to allow |
| 1449 | * someone else to use the stack temporarily and then continue to grow |
| 1450 | * the string, the user should use grabstack to allocate the space, and |
| 1451 | * then call ungrabstr(p) to return to the previous mode of operation. |
| 1452 | * |
| 1453 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
| 1454 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
| 1455 | * is space for at least one character. |
| 1456 | */ |
| 1457 | static void * |
| 1458 | growstackstr(void) |
| 1459 | { |
| 1460 | size_t len = stackblocksize(); |
| 1461 | if (herefd >= 0 && len >= 1024) { |
| 1462 | full_write(herefd, stackblock(), len); |
| 1463 | return stackblock(); |
| 1464 | } |
| 1465 | growstackblock(); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1466 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | /* |
| 1470 | * Called from CHECKSTRSPACE. |
| 1471 | */ |
| 1472 | static char * |
| 1473 | makestrspace(size_t newlen, char *p) |
| 1474 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1475 | size_t len = p - g_stacknxt; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1476 | size_t size = stackblocksize(); |
| 1477 | |
| 1478 | for (;;) { |
| 1479 | size_t nleft; |
| 1480 | |
| 1481 | size = stackblocksize(); |
| 1482 | nleft = size - len; |
| 1483 | if (nleft >= newlen) |
| 1484 | break; |
| 1485 | growstackblock(); |
| 1486 | } |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1487 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | static char * |
| 1491 | stack_nputstr(const char *s, size_t n, char *p) |
| 1492 | { |
| 1493 | p = makestrspace(n, p); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1494 | p = (char *)memcpy(p, s, n) + n; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1495 | return p; |
| 1496 | } |
| 1497 | |
| 1498 | static char * |
| 1499 | stack_putstr(const char *s, char *p) |
| 1500 | { |
| 1501 | return stack_nputstr(s, strlen(s), p); |
| 1502 | } |
| 1503 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1504 | static char * |
| 1505 | _STPUTC(int c, char *p) |
| 1506 | { |
| 1507 | if (p == sstrend) |
| 1508 | p = growstackstr(); |
| 1509 | *p++ = c; |
| 1510 | return p; |
| 1511 | } |
| 1512 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1513 | #define STARTSTACKSTR(p) ((p) = stackblock()) |
| 1514 | #define STPUTC(c, p) ((p) = _STPUTC((c), (p))) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1515 | #define CHECKSTRSPACE(n, p) do { \ |
| 1516 | char *q = (p); \ |
| 1517 | size_t l = (n); \ |
| 1518 | size_t m = sstrend - q; \ |
| 1519 | if (l > m) \ |
| 1520 | (p) = makestrspace(l, q); \ |
| 1521 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1522 | #define USTPUTC(c, p) (*(p)++ = (c)) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1523 | #define STACKSTRNUL(p) do { \ |
| 1524 | if ((p) == sstrend) \ |
| 1525 | (p) = growstackstr(); \ |
| 1526 | *(p) = '\0'; \ |
| 1527 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1528 | #define STUNPUTC(p) (--(p)) |
| 1529 | #define STTOPC(p) ((p)[-1]) |
| 1530 | #define STADJUST(amount, p) ((p) += (amount)) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1531 | |
| 1532 | #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock()) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1533 | #define ungrabstackstr(s, p) stunalloc(s) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1534 | #define stackstrend() ((void *)sstrend) |
| 1535 | |
| 1536 | |
| 1537 | /* ============ String helpers */ |
| 1538 | |
| 1539 | /* |
| 1540 | * prefix -- see if pfx is a prefix of string. |
| 1541 | */ |
| 1542 | static char * |
| 1543 | prefix(const char *string, const char *pfx) |
| 1544 | { |
| 1545 | while (*pfx) { |
| 1546 | if (*pfx++ != *string++) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 1547 | return NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1548 | } |
| 1549 | return (char *) string; |
| 1550 | } |
| 1551 | |
| 1552 | /* |
| 1553 | * Check for a valid number. This should be elsewhere. |
| 1554 | */ |
| 1555 | static int |
| 1556 | is_number(const char *p) |
| 1557 | { |
| 1558 | do { |
| 1559 | if (!isdigit(*p)) |
| 1560 | return 0; |
| 1561 | } while (*++p != '\0'); |
| 1562 | return 1; |
| 1563 | } |
| 1564 | |
| 1565 | /* |
| 1566 | * Convert a string of digits to an integer, printing an error message on |
| 1567 | * failure. |
| 1568 | */ |
| 1569 | static int |
| 1570 | number(const char *s) |
| 1571 | { |
| 1572 | if (!is_number(s)) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 1573 | ash_msg_and_raise_error(msg_illnum, s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1574 | return atoi(s); |
| 1575 | } |
| 1576 | |
| 1577 | /* |
| 1578 | * Produce a possibly single quoted string suitable as input to the shell. |
| 1579 | * The return string is allocated on the stack. |
| 1580 | */ |
| 1581 | static char * |
| 1582 | single_quote(const char *s) |
| 1583 | { |
| 1584 | char *p; |
| 1585 | |
| 1586 | STARTSTACKSTR(p); |
| 1587 | |
| 1588 | do { |
| 1589 | char *q; |
| 1590 | size_t len; |
| 1591 | |
| 1592 | len = strchrnul(s, '\'') - s; |
| 1593 | |
| 1594 | q = p = makestrspace(len + 3, p); |
| 1595 | |
| 1596 | *q++ = '\''; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1597 | q = (char *)memcpy(q, s, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1598 | *q++ = '\''; |
| 1599 | s += len; |
| 1600 | |
| 1601 | STADJUST(q - p, p); |
| 1602 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1603 | if (*s != '\'') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1604 | break; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1605 | len = 0; |
| 1606 | do len++; while (*++s == '\''); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1607 | |
| 1608 | q = p = makestrspace(len + 3, p); |
| 1609 | |
| 1610 | *q++ = '"'; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1611 | q = (char *)memcpy(q, s - len, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1612 | *q++ = '"'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1613 | |
| 1614 | STADJUST(q - p, p); |
| 1615 | } while (*s); |
| 1616 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1617 | USTPUTC('\0', p); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1618 | |
| 1619 | return stackblock(); |
| 1620 | } |
| 1621 | |
| 1622 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1623 | /* ============ nextopt */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1624 | |
| 1625 | static char **argptr; /* argument list for builtin commands */ |
| 1626 | static char *optionarg; /* set by nextopt (like getopt) */ |
| 1627 | static char *optptr; /* used by nextopt */ |
| 1628 | |
| 1629 | /* |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1630 | * XXX - should get rid of. Have all builtins use getopt(3). |
| 1631 | * The library getopt must have the BSD extension static variable |
| 1632 | * "optreset", otherwise it can't be used within the shell safely. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1633 | * |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1634 | * Standard option processing (a la getopt) for builtin routines. |
| 1635 | * The only argument that is passed to nextopt is the option string; |
| 1636 | * the other arguments are unnecessary. It returns the character, |
| 1637 | * or '\0' on end of input. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1638 | */ |
| 1639 | static int |
| 1640 | nextopt(const char *optstring) |
| 1641 | { |
| 1642 | char *p; |
| 1643 | const char *q; |
| 1644 | char c; |
| 1645 | |
| 1646 | p = optptr; |
| 1647 | if (p == NULL || *p == '\0') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1648 | /* We ate entire "-param", take next one */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1649 | p = *argptr; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1650 | if (p == NULL) |
| 1651 | return '\0'; |
| 1652 | if (*p != '-') |
| 1653 | return '\0'; |
| 1654 | if (*++p == '\0') /* just "-" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1655 | return '\0'; |
| 1656 | argptr++; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1657 | if (LONE_DASH(p)) /* "--" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1658 | return '\0'; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1659 | /* p => next "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1660 | } |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1661 | /* p => some option char in the middle of a "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1662 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 1663 | for (q = optstring; *q != c;) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1664 | if (*q == '\0') |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1665 | ash_msg_and_raise_error("illegal option -%c", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1666 | if (*++q == ':') |
| 1667 | q++; |
| 1668 | } |
| 1669 | if (*++q == ':') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1670 | if (*p == '\0') { |
| 1671 | p = *argptr++; |
| 1672 | if (p == NULL) |
| 1673 | ash_msg_and_raise_error("no arg for -%c option", c); |
| 1674 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1675 | optionarg = p; |
| 1676 | p = NULL; |
| 1677 | } |
| 1678 | optptr = p; |
| 1679 | return c; |
| 1680 | } |
| 1681 | |
| 1682 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1683 | /* ============ Shell variables */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1684 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1685 | /* |
| 1686 | * The parsefile structure pointed to by the global variable parsefile |
| 1687 | * contains information about the current file being read. |
| 1688 | */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1689 | struct shparam { |
| 1690 | int nparam; /* # of positional parameters (without $0) */ |
| 1691 | #if ENABLE_ASH_GETOPTS |
| 1692 | int optind; /* next parameter to be processed by getopts */ |
| 1693 | int optoff; /* used by getopts */ |
| 1694 | #endif |
| 1695 | unsigned char malloced; /* if parameter list dynamically allocated */ |
| 1696 | char **p; /* parameter list */ |
| 1697 | }; |
| 1698 | |
| 1699 | /* |
| 1700 | * Free the list of positional parameters. |
| 1701 | */ |
| 1702 | static void |
| 1703 | freeparam(volatile struct shparam *param) |
| 1704 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1705 | if (param->malloced) { |
Denis Vlasenko | 3177ba0 | 2008-07-13 20:39:23 +0000 | [diff] [blame] | 1706 | char **ap, **ap1; |
| 1707 | ap = ap1 = param->p; |
| 1708 | while (*ap) |
| 1709 | free(*ap++); |
| 1710 | free(ap1); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1715 | static void FAST_FUNC getoptsreset(const char *value); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1716 | #endif |
| 1717 | |
| 1718 | struct var { |
| 1719 | struct var *next; /* next entry in hash list */ |
| 1720 | int flags; /* flags are defined above */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1721 | const char *var_text; /* name=value */ |
| 1722 | void (*var_func)(const char *) FAST_FUNC; /* function to be called when */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1723 | /* the variable gets set/unset */ |
| 1724 | }; |
| 1725 | |
| 1726 | struct localvar { |
| 1727 | struct localvar *next; /* next local variable in list */ |
| 1728 | struct var *vp; /* the variable that was made local */ |
| 1729 | int flags; /* saved flags */ |
| 1730 | const char *text; /* saved text */ |
| 1731 | }; |
| 1732 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1733 | /* flags */ |
| 1734 | #define VEXPORT 0x01 /* variable is exported */ |
| 1735 | #define VREADONLY 0x02 /* variable cannot be modified */ |
| 1736 | #define VSTRFIXED 0x04 /* variable struct is statically allocated */ |
| 1737 | #define VTEXTFIXED 0x08 /* text is statically allocated */ |
| 1738 | #define VSTACK 0x10 /* text is allocated on the stack */ |
| 1739 | #define VUNSET 0x20 /* the variable is not set */ |
| 1740 | #define VNOFUNC 0x40 /* don't call the callback function */ |
| 1741 | #define VNOSET 0x80 /* do not set variable - just readonly test */ |
| 1742 | #define VNOSAVE 0x100 /* when text is on the heap before setvareq */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1743 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1744 | # define VDYNAMIC 0x200 /* dynamic variable */ |
| 1745 | #else |
| 1746 | # define VDYNAMIC 0 |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1747 | #endif |
| 1748 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1749 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1750 | /* Need to be before varinit_data[] */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1751 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 1752 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1753 | change_lc_all(const char *value) |
| 1754 | { |
| 1755 | if (value && *value != '\0') |
| 1756 | setlocale(LC_ALL, value); |
| 1757 | } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 1758 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1759 | change_lc_ctype(const char *value) |
| 1760 | { |
| 1761 | if (value && *value != '\0') |
| 1762 | setlocale(LC_CTYPE, value); |
| 1763 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1764 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1765 | #if ENABLE_ASH_MAIL |
| 1766 | static void chkmail(void); |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1767 | static void changemail(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1768 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1769 | static void changepath(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1770 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1771 | static void change_random(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1772 | #endif |
| 1773 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1774 | static const struct { |
| 1775 | int flags; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1776 | const char *var_text; |
| 1777 | void (*var_func)(const char *) FAST_FUNC; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1778 | } varinit_data[] = { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1779 | { VSTRFIXED|VTEXTFIXED , defifsvar , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1780 | #if ENABLE_ASH_MAIL |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1781 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL" , changemail }, |
| 1782 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH" , changemail }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1783 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1784 | { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath }, |
| 1785 | { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL }, |
| 1786 | { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL }, |
| 1787 | { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1788 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1789 | { VSTRFIXED|VTEXTFIXED , "OPTIND=1" , getoptsreset }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1790 | #endif |
| 1791 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1792 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM", change_random }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1793 | #endif |
| 1794 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1795 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL" , change_lc_all }, |
| 1796 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE" , change_lc_ctype }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1797 | #endif |
| 1798 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1799 | { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE" , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1800 | #endif |
| 1801 | }; |
| 1802 | |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1803 | struct redirtab; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1804 | |
| 1805 | struct globals_var { |
| 1806 | struct shparam shellparam; /* $@ current positional parameters */ |
| 1807 | struct redirtab *redirlist; |
| 1808 | int g_nullredirs; |
| 1809 | int preverrout_fd; /* save fd2 before print debug if xflag is set. */ |
| 1810 | struct var *vartab[VTABSIZE]; |
| 1811 | struct var varinit[ARRAY_SIZE(varinit_data)]; |
| 1812 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1813 | extern struct globals_var *const ash_ptr_to_globals_var; |
| 1814 | #define G_var (*ash_ptr_to_globals_var) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1815 | #define shellparam (G_var.shellparam ) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1816 | //#define redirlist (G_var.redirlist ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1817 | #define g_nullredirs (G_var.g_nullredirs ) |
| 1818 | #define preverrout_fd (G_var.preverrout_fd) |
| 1819 | #define vartab (G_var.vartab ) |
| 1820 | #define varinit (G_var.varinit ) |
| 1821 | #define INIT_G_var() do { \ |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1822 | unsigned i; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1823 | (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \ |
| 1824 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1825 | for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1826 | varinit[i].flags = varinit_data[i].flags; \ |
| 1827 | varinit[i].var_text = varinit_data[i].var_text; \ |
| 1828 | varinit[i].var_func = varinit_data[i].var_func; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1829 | } \ |
| 1830 | } while (0) |
| 1831 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1832 | #define vifs varinit[0] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1833 | #if ENABLE_ASH_MAIL |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1834 | # define vmail (&vifs)[1] |
| 1835 | # define vmpath (&vmail)[1] |
| 1836 | # define vpath (&vmpath)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1837 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1838 | # define vpath (&vifs)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1839 | #endif |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1840 | #define vps1 (&vpath)[1] |
| 1841 | #define vps2 (&vps1)[1] |
| 1842 | #define vps4 (&vps2)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1843 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1844 | # define voptind (&vps4)[1] |
| 1845 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1846 | # define vrandom (&voptind)[1] |
| 1847 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1848 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1849 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1850 | # define vrandom (&vps4)[1] |
| 1851 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1852 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1853 | |
| 1854 | /* |
| 1855 | * The following macros access the values of the above variables. |
| 1856 | * They have to skip over the name. They return the null string |
| 1857 | * for unset variables. |
| 1858 | */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1859 | #define ifsval() (vifs.var_text + 4) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1860 | #define ifsset() ((vifs.flags & VUNSET) == 0) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1861 | #if ENABLE_ASH_MAIL |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1862 | # define mailval() (vmail.var_text + 5) |
| 1863 | # define mpathval() (vmpath.var_text + 9) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1864 | # define mpathset() ((vmpath.flags & VUNSET) == 0) |
| 1865 | #endif |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1866 | #define pathval() (vpath.var_text + 5) |
| 1867 | #define ps1val() (vps1.var_text + 4) |
| 1868 | #define ps2val() (vps2.var_text + 4) |
| 1869 | #define ps4val() (vps4.var_text + 4) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1870 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1871 | # define optindval() (voptind.var_text + 7) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1872 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1873 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1874 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1875 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1876 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 1877 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1878 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1879 | static void FAST_FUNC |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1880 | getoptsreset(const char *value) |
| 1881 | { |
| 1882 | shellparam.optind = number(value); |
| 1883 | shellparam.optoff = -1; |
| 1884 | } |
| 1885 | #endif |
| 1886 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1887 | /* |
| 1888 | * Return of a legal variable name (a letter or underscore followed by zero or |
| 1889 | * more letters, underscores, and digits). |
| 1890 | */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1891 | static char* FAST_FUNC |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1892 | endofname(const char *name) |
| 1893 | { |
| 1894 | char *p; |
| 1895 | |
| 1896 | p = (char *) name; |
| 1897 | if (!is_name(*p)) |
| 1898 | return p; |
| 1899 | while (*++p) { |
| 1900 | if (!is_in_name(*p)) |
| 1901 | break; |
| 1902 | } |
| 1903 | return p; |
| 1904 | } |
| 1905 | |
| 1906 | /* |
| 1907 | * Compares two strings up to the first = or '\0'. The first |
| 1908 | * string must be terminated by '='; the second may be terminated by |
| 1909 | * either '=' or '\0'. |
| 1910 | */ |
| 1911 | static int |
| 1912 | varcmp(const char *p, const char *q) |
| 1913 | { |
| 1914 | int c, d; |
| 1915 | |
| 1916 | while ((c = *p) == (d = *q)) { |
| 1917 | if (!c || c == '=') |
| 1918 | goto out; |
| 1919 | p++; |
| 1920 | q++; |
| 1921 | } |
| 1922 | if (c == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 1923 | c = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1924 | if (d == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 1925 | d = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1926 | out: |
| 1927 | return c - d; |
| 1928 | } |
| 1929 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1930 | /* |
| 1931 | * Find the appropriate entry in the hash table from the name. |
| 1932 | */ |
| 1933 | static struct var ** |
| 1934 | hashvar(const char *p) |
| 1935 | { |
| 1936 | unsigned hashval; |
| 1937 | |
| 1938 | hashval = ((unsigned char) *p) << 4; |
| 1939 | while (*p && *p != '=') |
| 1940 | hashval += (unsigned char) *p++; |
| 1941 | return &vartab[hashval % VTABSIZE]; |
| 1942 | } |
| 1943 | |
| 1944 | static int |
| 1945 | vpcmp(const void *a, const void *b) |
| 1946 | { |
| 1947 | return varcmp(*(const char **)a, *(const char **)b); |
| 1948 | } |
| 1949 | |
| 1950 | /* |
| 1951 | * This routine initializes the builtin variables. |
| 1952 | */ |
| 1953 | static void |
| 1954 | initvar(void) |
| 1955 | { |
| 1956 | struct var *vp; |
| 1957 | struct var *end; |
| 1958 | struct var **vpp; |
| 1959 | |
| 1960 | /* |
| 1961 | * PS1 depends on uid |
| 1962 | */ |
| 1963 | #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1964 | vps1.var_text = "PS1=\\w \\$ "; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1965 | #else |
| 1966 | if (!geteuid()) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1967 | vps1.var_text = "PS1=# "; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1968 | #endif |
| 1969 | vp = varinit; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 1970 | end = vp + ARRAY_SIZE(varinit); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1971 | do { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1972 | vpp = hashvar(vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1973 | vp->next = *vpp; |
| 1974 | *vpp = vp; |
| 1975 | } while (++vp < end); |
| 1976 | } |
| 1977 | |
| 1978 | static struct var ** |
| 1979 | findvar(struct var **vpp, const char *name) |
| 1980 | { |
| 1981 | for (; *vpp; vpp = &(*vpp)->next) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1982 | if (varcmp((*vpp)->var_text, name) == 0) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1983 | break; |
| 1984 | } |
| 1985 | } |
| 1986 | return vpp; |
| 1987 | } |
| 1988 | |
| 1989 | /* |
| 1990 | * Find the value of a variable. Returns NULL if not set. |
| 1991 | */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1992 | static const char* FAST_FUNC |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1993 | lookupvar(const char *name) |
| 1994 | { |
| 1995 | struct var *v; |
| 1996 | |
| 1997 | v = *findvar(hashvar(name), name); |
| 1998 | if (v) { |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1999 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2000 | /* |
| 2001 | * Dynamic variables are implemented roughly the same way they are |
| 2002 | * in bash. Namely, they're "special" so long as they aren't unset. |
| 2003 | * As soon as they're unset, they're no longer dynamic, and dynamic |
| 2004 | * lookup will no longer happen at that point. -- PFM. |
| 2005 | */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2006 | if (v->flags & VDYNAMIC) |
| 2007 | v->var_func(NULL); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2008 | #endif |
| 2009 | if (!(v->flags & VUNSET)) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2010 | return var_end(v->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2011 | } |
| 2012 | return NULL; |
| 2013 | } |
| 2014 | |
| 2015 | /* |
| 2016 | * Search the environment of a builtin command. |
| 2017 | */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2018 | static const char * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2019 | bltinlookup(const char *name) |
| 2020 | { |
| 2021 | struct strlist *sp; |
| 2022 | |
| 2023 | for (sp = cmdenviron; sp; sp = sp->next) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2024 | if (varcmp(sp->text, name) == 0) |
| 2025 | return var_end(sp->text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2026 | } |
| 2027 | return lookupvar(name); |
| 2028 | } |
| 2029 | |
| 2030 | /* |
| 2031 | * Same as setvar except that the variable and value are passed in |
| 2032 | * the first argument as name=value. Since the first argument will |
| 2033 | * be actually stored in the table, it should not be a string that |
| 2034 | * will go away. |
| 2035 | * Called with interrupts off. |
| 2036 | */ |
| 2037 | static void |
| 2038 | setvareq(char *s, int flags) |
| 2039 | { |
| 2040 | struct var *vp, **vpp; |
| 2041 | |
| 2042 | vpp = hashvar(s); |
| 2043 | flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1)); |
| 2044 | vp = *findvar(vpp, s); |
| 2045 | if (vp) { |
| 2046 | if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) { |
| 2047 | const char *n; |
| 2048 | |
| 2049 | if (flags & VNOSAVE) |
| 2050 | free(s); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2051 | n = vp->var_text; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2052 | ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n); |
| 2053 | } |
| 2054 | |
| 2055 | if (flags & VNOSET) |
| 2056 | return; |
| 2057 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2058 | if (vp->var_func && !(flags & VNOFUNC)) |
| 2059 | vp->var_func(var_end(s)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2060 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2061 | if (!(vp->flags & (VTEXTFIXED|VSTACK))) |
| 2062 | free((char*)vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2063 | |
| 2064 | flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET); |
| 2065 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2066 | /* variable s is not found */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2067 | if (flags & VNOSET) |
| 2068 | return; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2069 | vp = ckzalloc(sizeof(*vp)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2070 | vp->next = *vpp; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2071 | /*vp->func = NULL; - ckzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2072 | *vpp = vp; |
| 2073 | } |
| 2074 | if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE))) |
| 2075 | s = ckstrdup(s); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2076 | vp->var_text = s; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2077 | vp->flags = flags; |
| 2078 | } |
| 2079 | |
| 2080 | /* |
| 2081 | * Set the value of a variable. The flags argument is ored with the |
| 2082 | * flags of the variable. If val is NULL, the variable is unset. |
| 2083 | */ |
| 2084 | static void |
| 2085 | setvar(const char *name, const char *val, int flags) |
| 2086 | { |
| 2087 | char *p, *q; |
| 2088 | size_t namelen; |
| 2089 | char *nameeq; |
| 2090 | size_t vallen; |
| 2091 | |
| 2092 | q = endofname(name); |
| 2093 | p = strchrnul(q, '='); |
| 2094 | namelen = p - name; |
| 2095 | if (!namelen || p != q) |
| 2096 | ash_msg_and_raise_error("%.*s: bad variable name", namelen, name); |
| 2097 | vallen = 0; |
| 2098 | if (val == NULL) { |
| 2099 | flags |= VUNSET; |
| 2100 | } else { |
| 2101 | vallen = strlen(val); |
| 2102 | } |
| 2103 | INT_OFF; |
| 2104 | nameeq = ckmalloc(namelen + vallen + 2); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2105 | p = (char *)memcpy(nameeq, name, namelen) + namelen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2106 | if (val) { |
| 2107 | *p++ = '='; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2108 | p = (char *)memcpy(p, val, vallen) + vallen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2109 | } |
| 2110 | *p = '\0'; |
| 2111 | setvareq(nameeq, flags | VNOSAVE); |
| 2112 | INT_ON; |
| 2113 | } |
| 2114 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2115 | static void FAST_FUNC |
| 2116 | setvar2(const char *name, const char *val) |
| 2117 | { |
| 2118 | setvar(name, val, 0); |
| 2119 | } |
| 2120 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2121 | #if ENABLE_ASH_GETOPTS |
| 2122 | /* |
| 2123 | * Safe version of setvar, returns 1 on success 0 on failure. |
| 2124 | */ |
| 2125 | static int |
| 2126 | setvarsafe(const char *name, const char *val, int flags) |
| 2127 | { |
| 2128 | int err; |
| 2129 | volatile int saveint; |
| 2130 | struct jmploc *volatile savehandler = exception_handler; |
| 2131 | struct jmploc jmploc; |
| 2132 | |
| 2133 | SAVE_INT(saveint); |
| 2134 | if (setjmp(jmploc.loc)) |
| 2135 | err = 1; |
| 2136 | else { |
| 2137 | exception_handler = &jmploc; |
| 2138 | setvar(name, val, flags); |
| 2139 | err = 0; |
| 2140 | } |
| 2141 | exception_handler = savehandler; |
| 2142 | RESTORE_INT(saveint); |
| 2143 | return err; |
| 2144 | } |
| 2145 | #endif |
| 2146 | |
| 2147 | /* |
| 2148 | * Unset the specified variable. |
| 2149 | */ |
| 2150 | static int |
| 2151 | unsetvar(const char *s) |
| 2152 | { |
| 2153 | struct var **vpp; |
| 2154 | struct var *vp; |
| 2155 | int retval; |
| 2156 | |
| 2157 | vpp = findvar(hashvar(s), s); |
| 2158 | vp = *vpp; |
| 2159 | retval = 2; |
| 2160 | if (vp) { |
| 2161 | int flags = vp->flags; |
| 2162 | |
| 2163 | retval = 1; |
| 2164 | if (flags & VREADONLY) |
| 2165 | goto out; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 2166 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2167 | vp->flags &= ~VDYNAMIC; |
| 2168 | #endif |
| 2169 | if (flags & VUNSET) |
| 2170 | goto ok; |
| 2171 | if ((flags & VSTRFIXED) == 0) { |
| 2172 | INT_OFF; |
| 2173 | if ((flags & (VTEXTFIXED|VSTACK)) == 0) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2174 | free((char*)vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2175 | *vpp = vp->next; |
| 2176 | free(vp); |
| 2177 | INT_ON; |
| 2178 | } else { |
| 2179 | setvar(s, 0, 0); |
| 2180 | vp->flags &= ~VEXPORT; |
| 2181 | } |
| 2182 | ok: |
| 2183 | retval = 0; |
| 2184 | } |
| 2185 | out: |
| 2186 | return retval; |
| 2187 | } |
| 2188 | |
| 2189 | /* |
| 2190 | * Process a linked list of variable assignments. |
| 2191 | */ |
| 2192 | static void |
| 2193 | listsetvar(struct strlist *list_set_var, int flags) |
| 2194 | { |
| 2195 | struct strlist *lp = list_set_var; |
| 2196 | |
| 2197 | if (!lp) |
| 2198 | return; |
| 2199 | INT_OFF; |
| 2200 | do { |
| 2201 | setvareq(lp->text, flags); |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2202 | lp = lp->next; |
| 2203 | } while (lp); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2204 | INT_ON; |
| 2205 | } |
| 2206 | |
| 2207 | /* |
| 2208 | * Generate a list of variables satisfying the given conditions. |
| 2209 | */ |
| 2210 | static char ** |
| 2211 | listvars(int on, int off, char ***end) |
| 2212 | { |
| 2213 | struct var **vpp; |
| 2214 | struct var *vp; |
| 2215 | char **ep; |
| 2216 | int mask; |
| 2217 | |
| 2218 | STARTSTACKSTR(ep); |
| 2219 | vpp = vartab; |
| 2220 | mask = on | off; |
| 2221 | do { |
| 2222 | for (vp = *vpp; vp; vp = vp->next) { |
| 2223 | if ((vp->flags & mask) == on) { |
| 2224 | if (ep == stackstrend()) |
| 2225 | ep = growstackstr(); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2226 | *ep++ = (char*)vp->var_text; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2227 | } |
| 2228 | } |
| 2229 | } while (++vpp < vartab + VTABSIZE); |
| 2230 | if (ep == stackstrend()) |
| 2231 | ep = growstackstr(); |
| 2232 | if (end) |
| 2233 | *end = ep; |
| 2234 | *ep++ = NULL; |
| 2235 | return grabstackstr(ep); |
| 2236 | } |
| 2237 | |
| 2238 | |
| 2239 | /* ============ Path search helper |
| 2240 | * |
| 2241 | * The variable path (passed by reference) should be set to the start |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2242 | * of the path before the first call; path_advance will update |
| 2243 | * this value as it proceeds. Successive calls to path_advance will return |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2244 | * the possible path expansions in sequence. If an option (indicated by |
| 2245 | * a percent sign) appears in the path entry then the global variable |
| 2246 | * pathopt will be set to point to it; otherwise pathopt will be set to |
| 2247 | * NULL. |
| 2248 | */ |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2249 | static const char *pathopt; /* set by path_advance */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2250 | |
| 2251 | static char * |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2252 | path_advance(const char **path, const char *name) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2253 | { |
| 2254 | const char *p; |
| 2255 | char *q; |
| 2256 | const char *start; |
| 2257 | size_t len; |
| 2258 | |
| 2259 | if (*path == NULL) |
| 2260 | return NULL; |
| 2261 | start = *path; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2262 | for (p = start; *p && *p != ':' && *p != '%'; p++) |
| 2263 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2264 | len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */ |
| 2265 | while (stackblocksize() < len) |
| 2266 | growstackblock(); |
| 2267 | q = stackblock(); |
| 2268 | if (p != start) { |
| 2269 | memcpy(q, start, p - start); |
| 2270 | q += p - start; |
| 2271 | *q++ = '/'; |
| 2272 | } |
| 2273 | strcpy(q, name); |
| 2274 | pathopt = NULL; |
| 2275 | if (*p == '%') { |
| 2276 | pathopt = ++p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2277 | while (*p && *p != ':') |
| 2278 | p++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2279 | } |
| 2280 | if (*p == ':') |
| 2281 | *path = p + 1; |
| 2282 | else |
| 2283 | *path = NULL; |
| 2284 | return stalloc(len); |
| 2285 | } |
| 2286 | |
| 2287 | |
| 2288 | /* ============ Prompt */ |
| 2289 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 2290 | static smallint doprompt; /* if set, prompt the user */ |
| 2291 | static smallint needprompt; /* true if interactive and at start of line */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2292 | |
| 2293 | #if ENABLE_FEATURE_EDITING |
| 2294 | static line_input_t *line_input_state; |
| 2295 | static const char *cmdedit_prompt; |
| 2296 | static void |
| 2297 | putprompt(const char *s) |
| 2298 | { |
| 2299 | if (ENABLE_ASH_EXPAND_PRMT) { |
| 2300 | free((char*)cmdedit_prompt); |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 2301 | cmdedit_prompt = ckstrdup(s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2302 | return; |
| 2303 | } |
| 2304 | cmdedit_prompt = s; |
| 2305 | } |
| 2306 | #else |
| 2307 | static void |
| 2308 | putprompt(const char *s) |
| 2309 | { |
| 2310 | out2str(s); |
| 2311 | } |
| 2312 | #endif |
| 2313 | |
| 2314 | #if ENABLE_ASH_EXPAND_PRMT |
| 2315 | /* expandstr() needs parsing machinery, so it is far away ahead... */ |
| 2316 | static const char *expandstr(const char *ps); |
| 2317 | #else |
| 2318 | #define expandstr(s) s |
| 2319 | #endif |
| 2320 | |
| 2321 | static void |
| 2322 | setprompt(int whichprompt) |
| 2323 | { |
| 2324 | const char *prompt; |
| 2325 | #if ENABLE_ASH_EXPAND_PRMT |
| 2326 | struct stackmark smark; |
| 2327 | #endif |
| 2328 | |
| 2329 | needprompt = 0; |
| 2330 | |
| 2331 | switch (whichprompt) { |
| 2332 | case 1: |
| 2333 | prompt = ps1val(); |
| 2334 | break; |
| 2335 | case 2: |
| 2336 | prompt = ps2val(); |
| 2337 | break; |
| 2338 | default: /* 0 */ |
| 2339 | prompt = nullstr; |
| 2340 | } |
| 2341 | #if ENABLE_ASH_EXPAND_PRMT |
| 2342 | setstackmark(&smark); |
| 2343 | stalloc(stackblocksize()); |
| 2344 | #endif |
| 2345 | putprompt(expandstr(prompt)); |
| 2346 | #if ENABLE_ASH_EXPAND_PRMT |
| 2347 | popstackmark(&smark); |
| 2348 | #endif |
| 2349 | } |
| 2350 | |
| 2351 | |
| 2352 | /* ============ The cd and pwd commands */ |
| 2353 | |
| 2354 | #define CD_PHYSICAL 1 |
| 2355 | #define CD_PRINT 2 |
| 2356 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2357 | static int |
| 2358 | cdopt(void) |
| 2359 | { |
| 2360 | int flags = 0; |
| 2361 | int i, j; |
| 2362 | |
| 2363 | j = 'L'; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 2364 | while ((i = nextopt("LP")) != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2365 | if (i != j) { |
| 2366 | flags ^= CD_PHYSICAL; |
| 2367 | j = i; |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | return flags; |
| 2372 | } |
| 2373 | |
| 2374 | /* |
| 2375 | * Update curdir (the name of the current directory) in response to a |
| 2376 | * cd command. |
| 2377 | */ |
| 2378 | static const char * |
| 2379 | updatepwd(const char *dir) |
| 2380 | { |
| 2381 | char *new; |
| 2382 | char *p; |
| 2383 | char *cdcomppath; |
| 2384 | const char *lim; |
| 2385 | |
| 2386 | cdcomppath = ststrdup(dir); |
| 2387 | STARTSTACKSTR(new); |
| 2388 | if (*dir != '/') { |
| 2389 | if (curdir == nullstr) |
| 2390 | return 0; |
| 2391 | new = stack_putstr(curdir, new); |
| 2392 | } |
| 2393 | new = makestrspace(strlen(dir) + 2, new); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2394 | lim = (char *)stackblock() + 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2395 | if (*dir != '/') { |
| 2396 | if (new[-1] != '/') |
| 2397 | USTPUTC('/', new); |
| 2398 | if (new > lim && *lim == '/') |
| 2399 | lim++; |
| 2400 | } else { |
| 2401 | USTPUTC('/', new); |
| 2402 | cdcomppath++; |
| 2403 | if (dir[1] == '/' && dir[2] != '/') { |
| 2404 | USTPUTC('/', new); |
| 2405 | cdcomppath++; |
| 2406 | lim++; |
| 2407 | } |
| 2408 | } |
| 2409 | p = strtok(cdcomppath, "/"); |
| 2410 | while (p) { |
| 2411 | switch (*p) { |
| 2412 | case '.': |
| 2413 | if (p[1] == '.' && p[2] == '\0') { |
| 2414 | while (new > lim) { |
| 2415 | STUNPUTC(new); |
| 2416 | if (new[-1] == '/') |
| 2417 | break; |
| 2418 | } |
| 2419 | break; |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 2420 | } |
| 2421 | if (p[1] == '\0') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2422 | break; |
| 2423 | /* fall through */ |
| 2424 | default: |
| 2425 | new = stack_putstr(p, new); |
| 2426 | USTPUTC('/', new); |
| 2427 | } |
| 2428 | p = strtok(0, "/"); |
| 2429 | } |
| 2430 | if (new > lim) |
| 2431 | STUNPUTC(new); |
| 2432 | *new = 0; |
| 2433 | return stackblock(); |
| 2434 | } |
| 2435 | |
| 2436 | /* |
| 2437 | * Find out what the current directory is. If we already know the current |
| 2438 | * directory, this routine returns immediately. |
| 2439 | */ |
| 2440 | static char * |
| 2441 | getpwd(void) |
| 2442 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2443 | char *dir = getcwd(NULL, 0); /* huh, using glibc extension? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2444 | return dir ? dir : nullstr; |
| 2445 | } |
| 2446 | |
| 2447 | static void |
| 2448 | setpwd(const char *val, int setold) |
| 2449 | { |
| 2450 | char *oldcur, *dir; |
| 2451 | |
| 2452 | oldcur = dir = curdir; |
| 2453 | |
| 2454 | if (setold) { |
| 2455 | setvar("OLDPWD", oldcur, VEXPORT); |
| 2456 | } |
| 2457 | INT_OFF; |
| 2458 | if (physdir != nullstr) { |
| 2459 | if (physdir != oldcur) |
| 2460 | free(physdir); |
| 2461 | physdir = nullstr; |
| 2462 | } |
| 2463 | if (oldcur == val || !val) { |
| 2464 | char *s = getpwd(); |
| 2465 | physdir = s; |
| 2466 | if (!val) |
| 2467 | dir = s; |
| 2468 | } else |
| 2469 | dir = ckstrdup(val); |
| 2470 | if (oldcur != dir && oldcur != nullstr) { |
| 2471 | free(oldcur); |
| 2472 | } |
| 2473 | curdir = dir; |
| 2474 | INT_ON; |
| 2475 | setvar("PWD", dir, VEXPORT); |
| 2476 | } |
| 2477 | |
| 2478 | static void hashcd(void); |
| 2479 | |
| 2480 | /* |
| 2481 | * Actually do the chdir. We also call hashcd to let the routines in exec.c |
| 2482 | * know that the current directory has changed. |
| 2483 | */ |
| 2484 | static int |
| 2485 | docd(const char *dest, int flags) |
| 2486 | { |
Denys Vlasenko | 4b1100e | 2010-03-05 14:10:54 +0100 | [diff] [blame] | 2487 | const char *dir = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2488 | int err; |
| 2489 | |
| 2490 | TRACE(("docd(\"%s\", %d) called\n", dest, flags)); |
| 2491 | |
| 2492 | INT_OFF; |
| 2493 | if (!(flags & CD_PHYSICAL)) { |
| 2494 | dir = updatepwd(dest); |
| 2495 | if (dir) |
| 2496 | dest = dir; |
| 2497 | } |
| 2498 | err = chdir(dest); |
| 2499 | if (err) |
| 2500 | goto out; |
| 2501 | setpwd(dir, 1); |
| 2502 | hashcd(); |
| 2503 | out: |
| 2504 | INT_ON; |
| 2505 | return err; |
| 2506 | } |
| 2507 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2508 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2509 | cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2510 | { |
| 2511 | const char *dest; |
| 2512 | const char *path; |
| 2513 | const char *p; |
| 2514 | char c; |
| 2515 | struct stat statb; |
| 2516 | int flags; |
| 2517 | |
| 2518 | flags = cdopt(); |
| 2519 | dest = *argptr; |
| 2520 | if (!dest) |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2521 | dest = bltinlookup("HOME"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2522 | else if (LONE_DASH(dest)) { |
| 2523 | dest = bltinlookup("OLDPWD"); |
| 2524 | flags |= CD_PRINT; |
| 2525 | } |
| 2526 | if (!dest) |
| 2527 | dest = nullstr; |
| 2528 | if (*dest == '/') |
| 2529 | goto step7; |
| 2530 | if (*dest == '.') { |
| 2531 | c = dest[1]; |
| 2532 | dotdot: |
| 2533 | switch (c) { |
| 2534 | case '\0': |
| 2535 | case '/': |
| 2536 | goto step6; |
| 2537 | case '.': |
| 2538 | c = dest[2]; |
| 2539 | if (c != '.') |
| 2540 | goto dotdot; |
| 2541 | } |
| 2542 | } |
| 2543 | if (!*dest) |
| 2544 | dest = "."; |
| 2545 | path = bltinlookup("CDPATH"); |
| 2546 | if (!path) { |
| 2547 | step6: |
| 2548 | step7: |
| 2549 | p = dest; |
| 2550 | goto docd; |
| 2551 | } |
| 2552 | do { |
| 2553 | c = *path; |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2554 | p = path_advance(&path, dest); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2555 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
| 2556 | if (c && c != ':') |
| 2557 | flags |= CD_PRINT; |
| 2558 | docd: |
| 2559 | if (!docd(p, flags)) |
| 2560 | goto out; |
| 2561 | break; |
| 2562 | } |
| 2563 | } while (path); |
| 2564 | ash_msg_and_raise_error("can't cd to %s", dest); |
| 2565 | /* NOTREACHED */ |
| 2566 | out: |
| 2567 | if (flags & CD_PRINT) |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2568 | out1fmt("%s\n", curdir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2569 | return 0; |
| 2570 | } |
| 2571 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2572 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2573 | pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2574 | { |
| 2575 | int flags; |
| 2576 | const char *dir = curdir; |
| 2577 | |
| 2578 | flags = cdopt(); |
| 2579 | if (flags) { |
| 2580 | if (physdir == nullstr) |
| 2581 | setpwd(dir, 0); |
| 2582 | dir = physdir; |
| 2583 | } |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2584 | out1fmt("%s\n", dir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2585 | return 0; |
| 2586 | } |
| 2587 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2588 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 2589 | /* ============ ... */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2590 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2591 | |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 2592 | #define IBUFSIZ (ENABLE_FEATURE_EDITING ? CONFIG_FEATURE_EDITING_MAX_LEN : 1024) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2593 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2594 | /* Syntax classes */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2595 | #define CWORD 0 /* character is nothing special */ |
| 2596 | #define CNL 1 /* newline character */ |
| 2597 | #define CBACK 2 /* a backslash character */ |
| 2598 | #define CSQUOTE 3 /* single quote */ |
| 2599 | #define CDQUOTE 4 /* double quote */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2600 | #define CENDQUOTE 5 /* a terminating quote */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2601 | #define CBQUOTE 6 /* backwards single quote */ |
| 2602 | #define CVAR 7 /* a dollar sign */ |
| 2603 | #define CENDVAR 8 /* a '}' character */ |
| 2604 | #define CLP 9 /* a left paren in arithmetic */ |
| 2605 | #define CRP 10 /* a right paren in arithmetic */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2606 | #define CENDFILE 11 /* end of file */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2607 | #define CCTL 12 /* like CWORD, except it must be escaped */ |
| 2608 | #define CSPCL 13 /* these terminate a word */ |
| 2609 | #define CIGN 14 /* character should be ignored */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2610 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2611 | #define PEOF 256 |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2612 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2613 | # define PEOA 257 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2614 | #endif |
| 2615 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2616 | #define USE_SIT_FUNCTION ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2617 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2618 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2619 | # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8) | (d << 12)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2620 | #else |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2621 | # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8)) |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2622 | #endif |
Denys Vlasenko | 068d386 | 2009-11-29 01:41:11 +0100 | [diff] [blame] | 2623 | static const uint16_t S_I_T[] = { |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2624 | #if ENABLE_ASH_ALIAS |
| 2625 | SIT_ITEM(CSPCL , CIGN , CIGN , CIGN ), /* 0, PEOA */ |
| 2626 | #endif |
| 2627 | SIT_ITEM(CSPCL , CWORD , CWORD, CWORD ), /* 1, ' ' */ |
| 2628 | SIT_ITEM(CNL , CNL , CNL , CNL ), /* 2, \n */ |
| 2629 | SIT_ITEM(CWORD , CCTL , CCTL , CWORD ), /* 3, !*-/:=?[]~ */ |
| 2630 | SIT_ITEM(CDQUOTE , CENDQUOTE, CWORD, CWORD ), /* 4, '"' */ |
| 2631 | SIT_ITEM(CVAR , CVAR , CWORD, CVAR ), /* 5, $ */ |
| 2632 | SIT_ITEM(CSQUOTE , CWORD , CENDQUOTE, CWORD), /* 6, "'" */ |
| 2633 | SIT_ITEM(CSPCL , CWORD , CWORD, CLP ), /* 7, ( */ |
| 2634 | SIT_ITEM(CSPCL , CWORD , CWORD, CRP ), /* 8, ) */ |
| 2635 | SIT_ITEM(CBACK , CBACK , CCTL , CBACK ), /* 9, \ */ |
| 2636 | SIT_ITEM(CBQUOTE , CBQUOTE , CWORD, CBQUOTE), /* 10, ` */ |
| 2637 | SIT_ITEM(CENDVAR , CENDVAR , CWORD, CENDVAR), /* 11, } */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2638 | #if !USE_SIT_FUNCTION |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2639 | SIT_ITEM(CENDFILE, CENDFILE , CENDFILE, CENDFILE),/* 12, PEOF */ |
| 2640 | SIT_ITEM(CWORD , CWORD , CWORD, CWORD ), /* 13, 0-9A-Za-z */ |
| 2641 | SIT_ITEM(CCTL , CCTL , CCTL , CCTL ) /* 14, CTLESC ... */ |
| 2642 | #endif |
| 2643 | #undef SIT_ITEM |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2644 | }; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2645 | /* Constants below must match table above */ |
| 2646 | enum { |
| 2647 | #if ENABLE_ASH_ALIAS |
| 2648 | CSPCL_CIGN_CIGN_CIGN , /* 0 */ |
| 2649 | #endif |
| 2650 | CSPCL_CWORD_CWORD_CWORD , /* 1 */ |
| 2651 | CNL_CNL_CNL_CNL , /* 2 */ |
| 2652 | CWORD_CCTL_CCTL_CWORD , /* 3 */ |
| 2653 | CDQUOTE_CENDQUOTE_CWORD_CWORD , /* 4 */ |
| 2654 | CVAR_CVAR_CWORD_CVAR , /* 5 */ |
| 2655 | CSQUOTE_CWORD_CENDQUOTE_CWORD , /* 6 */ |
| 2656 | CSPCL_CWORD_CWORD_CLP , /* 7 */ |
| 2657 | CSPCL_CWORD_CWORD_CRP , /* 8 */ |
| 2658 | CBACK_CBACK_CCTL_CBACK , /* 9 */ |
| 2659 | CBQUOTE_CBQUOTE_CWORD_CBQUOTE , /* 10 */ |
| 2660 | CENDVAR_CENDVAR_CWORD_CENDVAR , /* 11 */ |
| 2661 | CENDFILE_CENDFILE_CENDFILE_CENDFILE, /* 12 */ |
| 2662 | CWORD_CWORD_CWORD_CWORD , /* 13 */ |
| 2663 | CCTL_CCTL_CCTL_CCTL , /* 14 */ |
| 2664 | }; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2665 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2666 | /* c in SIT(c, syntax) must be an *unsigned char* or PEOA or PEOF, |
| 2667 | * caller must ensure proper cast on it if c is *char_ptr! |
| 2668 | */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2669 | /* Values for syntax param */ |
| 2670 | #define BASESYNTAX 0 /* not in quotes */ |
| 2671 | #define DQSYNTAX 1 /* in double quotes */ |
| 2672 | #define SQSYNTAX 2 /* in single quotes */ |
| 2673 | #define ARISYNTAX 3 /* in arithmetic */ |
| 2674 | #define PSSYNTAX 4 /* prompt. never passed to SIT() */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2675 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2676 | #if USE_SIT_FUNCTION |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2677 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2678 | static int |
| 2679 | SIT(int c, int syntax) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2680 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 2681 | static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-/:;<=>?[\\]`|}~"; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2682 | # if ENABLE_ASH_ALIAS |
| 2683 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2684 | 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */ |
| 2685 | 7, 8, 3, 3, 3, 3, 1, 1, /* "()*-/:;<" */ |
| 2686 | 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */ |
| 2687 | 11, 3 /* "}~" */ |
| 2688 | }; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2689 | # else |
| 2690 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2691 | 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */ |
| 2692 | 6, 7, 2, 2, 2, 2, 0, 0, /* "()*-/:;<" */ |
| 2693 | 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */ |
| 2694 | 10, 2 /* "}~" */ |
| 2695 | }; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2696 | # endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2697 | const char *s; |
| 2698 | int indx; |
| 2699 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2700 | if (c == PEOF) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2701 | return CENDFILE; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2702 | # if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2703 | if (c == PEOA) |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2704 | indx = 0; |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2705 | else |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2706 | # endif |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2707 | { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2708 | /* Cast is purely for paranoia here, |
| 2709 | * just in case someone passed signed char to us */ |
| 2710 | if ((unsigned char)c >= CTL_FIRST |
| 2711 | && (unsigned char)c <= CTL_LAST |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2712 | ) { |
| 2713 | return CCTL; |
| 2714 | } |
| 2715 | s = strchrnul(spec_symbls, c); |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2716 | if (*s == '\0') |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2717 | return CWORD; |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2718 | indx = syntax_index_table[s - spec_symbls]; |
| 2719 | } |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2720 | return (S_I_T[indx] >> (syntax*4)) & 0xf; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2723 | #else /* !USE_SIT_FUNCTION */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2724 | |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2725 | static const uint8_t syntax_index_table[] = { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2726 | /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2727 | /* 0 */ CWORD_CWORD_CWORD_CWORD, |
| 2728 | /* 1 */ CWORD_CWORD_CWORD_CWORD, |
| 2729 | /* 2 */ CWORD_CWORD_CWORD_CWORD, |
| 2730 | /* 3 */ CWORD_CWORD_CWORD_CWORD, |
| 2731 | /* 4 */ CWORD_CWORD_CWORD_CWORD, |
| 2732 | /* 5 */ CWORD_CWORD_CWORD_CWORD, |
| 2733 | /* 6 */ CWORD_CWORD_CWORD_CWORD, |
| 2734 | /* 7 */ CWORD_CWORD_CWORD_CWORD, |
| 2735 | /* 8 */ CWORD_CWORD_CWORD_CWORD, |
| 2736 | /* 9 "\t" */ CSPCL_CWORD_CWORD_CWORD, |
| 2737 | /* 10 "\n" */ CNL_CNL_CNL_CNL, |
| 2738 | /* 11 */ CWORD_CWORD_CWORD_CWORD, |
| 2739 | /* 12 */ CWORD_CWORD_CWORD_CWORD, |
| 2740 | /* 13 */ CWORD_CWORD_CWORD_CWORD, |
| 2741 | /* 14 */ CWORD_CWORD_CWORD_CWORD, |
| 2742 | /* 15 */ CWORD_CWORD_CWORD_CWORD, |
| 2743 | /* 16 */ CWORD_CWORD_CWORD_CWORD, |
| 2744 | /* 17 */ CWORD_CWORD_CWORD_CWORD, |
| 2745 | /* 18 */ CWORD_CWORD_CWORD_CWORD, |
| 2746 | /* 19 */ CWORD_CWORD_CWORD_CWORD, |
| 2747 | /* 20 */ CWORD_CWORD_CWORD_CWORD, |
| 2748 | /* 21 */ CWORD_CWORD_CWORD_CWORD, |
| 2749 | /* 22 */ CWORD_CWORD_CWORD_CWORD, |
| 2750 | /* 23 */ CWORD_CWORD_CWORD_CWORD, |
| 2751 | /* 24 */ CWORD_CWORD_CWORD_CWORD, |
| 2752 | /* 25 */ CWORD_CWORD_CWORD_CWORD, |
| 2753 | /* 26 */ CWORD_CWORD_CWORD_CWORD, |
| 2754 | /* 27 */ CWORD_CWORD_CWORD_CWORD, |
| 2755 | /* 28 */ CWORD_CWORD_CWORD_CWORD, |
| 2756 | /* 29 */ CWORD_CWORD_CWORD_CWORD, |
| 2757 | /* 30 */ CWORD_CWORD_CWORD_CWORD, |
| 2758 | /* 31 */ CWORD_CWORD_CWORD_CWORD, |
| 2759 | /* 32 " " */ CSPCL_CWORD_CWORD_CWORD, |
| 2760 | /* 33 "!" */ CWORD_CCTL_CCTL_CWORD, |
| 2761 | /* 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD, |
| 2762 | /* 35 "#" */ CWORD_CWORD_CWORD_CWORD, |
| 2763 | /* 36 "$" */ CVAR_CVAR_CWORD_CVAR, |
| 2764 | /* 37 "%" */ CWORD_CWORD_CWORD_CWORD, |
| 2765 | /* 38 "&" */ CSPCL_CWORD_CWORD_CWORD, |
| 2766 | /* 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD, |
| 2767 | /* 40 "(" */ CSPCL_CWORD_CWORD_CLP, |
| 2768 | /* 41 ")" */ CSPCL_CWORD_CWORD_CRP, |
| 2769 | /* 42 "*" */ CWORD_CCTL_CCTL_CWORD, |
| 2770 | /* 43 "+" */ CWORD_CWORD_CWORD_CWORD, |
| 2771 | /* 44 "," */ CWORD_CWORD_CWORD_CWORD, |
| 2772 | /* 45 "-" */ CWORD_CCTL_CCTL_CWORD, |
| 2773 | /* 46 "." */ CWORD_CWORD_CWORD_CWORD, |
| 2774 | /* 47 "/" */ CWORD_CCTL_CCTL_CWORD, |
| 2775 | /* 48 "0" */ CWORD_CWORD_CWORD_CWORD, |
| 2776 | /* 49 "1" */ CWORD_CWORD_CWORD_CWORD, |
| 2777 | /* 50 "2" */ CWORD_CWORD_CWORD_CWORD, |
| 2778 | /* 51 "3" */ CWORD_CWORD_CWORD_CWORD, |
| 2779 | /* 52 "4" */ CWORD_CWORD_CWORD_CWORD, |
| 2780 | /* 53 "5" */ CWORD_CWORD_CWORD_CWORD, |
| 2781 | /* 54 "6" */ CWORD_CWORD_CWORD_CWORD, |
| 2782 | /* 55 "7" */ CWORD_CWORD_CWORD_CWORD, |
| 2783 | /* 56 "8" */ CWORD_CWORD_CWORD_CWORD, |
| 2784 | /* 57 "9" */ CWORD_CWORD_CWORD_CWORD, |
| 2785 | /* 58 ":" */ CWORD_CCTL_CCTL_CWORD, |
| 2786 | /* 59 ";" */ CSPCL_CWORD_CWORD_CWORD, |
| 2787 | /* 60 "<" */ CSPCL_CWORD_CWORD_CWORD, |
| 2788 | /* 61 "=" */ CWORD_CCTL_CCTL_CWORD, |
| 2789 | /* 62 ">" */ CSPCL_CWORD_CWORD_CWORD, |
| 2790 | /* 63 "?" */ CWORD_CCTL_CCTL_CWORD, |
| 2791 | /* 64 "@" */ CWORD_CWORD_CWORD_CWORD, |
| 2792 | /* 65 "A" */ CWORD_CWORD_CWORD_CWORD, |
| 2793 | /* 66 "B" */ CWORD_CWORD_CWORD_CWORD, |
| 2794 | /* 67 "C" */ CWORD_CWORD_CWORD_CWORD, |
| 2795 | /* 68 "D" */ CWORD_CWORD_CWORD_CWORD, |
| 2796 | /* 69 "E" */ CWORD_CWORD_CWORD_CWORD, |
| 2797 | /* 70 "F" */ CWORD_CWORD_CWORD_CWORD, |
| 2798 | /* 71 "G" */ CWORD_CWORD_CWORD_CWORD, |
| 2799 | /* 72 "H" */ CWORD_CWORD_CWORD_CWORD, |
| 2800 | /* 73 "I" */ CWORD_CWORD_CWORD_CWORD, |
| 2801 | /* 74 "J" */ CWORD_CWORD_CWORD_CWORD, |
| 2802 | /* 75 "K" */ CWORD_CWORD_CWORD_CWORD, |
| 2803 | /* 76 "L" */ CWORD_CWORD_CWORD_CWORD, |
| 2804 | /* 77 "M" */ CWORD_CWORD_CWORD_CWORD, |
| 2805 | /* 78 "N" */ CWORD_CWORD_CWORD_CWORD, |
| 2806 | /* 79 "O" */ CWORD_CWORD_CWORD_CWORD, |
| 2807 | /* 80 "P" */ CWORD_CWORD_CWORD_CWORD, |
| 2808 | /* 81 "Q" */ CWORD_CWORD_CWORD_CWORD, |
| 2809 | /* 82 "R" */ CWORD_CWORD_CWORD_CWORD, |
| 2810 | /* 83 "S" */ CWORD_CWORD_CWORD_CWORD, |
| 2811 | /* 84 "T" */ CWORD_CWORD_CWORD_CWORD, |
| 2812 | /* 85 "U" */ CWORD_CWORD_CWORD_CWORD, |
| 2813 | /* 86 "V" */ CWORD_CWORD_CWORD_CWORD, |
| 2814 | /* 87 "W" */ CWORD_CWORD_CWORD_CWORD, |
| 2815 | /* 88 "X" */ CWORD_CWORD_CWORD_CWORD, |
| 2816 | /* 89 "Y" */ CWORD_CWORD_CWORD_CWORD, |
| 2817 | /* 90 "Z" */ CWORD_CWORD_CWORD_CWORD, |
| 2818 | /* 91 "[" */ CWORD_CCTL_CCTL_CWORD, |
| 2819 | /* 92 "\" */ CBACK_CBACK_CCTL_CBACK, |
| 2820 | /* 93 "]" */ CWORD_CCTL_CCTL_CWORD, |
| 2821 | /* 94 "^" */ CWORD_CWORD_CWORD_CWORD, |
| 2822 | /* 95 "_" */ CWORD_CWORD_CWORD_CWORD, |
| 2823 | /* 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE, |
| 2824 | /* 97 "a" */ CWORD_CWORD_CWORD_CWORD, |
| 2825 | /* 98 "b" */ CWORD_CWORD_CWORD_CWORD, |
| 2826 | /* 99 "c" */ CWORD_CWORD_CWORD_CWORD, |
| 2827 | /* 100 "d" */ CWORD_CWORD_CWORD_CWORD, |
| 2828 | /* 101 "e" */ CWORD_CWORD_CWORD_CWORD, |
| 2829 | /* 102 "f" */ CWORD_CWORD_CWORD_CWORD, |
| 2830 | /* 103 "g" */ CWORD_CWORD_CWORD_CWORD, |
| 2831 | /* 104 "h" */ CWORD_CWORD_CWORD_CWORD, |
| 2832 | /* 105 "i" */ CWORD_CWORD_CWORD_CWORD, |
| 2833 | /* 106 "j" */ CWORD_CWORD_CWORD_CWORD, |
| 2834 | /* 107 "k" */ CWORD_CWORD_CWORD_CWORD, |
| 2835 | /* 108 "l" */ CWORD_CWORD_CWORD_CWORD, |
| 2836 | /* 109 "m" */ CWORD_CWORD_CWORD_CWORD, |
| 2837 | /* 110 "n" */ CWORD_CWORD_CWORD_CWORD, |
| 2838 | /* 111 "o" */ CWORD_CWORD_CWORD_CWORD, |
| 2839 | /* 112 "p" */ CWORD_CWORD_CWORD_CWORD, |
| 2840 | /* 113 "q" */ CWORD_CWORD_CWORD_CWORD, |
| 2841 | /* 114 "r" */ CWORD_CWORD_CWORD_CWORD, |
| 2842 | /* 115 "s" */ CWORD_CWORD_CWORD_CWORD, |
| 2843 | /* 116 "t" */ CWORD_CWORD_CWORD_CWORD, |
| 2844 | /* 117 "u" */ CWORD_CWORD_CWORD_CWORD, |
| 2845 | /* 118 "v" */ CWORD_CWORD_CWORD_CWORD, |
| 2846 | /* 119 "w" */ CWORD_CWORD_CWORD_CWORD, |
| 2847 | /* 120 "x" */ CWORD_CWORD_CWORD_CWORD, |
| 2848 | /* 121 "y" */ CWORD_CWORD_CWORD_CWORD, |
| 2849 | /* 122 "z" */ CWORD_CWORD_CWORD_CWORD, |
| 2850 | /* 123 "{" */ CWORD_CWORD_CWORD_CWORD, |
| 2851 | /* 124 "|" */ CSPCL_CWORD_CWORD_CWORD, |
| 2852 | /* 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR, |
| 2853 | /* 126 "~" */ CWORD_CCTL_CCTL_CWORD, |
| 2854 | /* 127 del */ CWORD_CWORD_CWORD_CWORD, |
| 2855 | /* 128 0x80 */ CWORD_CWORD_CWORD_CWORD, |
| 2856 | /* 129 CTLESC */ CCTL_CCTL_CCTL_CCTL, |
| 2857 | /* 130 CTLVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2858 | /* 131 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2859 | /* 132 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL, |
| 2860 | /* 133 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL, |
| 2861 | /* 134 CTLARI */ CCTL_CCTL_CCTL_CCTL, |
| 2862 | /* 135 CTLENDARI */ CCTL_CCTL_CCTL_CCTL, |
| 2863 | /* 136 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL, |
| 2864 | /* 137 */ CWORD_CWORD_CWORD_CWORD, |
| 2865 | /* 138 */ CWORD_CWORD_CWORD_CWORD, |
| 2866 | /* 139 */ CWORD_CWORD_CWORD_CWORD, |
| 2867 | /* 140 */ CWORD_CWORD_CWORD_CWORD, |
| 2868 | /* 141 */ CWORD_CWORD_CWORD_CWORD, |
| 2869 | /* 142 */ CWORD_CWORD_CWORD_CWORD, |
| 2870 | /* 143 */ CWORD_CWORD_CWORD_CWORD, |
| 2871 | /* 144 */ CWORD_CWORD_CWORD_CWORD, |
| 2872 | /* 145 */ CWORD_CWORD_CWORD_CWORD, |
| 2873 | /* 146 */ CWORD_CWORD_CWORD_CWORD, |
| 2874 | /* 147 */ CWORD_CWORD_CWORD_CWORD, |
| 2875 | /* 148 */ CWORD_CWORD_CWORD_CWORD, |
| 2876 | /* 149 */ CWORD_CWORD_CWORD_CWORD, |
| 2877 | /* 150 */ CWORD_CWORD_CWORD_CWORD, |
| 2878 | /* 151 */ CWORD_CWORD_CWORD_CWORD, |
| 2879 | /* 152 */ CWORD_CWORD_CWORD_CWORD, |
| 2880 | /* 153 */ CWORD_CWORD_CWORD_CWORD, |
| 2881 | /* 154 */ CWORD_CWORD_CWORD_CWORD, |
| 2882 | /* 155 */ CWORD_CWORD_CWORD_CWORD, |
| 2883 | /* 156 */ CWORD_CWORD_CWORD_CWORD, |
| 2884 | /* 157 */ CWORD_CWORD_CWORD_CWORD, |
| 2885 | /* 158 */ CWORD_CWORD_CWORD_CWORD, |
| 2886 | /* 159 */ CWORD_CWORD_CWORD_CWORD, |
| 2887 | /* 160 */ CWORD_CWORD_CWORD_CWORD, |
| 2888 | /* 161 */ CWORD_CWORD_CWORD_CWORD, |
| 2889 | /* 162 */ CWORD_CWORD_CWORD_CWORD, |
| 2890 | /* 163 */ CWORD_CWORD_CWORD_CWORD, |
| 2891 | /* 164 */ CWORD_CWORD_CWORD_CWORD, |
| 2892 | /* 165 */ CWORD_CWORD_CWORD_CWORD, |
| 2893 | /* 166 */ CWORD_CWORD_CWORD_CWORD, |
| 2894 | /* 167 */ CWORD_CWORD_CWORD_CWORD, |
| 2895 | /* 168 */ CWORD_CWORD_CWORD_CWORD, |
| 2896 | /* 169 */ CWORD_CWORD_CWORD_CWORD, |
| 2897 | /* 170 */ CWORD_CWORD_CWORD_CWORD, |
| 2898 | /* 171 */ CWORD_CWORD_CWORD_CWORD, |
| 2899 | /* 172 */ CWORD_CWORD_CWORD_CWORD, |
| 2900 | /* 173 */ CWORD_CWORD_CWORD_CWORD, |
| 2901 | /* 174 */ CWORD_CWORD_CWORD_CWORD, |
| 2902 | /* 175 */ CWORD_CWORD_CWORD_CWORD, |
| 2903 | /* 176 */ CWORD_CWORD_CWORD_CWORD, |
| 2904 | /* 177 */ CWORD_CWORD_CWORD_CWORD, |
| 2905 | /* 178 */ CWORD_CWORD_CWORD_CWORD, |
| 2906 | /* 179 */ CWORD_CWORD_CWORD_CWORD, |
| 2907 | /* 180 */ CWORD_CWORD_CWORD_CWORD, |
| 2908 | /* 181 */ CWORD_CWORD_CWORD_CWORD, |
| 2909 | /* 182 */ CWORD_CWORD_CWORD_CWORD, |
| 2910 | /* 183 */ CWORD_CWORD_CWORD_CWORD, |
| 2911 | /* 184 */ CWORD_CWORD_CWORD_CWORD, |
| 2912 | /* 185 */ CWORD_CWORD_CWORD_CWORD, |
| 2913 | /* 186 */ CWORD_CWORD_CWORD_CWORD, |
| 2914 | /* 187 */ CWORD_CWORD_CWORD_CWORD, |
| 2915 | /* 188 */ CWORD_CWORD_CWORD_CWORD, |
| 2916 | /* 189 */ CWORD_CWORD_CWORD_CWORD, |
| 2917 | /* 190 */ CWORD_CWORD_CWORD_CWORD, |
| 2918 | /* 191 */ CWORD_CWORD_CWORD_CWORD, |
| 2919 | /* 192 */ CWORD_CWORD_CWORD_CWORD, |
| 2920 | /* 193 */ CWORD_CWORD_CWORD_CWORD, |
| 2921 | /* 194 */ CWORD_CWORD_CWORD_CWORD, |
| 2922 | /* 195 */ CWORD_CWORD_CWORD_CWORD, |
| 2923 | /* 196 */ CWORD_CWORD_CWORD_CWORD, |
| 2924 | /* 197 */ CWORD_CWORD_CWORD_CWORD, |
| 2925 | /* 198 */ CWORD_CWORD_CWORD_CWORD, |
| 2926 | /* 199 */ CWORD_CWORD_CWORD_CWORD, |
| 2927 | /* 200 */ CWORD_CWORD_CWORD_CWORD, |
| 2928 | /* 201 */ CWORD_CWORD_CWORD_CWORD, |
| 2929 | /* 202 */ CWORD_CWORD_CWORD_CWORD, |
| 2930 | /* 203 */ CWORD_CWORD_CWORD_CWORD, |
| 2931 | /* 204 */ CWORD_CWORD_CWORD_CWORD, |
| 2932 | /* 205 */ CWORD_CWORD_CWORD_CWORD, |
| 2933 | /* 206 */ CWORD_CWORD_CWORD_CWORD, |
| 2934 | /* 207 */ CWORD_CWORD_CWORD_CWORD, |
| 2935 | /* 208 */ CWORD_CWORD_CWORD_CWORD, |
| 2936 | /* 209 */ CWORD_CWORD_CWORD_CWORD, |
| 2937 | /* 210 */ CWORD_CWORD_CWORD_CWORD, |
| 2938 | /* 211 */ CWORD_CWORD_CWORD_CWORD, |
| 2939 | /* 212 */ CWORD_CWORD_CWORD_CWORD, |
| 2940 | /* 213 */ CWORD_CWORD_CWORD_CWORD, |
| 2941 | /* 214 */ CWORD_CWORD_CWORD_CWORD, |
| 2942 | /* 215 */ CWORD_CWORD_CWORD_CWORD, |
| 2943 | /* 216 */ CWORD_CWORD_CWORD_CWORD, |
| 2944 | /* 217 */ CWORD_CWORD_CWORD_CWORD, |
| 2945 | /* 218 */ CWORD_CWORD_CWORD_CWORD, |
| 2946 | /* 219 */ CWORD_CWORD_CWORD_CWORD, |
| 2947 | /* 220 */ CWORD_CWORD_CWORD_CWORD, |
| 2948 | /* 221 */ CWORD_CWORD_CWORD_CWORD, |
| 2949 | /* 222 */ CWORD_CWORD_CWORD_CWORD, |
| 2950 | /* 223 */ CWORD_CWORD_CWORD_CWORD, |
| 2951 | /* 224 */ CWORD_CWORD_CWORD_CWORD, |
| 2952 | /* 225 */ CWORD_CWORD_CWORD_CWORD, |
| 2953 | /* 226 */ CWORD_CWORD_CWORD_CWORD, |
| 2954 | /* 227 */ CWORD_CWORD_CWORD_CWORD, |
| 2955 | /* 228 */ CWORD_CWORD_CWORD_CWORD, |
| 2956 | /* 229 */ CWORD_CWORD_CWORD_CWORD, |
| 2957 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 2958 | /* 231 */ CWORD_CWORD_CWORD_CWORD, |
| 2959 | /* 232 */ CWORD_CWORD_CWORD_CWORD, |
| 2960 | /* 233 */ CWORD_CWORD_CWORD_CWORD, |
| 2961 | /* 234 */ CWORD_CWORD_CWORD_CWORD, |
| 2962 | /* 235 */ CWORD_CWORD_CWORD_CWORD, |
| 2963 | /* 236 */ CWORD_CWORD_CWORD_CWORD, |
| 2964 | /* 237 */ CWORD_CWORD_CWORD_CWORD, |
| 2965 | /* 238 */ CWORD_CWORD_CWORD_CWORD, |
| 2966 | /* 239 */ CWORD_CWORD_CWORD_CWORD, |
| 2967 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 2968 | /* 241 */ CWORD_CWORD_CWORD_CWORD, |
| 2969 | /* 242 */ CWORD_CWORD_CWORD_CWORD, |
| 2970 | /* 243 */ CWORD_CWORD_CWORD_CWORD, |
| 2971 | /* 244 */ CWORD_CWORD_CWORD_CWORD, |
| 2972 | /* 245 */ CWORD_CWORD_CWORD_CWORD, |
| 2973 | /* 246 */ CWORD_CWORD_CWORD_CWORD, |
| 2974 | /* 247 */ CWORD_CWORD_CWORD_CWORD, |
| 2975 | /* 248 */ CWORD_CWORD_CWORD_CWORD, |
| 2976 | /* 249 */ CWORD_CWORD_CWORD_CWORD, |
| 2977 | /* 250 */ CWORD_CWORD_CWORD_CWORD, |
| 2978 | /* 251 */ CWORD_CWORD_CWORD_CWORD, |
| 2979 | /* 252 */ CWORD_CWORD_CWORD_CWORD, |
| 2980 | /* 253 */ CWORD_CWORD_CWORD_CWORD, |
| 2981 | /* 254 */ CWORD_CWORD_CWORD_CWORD, |
| 2982 | /* 255 */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2983 | /* PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE, |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2984 | # if ENABLE_ASH_ALIAS |
| 2985 | /* PEOA */ CSPCL_CIGN_CIGN_CIGN, |
| 2986 | # endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2987 | }; |
| 2988 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2989 | # define SIT(c, syntax) ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2990 | |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2991 | #endif /* !USE_SIT_FUNCTION */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2992 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2993 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2994 | /* ============ Alias handling */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 2995 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2996 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2997 | |
| 2998 | #define ALIASINUSE 1 |
| 2999 | #define ALIASDEAD 2 |
| 3000 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3001 | struct alias { |
| 3002 | struct alias *next; |
| 3003 | char *name; |
| 3004 | char *val; |
| 3005 | int flag; |
| 3006 | }; |
| 3007 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 3008 | |
| 3009 | static struct alias **atab; // [ATABSIZE]; |
| 3010 | #define INIT_G_alias() do { \ |
| 3011 | atab = xzalloc(ATABSIZE * sizeof(atab[0])); \ |
| 3012 | } while (0) |
| 3013 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3014 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3015 | static struct alias ** |
| 3016 | __lookupalias(const char *name) { |
| 3017 | unsigned int hashval; |
| 3018 | struct alias **app; |
| 3019 | const char *p; |
| 3020 | unsigned int ch; |
| 3021 | |
| 3022 | p = name; |
| 3023 | |
| 3024 | ch = (unsigned char)*p; |
| 3025 | hashval = ch << 4; |
| 3026 | while (ch) { |
| 3027 | hashval += ch; |
| 3028 | ch = (unsigned char)*++p; |
| 3029 | } |
| 3030 | app = &atab[hashval % ATABSIZE]; |
| 3031 | |
| 3032 | for (; *app; app = &(*app)->next) { |
| 3033 | if (strcmp(name, (*app)->name) == 0) { |
| 3034 | break; |
| 3035 | } |
| 3036 | } |
| 3037 | |
| 3038 | return app; |
| 3039 | } |
| 3040 | |
| 3041 | static struct alias * |
| 3042 | lookupalias(const char *name, int check) |
| 3043 | { |
| 3044 | struct alias *ap = *__lookupalias(name); |
| 3045 | |
| 3046 | if (check && ap && (ap->flag & ALIASINUSE)) |
| 3047 | return NULL; |
| 3048 | return ap; |
| 3049 | } |
| 3050 | |
| 3051 | static struct alias * |
| 3052 | freealias(struct alias *ap) |
| 3053 | { |
| 3054 | struct alias *next; |
| 3055 | |
| 3056 | if (ap->flag & ALIASINUSE) { |
| 3057 | ap->flag |= ALIASDEAD; |
| 3058 | return ap; |
| 3059 | } |
| 3060 | |
| 3061 | next = ap->next; |
| 3062 | free(ap->name); |
| 3063 | free(ap->val); |
| 3064 | free(ap); |
| 3065 | return next; |
| 3066 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3067 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3068 | static void |
| 3069 | setalias(const char *name, const char *val) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3070 | { |
| 3071 | struct alias *ap, **app; |
| 3072 | |
| 3073 | app = __lookupalias(name); |
| 3074 | ap = *app; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3075 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3076 | if (ap) { |
| 3077 | if (!(ap->flag & ALIASINUSE)) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3078 | free(ap->val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3079 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3080 | ap->val = ckstrdup(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3081 | ap->flag &= ~ALIASDEAD; |
| 3082 | } else { |
| 3083 | /* not found */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3084 | ap = ckzalloc(sizeof(struct alias)); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3085 | ap->name = ckstrdup(name); |
| 3086 | ap->val = ckstrdup(val); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3087 | /*ap->flag = 0; - ckzalloc did it */ |
| 3088 | /*ap->next = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3089 | *app = ap; |
| 3090 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3091 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3094 | static int |
| 3095 | unalias(const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3096 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3097 | struct alias **app; |
| 3098 | |
| 3099 | app = __lookupalias(name); |
| 3100 | |
| 3101 | if (*app) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3102 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3103 | *app = freealias(*app); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3104 | INT_ON; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3105 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3108 | return 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3111 | static void |
| 3112 | rmaliases(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3113 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3114 | struct alias *ap, **app; |
| 3115 | int i; |
| 3116 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3117 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3118 | for (i = 0; i < ATABSIZE; i++) { |
| 3119 | app = &atab[i]; |
| 3120 | for (ap = *app; ap; ap = *app) { |
| 3121 | *app = freealias(*app); |
| 3122 | if (ap == *app) { |
| 3123 | app = &ap->next; |
| 3124 | } |
| 3125 | } |
| 3126 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3127 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3130 | static void |
| 3131 | printalias(const struct alias *ap) |
| 3132 | { |
| 3133 | out1fmt("%s=%s\n", ap->name, single_quote(ap->val)); |
| 3134 | } |
| 3135 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3136 | /* |
| 3137 | * TODO - sort output |
| 3138 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3139 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3140 | aliascmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3141 | { |
| 3142 | char *n, *v; |
| 3143 | int ret = 0; |
| 3144 | struct alias *ap; |
| 3145 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3146 | if (!argv[1]) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3147 | int i; |
| 3148 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3149 | for (i = 0; i < ATABSIZE; i++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3150 | for (ap = atab[i]; ap; ap = ap->next) { |
| 3151 | printalias(ap); |
| 3152 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3153 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3154 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3155 | } |
| 3156 | while ((n = *++argv) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3157 | v = strchr(n+1, '='); |
| 3158 | if (v == NULL) { /* n+1: funny ksh stuff */ |
| 3159 | ap = *__lookupalias(n); |
| 3160 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3161 | fprintf(stderr, "%s: %s not found\n", "alias", n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3162 | ret = 1; |
| 3163 | } else |
| 3164 | printalias(ap); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3165 | } else { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3166 | *v++ = '\0'; |
| 3167 | setalias(n, v); |
| 3168 | } |
| 3169 | } |
| 3170 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3171 | return ret; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3172 | } |
| 3173 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3174 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3175 | unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3176 | { |
| 3177 | int i; |
| 3178 | |
| 3179 | while ((i = nextopt("a")) != '\0') { |
| 3180 | if (i == 'a') { |
| 3181 | rmaliases(); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3182 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3183 | } |
| 3184 | } |
| 3185 | for (i = 0; *argptr; argptr++) { |
| 3186 | if (unalias(*argptr)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3187 | fprintf(stderr, "%s: %s not found\n", "unalias", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3188 | i = 1; |
| 3189 | } |
| 3190 | } |
| 3191 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3192 | return i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3193 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3194 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3195 | #endif /* ASH_ALIAS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3196 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3197 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3198 | /* ============ jobs.c */ |
| 3199 | |
| 3200 | /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3201 | #define FORK_FG 0 |
| 3202 | #define FORK_BG 1 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3203 | #define FORK_NOJOB 2 |
| 3204 | |
| 3205 | /* mode flags for showjob(s) */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3206 | #define SHOW_ONLY_PGID 0x01 /* show only pgid (jobs -p) */ |
| 3207 | #define SHOW_PIDS 0x02 /* show individual pids, not just one line per job */ |
| 3208 | #define SHOW_CHANGED 0x04 /* only jobs whose state has changed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3209 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3210 | /* |
| 3211 | * A job structure contains information about a job. A job is either a |
| 3212 | * single process or a set of processes contained in a pipeline. In the |
| 3213 | * latter case, pidlist will be non-NULL, and will point to a -1 terminated |
| 3214 | * array of pids. |
| 3215 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3216 | struct procstat { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3217 | pid_t ps_pid; /* process id */ |
| 3218 | int ps_status; /* last process status from wait() */ |
| 3219 | char *ps_cmd; /* text of command being run */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3220 | }; |
| 3221 | |
| 3222 | struct job { |
| 3223 | struct procstat ps0; /* status of process */ |
| 3224 | struct procstat *ps; /* status or processes when more than one */ |
| 3225 | #if JOBS |
| 3226 | int stopstatus; /* status of a stopped job */ |
| 3227 | #endif |
| 3228 | uint32_t |
| 3229 | nprocs: 16, /* number of processes */ |
| 3230 | state: 8, |
| 3231 | #define JOBRUNNING 0 /* at least one proc running */ |
| 3232 | #define JOBSTOPPED 1 /* all procs are stopped */ |
| 3233 | #define JOBDONE 2 /* all procs are completed */ |
| 3234 | #if JOBS |
| 3235 | sigint: 1, /* job was killed by SIGINT */ |
| 3236 | jobctl: 1, /* job running under job control */ |
| 3237 | #endif |
| 3238 | waited: 1, /* true if this entry has been waited for */ |
| 3239 | used: 1, /* true if this entry is in used */ |
| 3240 | changed: 1; /* true if status has changed */ |
| 3241 | struct job *prev_job; /* previous job */ |
| 3242 | }; |
| 3243 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3244 | static struct job *makejob(/*union node *,*/ int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3245 | static int forkshell(struct job *, union node *, int); |
| 3246 | static int waitforjob(struct job *); |
| 3247 | |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3248 | #if !JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3249 | enum { doing_jobctl = 0 }; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3250 | #define setjobctl(on) do {} while (0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3251 | #else |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3252 | static smallint doing_jobctl; //references:8 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3253 | static void setjobctl(int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3254 | #endif |
| 3255 | |
| 3256 | /* |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3257 | * Ignore a signal. |
| 3258 | */ |
| 3259 | static void |
| 3260 | ignoresig(int signo) |
| 3261 | { |
| 3262 | /* Avoid unnecessary system calls. Is it already SIG_IGNed? */ |
| 3263 | if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { |
| 3264 | /* No, need to do it */ |
| 3265 | signal(signo, SIG_IGN); |
| 3266 | } |
| 3267 | sigmode[signo - 1] = S_HARD_IGN; |
| 3268 | } |
| 3269 | |
| 3270 | /* |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3271 | * Only one usage site - in setsignal() |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3272 | */ |
| 3273 | static void |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3274 | signal_handler(int signo) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3275 | { |
| 3276 | gotsig[signo - 1] = 1; |
| 3277 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3278 | if (signo == SIGINT && !trap[SIGINT]) { |
| 3279 | if (!suppress_int) { |
| 3280 | pending_sig = 0; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3281 | raise_interrupt(); /* does not return */ |
| 3282 | } |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3283 | pending_int = 1; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3284 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3285 | pending_sig = signo; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | /* |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3290 | * Set the signal handler for the specified signal. The routine figures |
| 3291 | * out what it should be set to. |
| 3292 | */ |
| 3293 | static void |
| 3294 | setsignal(int signo) |
| 3295 | { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3296 | char *t; |
| 3297 | char cur_act, new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3298 | struct sigaction act; |
| 3299 | |
| 3300 | t = trap[signo]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3301 | new_act = S_DFL; |
| 3302 | if (t != NULL) { /* trap for this sig is set */ |
| 3303 | new_act = S_CATCH; |
| 3304 | if (t[0] == '\0') /* trap is "": ignore this sig */ |
| 3305 | new_act = S_IGN; |
| 3306 | } |
| 3307 | |
| 3308 | if (rootshell && new_act == S_DFL) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3309 | switch (signo) { |
| 3310 | case SIGINT: |
| 3311 | if (iflag || minusc || sflag == 0) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3312 | new_act = S_CATCH; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3313 | break; |
| 3314 | case SIGQUIT: |
| 3315 | #if DEBUG |
| 3316 | if (debug) |
| 3317 | break; |
| 3318 | #endif |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3319 | /* man bash: |
| 3320 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 3321 | * commands run by bash have signal handlers |
| 3322 | * set to the values inherited by the shell |
| 3323 | * from its parent". */ |
| 3324 | new_act = S_IGN; |
| 3325 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3326 | case SIGTERM: |
| 3327 | if (iflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3328 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3329 | break; |
| 3330 | #if JOBS |
| 3331 | case SIGTSTP: |
| 3332 | case SIGTTOU: |
| 3333 | if (mflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3334 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3335 | break; |
| 3336 | #endif |
| 3337 | } |
| 3338 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3339 | //TODO: if !rootshell, we reset SIGQUIT to DFL, |
| 3340 | //whereas we have to restore it to what shell got on entry |
| 3341 | //from the parent. See comment above |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3342 | |
| 3343 | t = &sigmode[signo - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3344 | cur_act = *t; |
| 3345 | if (cur_act == 0) { |
| 3346 | /* current setting is not yet known */ |
| 3347 | if (sigaction(signo, NULL, &act)) { |
| 3348 | /* pretend it worked; maybe we should give a warning, |
| 3349 | * but other shells don't. We don't alter sigmode, |
| 3350 | * so we retry every time. |
| 3351 | * btw, in Linux it never fails. --vda */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3352 | return; |
| 3353 | } |
| 3354 | if (act.sa_handler == SIG_IGN) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3355 | cur_act = S_HARD_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3356 | if (mflag |
| 3357 | && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU) |
| 3358 | ) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3359 | cur_act = S_IGN; /* don't hard ignore these */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3360 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3361 | } |
| 3362 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3363 | if (cur_act == S_HARD_IGN || cur_act == new_act) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3364 | return; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3365 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3366 | act.sa_handler = SIG_DFL; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3367 | switch (new_act) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3368 | case S_CATCH: |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3369 | act.sa_handler = signal_handler; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3370 | act.sa_flags = 0; /* matters only if !DFL and !IGN */ |
| 3371 | sigfillset(&act.sa_mask); /* ditto */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3372 | break; |
| 3373 | case S_IGN: |
| 3374 | act.sa_handler = SIG_IGN; |
| 3375 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3376 | } |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 3377 | sigaction_set(signo, &act); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3378 | |
| 3379 | *t = new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
| 3382 | /* mode flags for set_curjob */ |
| 3383 | #define CUR_DELETE 2 |
| 3384 | #define CUR_RUNNING 1 |
| 3385 | #define CUR_STOPPED 0 |
| 3386 | |
| 3387 | /* mode flags for dowait */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3388 | #define DOWAIT_NONBLOCK WNOHANG |
| 3389 | #define DOWAIT_BLOCK 0 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3390 | |
| 3391 | #if JOBS |
| 3392 | /* pgrp of shell on invocation */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3393 | static int initialpgrp; //references:2 |
| 3394 | static int ttyfd = -1; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3395 | #endif |
| 3396 | /* array of jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3397 | static struct job *jobtab; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3398 | /* size of array */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3399 | static unsigned njobs; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3400 | /* current job */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3401 | static struct job *curjob; //lots |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3402 | /* number of presumed living untracked jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3403 | static int jobless; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3404 | |
| 3405 | static void |
| 3406 | set_curjob(struct job *jp, unsigned mode) |
| 3407 | { |
| 3408 | struct job *jp1; |
| 3409 | struct job **jpp, **curp; |
| 3410 | |
| 3411 | /* first remove from list */ |
| 3412 | jpp = curp = &curjob; |
| 3413 | do { |
| 3414 | jp1 = *jpp; |
| 3415 | if (jp1 == jp) |
| 3416 | break; |
| 3417 | jpp = &jp1->prev_job; |
| 3418 | } while (1); |
| 3419 | *jpp = jp1->prev_job; |
| 3420 | |
| 3421 | /* Then re-insert in correct position */ |
| 3422 | jpp = curp; |
| 3423 | switch (mode) { |
| 3424 | default: |
| 3425 | #if DEBUG |
| 3426 | abort(); |
| 3427 | #endif |
| 3428 | case CUR_DELETE: |
| 3429 | /* job being deleted */ |
| 3430 | break; |
| 3431 | case CUR_RUNNING: |
| 3432 | /* newly created job or backgrounded job, |
| 3433 | put after all stopped jobs. */ |
| 3434 | do { |
| 3435 | jp1 = *jpp; |
| 3436 | #if JOBS |
| 3437 | if (!jp1 || jp1->state != JOBSTOPPED) |
| 3438 | #endif |
| 3439 | break; |
| 3440 | jpp = &jp1->prev_job; |
| 3441 | } while (1); |
| 3442 | /* FALLTHROUGH */ |
| 3443 | #if JOBS |
| 3444 | case CUR_STOPPED: |
| 3445 | #endif |
| 3446 | /* newly stopped job - becomes curjob */ |
| 3447 | jp->prev_job = *jpp; |
| 3448 | *jpp = jp; |
| 3449 | break; |
| 3450 | } |
| 3451 | } |
| 3452 | |
| 3453 | #if JOBS || DEBUG |
| 3454 | static int |
| 3455 | jobno(const struct job *jp) |
| 3456 | { |
| 3457 | return jp - jobtab + 1; |
| 3458 | } |
| 3459 | #endif |
| 3460 | |
| 3461 | /* |
| 3462 | * Convert a job name to a job structure. |
| 3463 | */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 3464 | #if !JOBS |
| 3465 | #define getjob(name, getctl) getjob(name) |
| 3466 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3467 | static struct job * |
| 3468 | getjob(const char *name, int getctl) |
| 3469 | { |
| 3470 | struct job *jp; |
| 3471 | struct job *found; |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3472 | const char *err_msg = "%s: no such job"; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3473 | unsigned num; |
| 3474 | int c; |
| 3475 | const char *p; |
| 3476 | char *(*match)(const char *, const char *); |
| 3477 | |
| 3478 | jp = curjob; |
| 3479 | p = name; |
| 3480 | if (!p) |
| 3481 | goto currentjob; |
| 3482 | |
| 3483 | if (*p != '%') |
| 3484 | goto err; |
| 3485 | |
| 3486 | c = *++p; |
| 3487 | if (!c) |
| 3488 | goto currentjob; |
| 3489 | |
| 3490 | if (!p[1]) { |
| 3491 | if (c == '+' || c == '%') { |
| 3492 | currentjob: |
| 3493 | err_msg = "No current job"; |
| 3494 | goto check; |
| 3495 | } |
| 3496 | if (c == '-') { |
| 3497 | if (jp) |
| 3498 | jp = jp->prev_job; |
| 3499 | err_msg = "No previous job"; |
| 3500 | check: |
| 3501 | if (!jp) |
| 3502 | goto err; |
| 3503 | goto gotit; |
| 3504 | } |
| 3505 | } |
| 3506 | |
| 3507 | if (is_number(p)) { |
| 3508 | num = atoi(p); |
| 3509 | if (num < njobs) { |
| 3510 | jp = jobtab + num - 1; |
| 3511 | if (jp->used) |
| 3512 | goto gotit; |
| 3513 | goto err; |
| 3514 | } |
| 3515 | } |
| 3516 | |
| 3517 | match = prefix; |
| 3518 | if (*p == '?') { |
| 3519 | match = strstr; |
| 3520 | p++; |
| 3521 | } |
| 3522 | |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3523 | found = NULL; |
| 3524 | while (jp) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3525 | if (match(jp->ps[0].ps_cmd, p)) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3526 | if (found) |
| 3527 | goto err; |
| 3528 | found = jp; |
| 3529 | err_msg = "%s: ambiguous"; |
| 3530 | } |
| 3531 | jp = jp->prev_job; |
| 3532 | } |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3533 | if (!found) |
| 3534 | goto err; |
| 3535 | jp = found; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3536 | |
| 3537 | gotit: |
| 3538 | #if JOBS |
| 3539 | err_msg = "job %s not created under job control"; |
| 3540 | if (getctl && jp->jobctl == 0) |
| 3541 | goto err; |
| 3542 | #endif |
| 3543 | return jp; |
| 3544 | err: |
| 3545 | ash_msg_and_raise_error(err_msg, name); |
| 3546 | } |
| 3547 | |
| 3548 | /* |
| 3549 | * Mark a job structure as unused. |
| 3550 | */ |
| 3551 | static void |
| 3552 | freejob(struct job *jp) |
| 3553 | { |
| 3554 | struct procstat *ps; |
| 3555 | int i; |
| 3556 | |
| 3557 | INT_OFF; |
| 3558 | for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3559 | if (ps->ps_cmd != nullstr) |
| 3560 | free(ps->ps_cmd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3561 | } |
| 3562 | if (jp->ps != &jp->ps0) |
| 3563 | free(jp->ps); |
| 3564 | jp->used = 0; |
| 3565 | set_curjob(jp, CUR_DELETE); |
| 3566 | INT_ON; |
| 3567 | } |
| 3568 | |
| 3569 | #if JOBS |
| 3570 | static void |
| 3571 | xtcsetpgrp(int fd, pid_t pgrp) |
| 3572 | { |
| 3573 | if (tcsetpgrp(fd, pgrp)) |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 3574 | ash_msg_and_raise_error("can't set tty process group (%m)"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | /* |
| 3578 | * Turn job control on and off. |
| 3579 | * |
| 3580 | * Note: This code assumes that the third arg to ioctl is a character |
| 3581 | * pointer, which is true on Berkeley systems but not System V. Since |
| 3582 | * System V doesn't have job control yet, this isn't a problem now. |
| 3583 | * |
| 3584 | * Called with interrupts off. |
| 3585 | */ |
| 3586 | static void |
| 3587 | setjobctl(int on) |
| 3588 | { |
| 3589 | int fd; |
| 3590 | int pgrp; |
| 3591 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3592 | if (on == doing_jobctl || rootshell == 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3593 | return; |
| 3594 | if (on) { |
| 3595 | int ofd; |
| 3596 | ofd = fd = open(_PATH_TTY, O_RDWR); |
| 3597 | if (fd < 0) { |
| 3598 | /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails. |
| 3599 | * That sometimes helps to acquire controlling tty. |
| 3600 | * Obviously, a workaround for bugs when someone |
| 3601 | * failed to provide a controlling tty to bash! :) */ |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3602 | fd = 2; |
| 3603 | while (!isatty(fd)) |
| 3604 | if (--fd < 0) |
| 3605 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3606 | } |
| 3607 | fd = fcntl(fd, F_DUPFD, 10); |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3608 | if (ofd >= 0) |
| 3609 | close(ofd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3610 | if (fd < 0) |
| 3611 | goto out; |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3612 | /* fd is a tty at this point */ |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 3613 | close_on_exec_on(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3614 | do { /* while we are in the background */ |
| 3615 | pgrp = tcgetpgrp(fd); |
| 3616 | if (pgrp < 0) { |
| 3617 | out: |
| 3618 | ash_msg("can't access tty; job control turned off"); |
| 3619 | mflag = on = 0; |
| 3620 | goto close; |
| 3621 | } |
| 3622 | if (pgrp == getpgrp()) |
| 3623 | break; |
| 3624 | killpg(0, SIGTTIN); |
| 3625 | } while (1); |
| 3626 | initialpgrp = pgrp; |
| 3627 | |
| 3628 | setsignal(SIGTSTP); |
| 3629 | setsignal(SIGTTOU); |
| 3630 | setsignal(SIGTTIN); |
| 3631 | pgrp = rootpid; |
| 3632 | setpgid(0, pgrp); |
| 3633 | xtcsetpgrp(fd, pgrp); |
| 3634 | } else { |
| 3635 | /* turning job control off */ |
| 3636 | fd = ttyfd; |
| 3637 | pgrp = initialpgrp; |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3638 | /* was xtcsetpgrp, but this can make exiting ash |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3639 | * loop forever if pty is already deleted */ |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3640 | tcsetpgrp(fd, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3641 | setpgid(0, pgrp); |
| 3642 | setsignal(SIGTSTP); |
| 3643 | setsignal(SIGTTOU); |
| 3644 | setsignal(SIGTTIN); |
| 3645 | close: |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3646 | if (fd >= 0) |
| 3647 | close(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3648 | fd = -1; |
| 3649 | } |
| 3650 | ttyfd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3651 | doing_jobctl = on; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3654 | static int FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3655 | killcmd(int argc, char **argv) |
| 3656 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3657 | int i = 1; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3658 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3659 | do { |
| 3660 | if (argv[i][0] == '%') { |
| 3661 | struct job *jp = getjob(argv[i], 0); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3662 | unsigned pid = jp->ps[0].ps_pid; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3663 | /* Enough space for ' -NNN<nul>' */ |
| 3664 | argv[i] = alloca(sizeof(int)*3 + 3); |
| 3665 | /* kill_main has matching code to expect |
| 3666 | * leading space. Needed to not confuse |
| 3667 | * negative pids with "kill -SIGNAL_NO" syntax */ |
| 3668 | sprintf(argv[i], " -%u", pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3669 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3670 | } while (argv[++i]); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3671 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3672 | return kill_main(argc, argv); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3673 | } |
| 3674 | |
| 3675 | static void |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3676 | showpipe(struct job *jp /*, FILE *out*/) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3677 | { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3678 | struct procstat *ps; |
| 3679 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3680 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3681 | psend = jp->ps + jp->nprocs; |
| 3682 | for (ps = jp->ps + 1; ps < psend; ps++) |
| 3683 | printf(" | %s", ps->ps_cmd); |
| 3684 | outcslow('\n', stdout); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3685 | flush_stdout_stderr(); |
| 3686 | } |
| 3687 | |
| 3688 | |
| 3689 | static int |
| 3690 | restartjob(struct job *jp, int mode) |
| 3691 | { |
| 3692 | struct procstat *ps; |
| 3693 | int i; |
| 3694 | int status; |
| 3695 | pid_t pgid; |
| 3696 | |
| 3697 | INT_OFF; |
| 3698 | if (jp->state == JOBDONE) |
| 3699 | goto out; |
| 3700 | jp->state = JOBRUNNING; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3701 | pgid = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3702 | if (mode == FORK_FG) |
| 3703 | xtcsetpgrp(ttyfd, pgid); |
| 3704 | killpg(pgid, SIGCONT); |
| 3705 | ps = jp->ps; |
| 3706 | i = jp->nprocs; |
| 3707 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3708 | if (WIFSTOPPED(ps->ps_status)) { |
| 3709 | ps->ps_status = -1; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3710 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3711 | ps++; |
| 3712 | } while (--i); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3713 | out: |
| 3714 | status = (mode == FORK_FG) ? waitforjob(jp) : 0; |
| 3715 | INT_ON; |
| 3716 | return status; |
| 3717 | } |
| 3718 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3719 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3720 | fg_bgcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3721 | { |
| 3722 | struct job *jp; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3723 | int mode; |
| 3724 | int retval; |
| 3725 | |
| 3726 | mode = (**argv == 'f') ? FORK_FG : FORK_BG; |
| 3727 | nextopt(nullstr); |
| 3728 | argv = argptr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3729 | do { |
| 3730 | jp = getjob(*argv, 1); |
| 3731 | if (mode == FORK_BG) { |
| 3732 | set_curjob(jp, CUR_RUNNING); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3733 | printf("[%d] ", jobno(jp)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3734 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3735 | out1str(jp->ps[0].ps_cmd); |
| 3736 | showpipe(jp /*, stdout*/); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3737 | retval = restartjob(jp, mode); |
| 3738 | } while (*argv && *++argv); |
| 3739 | return retval; |
| 3740 | } |
| 3741 | #endif |
| 3742 | |
| 3743 | static int |
| 3744 | sprint_status(char *s, int status, int sigonly) |
| 3745 | { |
| 3746 | int col; |
| 3747 | int st; |
| 3748 | |
| 3749 | col = 0; |
| 3750 | if (!WIFEXITED(status)) { |
| 3751 | #if JOBS |
| 3752 | if (WIFSTOPPED(status)) |
| 3753 | st = WSTOPSIG(status); |
| 3754 | else |
| 3755 | #endif |
| 3756 | st = WTERMSIG(status); |
| 3757 | if (sigonly) { |
| 3758 | if (st == SIGINT || st == SIGPIPE) |
| 3759 | goto out; |
| 3760 | #if JOBS |
| 3761 | if (WIFSTOPPED(status)) |
| 3762 | goto out; |
| 3763 | #endif |
| 3764 | } |
| 3765 | st &= 0x7f; |
| 3766 | col = fmtstr(s, 32, strsignal(st)); |
| 3767 | if (WCOREDUMP(status)) { |
| 3768 | col += fmtstr(s + col, 16, " (core dumped)"); |
| 3769 | } |
| 3770 | } else if (!sigonly) { |
| 3771 | st = WEXITSTATUS(status); |
| 3772 | if (st) |
| 3773 | col = fmtstr(s, 16, "Done(%d)", st); |
| 3774 | else |
| 3775 | col = fmtstr(s, 16, "Done"); |
| 3776 | } |
| 3777 | out: |
| 3778 | return col; |
| 3779 | } |
| 3780 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3781 | static int |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3782 | dowait(int wait_flags, struct job *job) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3783 | { |
| 3784 | int pid; |
| 3785 | int status; |
| 3786 | struct job *jp; |
| 3787 | struct job *thisjob; |
| 3788 | int state; |
| 3789 | |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 3790 | TRACE(("dowait(0x%x) called\n", wait_flags)); |
| 3791 | |
| 3792 | /* Do a wait system call. If job control is compiled in, we accept |
| 3793 | * stopped processes. wait_flags may have WNOHANG, preventing blocking. |
| 3794 | * NB: _not_ safe_waitpid, we need to detect EINTR */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3795 | if (doing_jobctl) |
| 3796 | wait_flags |= WUNTRACED; |
| 3797 | pid = waitpid(-1, &status, wait_flags); |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 3798 | TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n", |
| 3799 | pid, status, errno, strerror(errno))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3800 | if (pid <= 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3801 | return pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3802 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3803 | INT_OFF; |
| 3804 | thisjob = NULL; |
| 3805 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3806 | struct procstat *ps; |
| 3807 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3808 | if (jp->state == JOBDONE) |
| 3809 | continue; |
| 3810 | state = JOBDONE; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3811 | ps = jp->ps; |
| 3812 | psend = ps + jp->nprocs; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3813 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3814 | if (ps->ps_pid == pid) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3815 | TRACE(("Job %d: changing status of proc %d " |
| 3816 | "from 0x%x to 0x%x\n", |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3817 | jobno(jp), pid, ps->ps_status, status)); |
| 3818 | ps->ps_status = status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3819 | thisjob = jp; |
| 3820 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3821 | if (ps->ps_status == -1) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3822 | state = JOBRUNNING; |
| 3823 | #if JOBS |
| 3824 | if (state == JOBRUNNING) |
| 3825 | continue; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3826 | if (WIFSTOPPED(ps->ps_status)) { |
| 3827 | jp->stopstatus = ps->ps_status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3828 | state = JOBSTOPPED; |
| 3829 | } |
| 3830 | #endif |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3831 | } while (++ps < psend); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3832 | if (thisjob) |
| 3833 | goto gotjob; |
| 3834 | } |
| 3835 | #if JOBS |
| 3836 | if (!WIFSTOPPED(status)) |
| 3837 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3838 | jobless--; |
| 3839 | goto out; |
| 3840 | |
| 3841 | gotjob: |
| 3842 | if (state != JOBRUNNING) { |
| 3843 | thisjob->changed = 1; |
| 3844 | |
| 3845 | if (thisjob->state != state) { |
| 3846 | TRACE(("Job %d: changing state from %d to %d\n", |
| 3847 | jobno(thisjob), thisjob->state, state)); |
| 3848 | thisjob->state = state; |
| 3849 | #if JOBS |
| 3850 | if (state == JOBSTOPPED) { |
| 3851 | set_curjob(thisjob, CUR_STOPPED); |
| 3852 | } |
| 3853 | #endif |
| 3854 | } |
| 3855 | } |
| 3856 | |
| 3857 | out: |
| 3858 | INT_ON; |
| 3859 | |
| 3860 | if (thisjob && thisjob == job) { |
| 3861 | char s[48 + 1]; |
| 3862 | int len; |
| 3863 | |
| 3864 | len = sprint_status(s, status, 1); |
| 3865 | if (len) { |
| 3866 | s[len] = '\n'; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3867 | s[len + 1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3868 | out2str(s); |
| 3869 | } |
| 3870 | } |
| 3871 | return pid; |
| 3872 | } |
| 3873 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3874 | static int |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 3875 | blocking_wait_with_raise_on_sig(void) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3876 | { |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 3877 | pid_t pid = dowait(DOWAIT_BLOCK, NULL); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3878 | if (pid <= 0 && pending_sig) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3879 | raise_exception(EXSIG); |
| 3880 | return pid; |
| 3881 | } |
| 3882 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3883 | #if JOBS |
| 3884 | static void |
| 3885 | showjob(FILE *out, struct job *jp, int mode) |
| 3886 | { |
| 3887 | struct procstat *ps; |
| 3888 | struct procstat *psend; |
| 3889 | int col; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 3890 | int indent_col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3891 | char s[80]; |
| 3892 | |
| 3893 | ps = jp->ps; |
| 3894 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3895 | if (mode & SHOW_ONLY_PGID) { /* jobs -p */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3896 | /* just output process (group) id of pipeline */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3897 | fprintf(out, "%d\n", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3898 | return; |
| 3899 | } |
| 3900 | |
| 3901 | col = fmtstr(s, 16, "[%d] ", jobno(jp)); |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 3902 | indent_col = col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3903 | |
| 3904 | if (jp == curjob) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3905 | s[col - 3] = '+'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3906 | else if (curjob && jp == curjob->prev_job) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3907 | s[col - 3] = '-'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3908 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3909 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3910 | col += fmtstr(s + col, 16, "%d ", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3911 | |
| 3912 | psend = ps + jp->nprocs; |
| 3913 | |
| 3914 | if (jp->state == JOBRUNNING) { |
| 3915 | strcpy(s + col, "Running"); |
| 3916 | col += sizeof("Running") - 1; |
| 3917 | } else { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3918 | int status = psend[-1].ps_status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3919 | if (jp->state == JOBSTOPPED) |
| 3920 | status = jp->stopstatus; |
| 3921 | col += sprint_status(s + col, status, 0); |
| 3922 | } |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3923 | /* By now, "[JOBID]* [maybe PID] STATUS" is printed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3924 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3925 | /* This loop either prints "<cmd1> | <cmd2> | <cmd3>" line |
| 3926 | * or prints several "PID | <cmdN>" lines, |
| 3927 | * depending on SHOW_PIDS bit. |
| 3928 | * We do not print status of individual processes |
| 3929 | * between PID and <cmdN>. bash does it, but not very well: |
| 3930 | * first line shows overall job status, not process status, |
| 3931 | * making it impossible to know 1st process status. |
| 3932 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3933 | goto start; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3934 | do { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3935 | /* for each process */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3936 | s[0] = '\0'; |
| 3937 | col = 33; |
| 3938 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3939 | col = fmtstr(s, 48, "\n%*c%d ", indent_col, ' ', ps->ps_pid) - 1; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3940 | start: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3941 | fprintf(out, "%s%*c%s%s", |
| 3942 | s, |
| 3943 | 33 - col >= 0 ? 33 - col : 0, ' ', |
| 3944 | ps == jp->ps ? "" : "| ", |
| 3945 | ps->ps_cmd |
| 3946 | ); |
| 3947 | } while (++ps != psend); |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3948 | outcslow('\n', out); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3949 | |
| 3950 | jp->changed = 0; |
| 3951 | |
| 3952 | if (jp->state == JOBDONE) { |
| 3953 | TRACE(("showjob: freeing job %d\n", jobno(jp))); |
| 3954 | freejob(jp); |
| 3955 | } |
| 3956 | } |
| 3957 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3958 | /* |
| 3959 | * Print a list of jobs. If "change" is nonzero, only print jobs whose |
| 3960 | * statuses have changed since the last call to showjobs. |
| 3961 | */ |
| 3962 | static void |
| 3963 | showjobs(FILE *out, int mode) |
| 3964 | { |
| 3965 | struct job *jp; |
| 3966 | |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 3967 | TRACE(("showjobs(0x%x) called\n", mode)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3968 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3969 | /* Handle all finished jobs */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3970 | while (dowait(DOWAIT_NONBLOCK, NULL) > 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3971 | continue; |
| 3972 | |
| 3973 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3974 | if (!(mode & SHOW_CHANGED) || jp->changed) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3975 | showjob(out, jp, mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3976 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3977 | } |
| 3978 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3979 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3980 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3981 | jobscmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3982 | { |
| 3983 | int mode, m; |
| 3984 | |
| 3985 | mode = 0; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3986 | while ((m = nextopt("lp")) != '\0') { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3987 | if (m == 'l') |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3988 | mode |= SHOW_PIDS; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3989 | else |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3990 | mode |= SHOW_ONLY_PGID; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
| 3993 | argv = argptr; |
| 3994 | if (*argv) { |
| 3995 | do |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3996 | showjob(stdout, getjob(*argv, 0), mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3997 | while (*++argv); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3998 | } else { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3999 | showjobs(stdout, mode); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4000 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4001 | |
| 4002 | return 0; |
| 4003 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4004 | #endif /* JOBS */ |
| 4005 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4006 | /* Called only on finished or stopped jobs (no members are running) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4007 | static int |
| 4008 | getstatus(struct job *job) |
| 4009 | { |
| 4010 | int status; |
| 4011 | int retval; |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4012 | struct procstat *ps; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4013 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4014 | /* Fetch last member's status */ |
| 4015 | ps = job->ps + job->nprocs - 1; |
| 4016 | status = ps->ps_status; |
| 4017 | if (pipefail) { |
| 4018 | /* "set -o pipefail" mode: use last _nonzero_ status */ |
| 4019 | while (status == 0 && --ps >= job->ps) |
| 4020 | status = ps->ps_status; |
| 4021 | } |
| 4022 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4023 | retval = WEXITSTATUS(status); |
| 4024 | if (!WIFEXITED(status)) { |
| 4025 | #if JOBS |
| 4026 | retval = WSTOPSIG(status); |
| 4027 | if (!WIFSTOPPED(status)) |
| 4028 | #endif |
| 4029 | { |
| 4030 | /* XXX: limits number of signals */ |
| 4031 | retval = WTERMSIG(status); |
| 4032 | #if JOBS |
| 4033 | if (retval == SIGINT) |
| 4034 | job->sigint = 1; |
| 4035 | #endif |
| 4036 | } |
| 4037 | retval += 128; |
| 4038 | } |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4039 | TRACE(("getstatus: job %d, nproc %d, status 0x%x, retval 0x%x\n", |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4040 | jobno(job), job->nprocs, status, retval)); |
| 4041 | return retval; |
| 4042 | } |
| 4043 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4044 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4045 | waitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4046 | { |
| 4047 | struct job *job; |
| 4048 | int retval; |
| 4049 | struct job *jp; |
| 4050 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4051 | if (pending_sig) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4052 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4053 | |
| 4054 | nextopt(nullstr); |
| 4055 | retval = 0; |
| 4056 | |
| 4057 | argv = argptr; |
| 4058 | if (!*argv) { |
| 4059 | /* wait for all jobs */ |
| 4060 | for (;;) { |
| 4061 | jp = curjob; |
| 4062 | while (1) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4063 | if (!jp) /* no running procs */ |
| 4064 | goto ret; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4065 | if (jp->state == JOBRUNNING) |
| 4066 | break; |
| 4067 | jp->waited = 1; |
| 4068 | jp = jp->prev_job; |
| 4069 | } |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4070 | blocking_wait_with_raise_on_sig(); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4071 | /* man bash: |
| 4072 | * "When bash is waiting for an asynchronous command via |
| 4073 | * the wait builtin, the reception of a signal for which a trap |
| 4074 | * has been set will cause the wait builtin to return immediately |
| 4075 | * with an exit status greater than 128, immediately after which |
| 4076 | * the trap is executed." |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4077 | * |
| 4078 | * blocking_wait_with_raise_on_sig raises signal handlers |
| 4079 | * if it gets no pid (pid < 0). However, |
| 4080 | * if child sends us a signal *and immediately exits*, |
| 4081 | * blocking_wait_with_raise_on_sig gets pid > 0 |
| 4082 | * and does not handle pending_sig. Check this case: */ |
| 4083 | if (pending_sig) |
| 4084 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4085 | } |
| 4086 | } |
| 4087 | |
| 4088 | retval = 127; |
| 4089 | do { |
| 4090 | if (**argv != '%') { |
| 4091 | pid_t pid = number(*argv); |
| 4092 | job = curjob; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4093 | while (1) { |
| 4094 | if (!job) |
| 4095 | goto repeat; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4096 | if (job->ps[job->nprocs - 1].ps_pid == pid) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4097 | break; |
| 4098 | job = job->prev_job; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4099 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4100 | } else |
| 4101 | job = getjob(*argv, 0); |
| 4102 | /* loop until process terminated or stopped */ |
| 4103 | while (job->state == JOBRUNNING) |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4104 | blocking_wait_with_raise_on_sig(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4105 | job->waited = 1; |
| 4106 | retval = getstatus(job); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4107 | repeat: ; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4108 | } while (*++argv); |
| 4109 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4110 | ret: |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4111 | return retval; |
| 4112 | } |
| 4113 | |
| 4114 | static struct job * |
| 4115 | growjobtab(void) |
| 4116 | { |
| 4117 | size_t len; |
| 4118 | ptrdiff_t offset; |
| 4119 | struct job *jp, *jq; |
| 4120 | |
| 4121 | len = njobs * sizeof(*jp); |
| 4122 | jq = jobtab; |
| 4123 | jp = ckrealloc(jq, len + 4 * sizeof(*jp)); |
| 4124 | |
| 4125 | offset = (char *)jp - (char *)jq; |
| 4126 | if (offset) { |
| 4127 | /* Relocate pointers */ |
| 4128 | size_t l = len; |
| 4129 | |
| 4130 | jq = (struct job *)((char *)jq + l); |
| 4131 | while (l) { |
| 4132 | l -= sizeof(*jp); |
| 4133 | jq--; |
| 4134 | #define joff(p) ((struct job *)((char *)(p) + l)) |
| 4135 | #define jmove(p) (p) = (void *)((char *)(p) + offset) |
| 4136 | if (joff(jp)->ps == &jq->ps0) |
| 4137 | jmove(joff(jp)->ps); |
| 4138 | if (joff(jp)->prev_job) |
| 4139 | jmove(joff(jp)->prev_job); |
| 4140 | } |
| 4141 | if (curjob) |
| 4142 | jmove(curjob); |
| 4143 | #undef joff |
| 4144 | #undef jmove |
| 4145 | } |
| 4146 | |
| 4147 | njobs += 4; |
| 4148 | jobtab = jp; |
| 4149 | jp = (struct job *)((char *)jp + len); |
| 4150 | jq = jp + 3; |
| 4151 | do { |
| 4152 | jq->used = 0; |
| 4153 | } while (--jq >= jp); |
| 4154 | return jp; |
| 4155 | } |
| 4156 | |
| 4157 | /* |
| 4158 | * Return a new job structure. |
| 4159 | * Called with interrupts off. |
| 4160 | */ |
| 4161 | static struct job * |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4162 | makejob(/*union node *node,*/ int nprocs) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4163 | { |
| 4164 | int i; |
| 4165 | struct job *jp; |
| 4166 | |
| 4167 | for (i = njobs, jp = jobtab; ; jp++) { |
| 4168 | if (--i < 0) { |
| 4169 | jp = growjobtab(); |
| 4170 | break; |
| 4171 | } |
| 4172 | if (jp->used == 0) |
| 4173 | break; |
| 4174 | if (jp->state != JOBDONE || !jp->waited) |
| 4175 | continue; |
| 4176 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4177 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4178 | continue; |
| 4179 | #endif |
| 4180 | freejob(jp); |
| 4181 | break; |
| 4182 | } |
| 4183 | memset(jp, 0, sizeof(*jp)); |
| 4184 | #if JOBS |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4185 | /* jp->jobctl is a bitfield. |
| 4186 | * "jp->jobctl |= jobctl" likely to give awful code */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4187 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4188 | jp->jobctl = 1; |
| 4189 | #endif |
| 4190 | jp->prev_job = curjob; |
| 4191 | curjob = jp; |
| 4192 | jp->used = 1; |
| 4193 | jp->ps = &jp->ps0; |
| 4194 | if (nprocs > 1) { |
| 4195 | jp->ps = ckmalloc(nprocs * sizeof(struct procstat)); |
| 4196 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4197 | TRACE(("makejob(%d) returns %%%d\n", nprocs, |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4198 | jobno(jp))); |
| 4199 | return jp; |
| 4200 | } |
| 4201 | |
| 4202 | #if JOBS |
| 4203 | /* |
| 4204 | * Return a string identifying a command (to be printed by the |
| 4205 | * jobs command). |
| 4206 | */ |
| 4207 | static char *cmdnextc; |
| 4208 | |
| 4209 | static void |
| 4210 | cmdputs(const char *s) |
| 4211 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4212 | static const char vstype[VSTYPE + 1][3] = { |
| 4213 | "", "}", "-", "+", "?", "=", |
| 4214 | "%", "%%", "#", "##" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4215 | IF_ASH_BASH_COMPAT(, ":", "/", "//") |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4216 | }; |
| 4217 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4218 | const char *p, *str; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4219 | char cc[2]; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4220 | char *nextc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 4221 | unsigned char c; |
| 4222 | unsigned char subtype = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4223 | int quoted = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4224 | |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4225 | cc[1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4226 | nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc); |
| 4227 | p = s; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4228 | while ((c = *p++) != '\0') { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 4229 | str = NULL; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4230 | switch (c) { |
| 4231 | case CTLESC: |
| 4232 | c = *p++; |
| 4233 | break; |
| 4234 | case CTLVAR: |
| 4235 | subtype = *p++; |
| 4236 | if ((subtype & VSTYPE) == VSLENGTH) |
| 4237 | str = "${#"; |
| 4238 | else |
| 4239 | str = "${"; |
| 4240 | if (!(subtype & VSQUOTE) == !(quoted & 1)) |
| 4241 | goto dostr; |
| 4242 | quoted ^= 1; |
| 4243 | c = '"'; |
| 4244 | break; |
| 4245 | case CTLENDVAR: |
| 4246 | str = "\"}" + !(quoted & 1); |
| 4247 | quoted >>= 1; |
| 4248 | subtype = 0; |
| 4249 | goto dostr; |
| 4250 | case CTLBACKQ: |
| 4251 | str = "$(...)"; |
| 4252 | goto dostr; |
| 4253 | case CTLBACKQ+CTLQUOTE: |
| 4254 | str = "\"$(...)\""; |
| 4255 | goto dostr; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4256 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4257 | case CTLARI: |
| 4258 | str = "$(("; |
| 4259 | goto dostr; |
| 4260 | case CTLENDARI: |
| 4261 | str = "))"; |
| 4262 | goto dostr; |
| 4263 | #endif |
| 4264 | case CTLQUOTEMARK: |
| 4265 | quoted ^= 1; |
| 4266 | c = '"'; |
| 4267 | break; |
| 4268 | case '=': |
| 4269 | if (subtype == 0) |
| 4270 | break; |
| 4271 | if ((subtype & VSTYPE) != VSNORMAL) |
| 4272 | quoted <<= 1; |
| 4273 | str = vstype[subtype & VSTYPE]; |
| 4274 | if (subtype & VSNUL) |
| 4275 | c = ':'; |
| 4276 | else |
| 4277 | goto checkstr; |
| 4278 | break; |
| 4279 | case '\'': |
| 4280 | case '\\': |
| 4281 | case '"': |
| 4282 | case '$': |
| 4283 | /* These can only happen inside quotes */ |
| 4284 | cc[0] = c; |
| 4285 | str = cc; |
| 4286 | c = '\\'; |
| 4287 | break; |
| 4288 | default: |
| 4289 | break; |
| 4290 | } |
| 4291 | USTPUTC(c, nextc); |
| 4292 | checkstr: |
| 4293 | if (!str) |
| 4294 | continue; |
| 4295 | dostr: |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4296 | while ((c = *str++) != '\0') { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4297 | USTPUTC(c, nextc); |
| 4298 | } |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4299 | } /* while *p++ not NUL */ |
| 4300 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4301 | if (quoted & 1) { |
| 4302 | USTPUTC('"', nextc); |
| 4303 | } |
| 4304 | *nextc = 0; |
| 4305 | cmdnextc = nextc; |
| 4306 | } |
| 4307 | |
| 4308 | /* cmdtxt() and cmdlist() call each other */ |
| 4309 | static void cmdtxt(union node *n); |
| 4310 | |
| 4311 | static void |
| 4312 | cmdlist(union node *np, int sep) |
| 4313 | { |
| 4314 | for (; np; np = np->narg.next) { |
| 4315 | if (!sep) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4316 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4317 | cmdtxt(np); |
| 4318 | if (sep && np->narg.next) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4319 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4320 | } |
| 4321 | } |
| 4322 | |
| 4323 | static void |
| 4324 | cmdtxt(union node *n) |
| 4325 | { |
| 4326 | union node *np; |
| 4327 | struct nodelist *lp; |
| 4328 | const char *p; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4329 | |
| 4330 | if (!n) |
| 4331 | return; |
| 4332 | switch (n->type) { |
| 4333 | default: |
| 4334 | #if DEBUG |
| 4335 | abort(); |
| 4336 | #endif |
| 4337 | case NPIPE: |
| 4338 | lp = n->npipe.cmdlist; |
| 4339 | for (;;) { |
| 4340 | cmdtxt(lp->n); |
| 4341 | lp = lp->next; |
| 4342 | if (!lp) |
| 4343 | break; |
| 4344 | cmdputs(" | "); |
| 4345 | } |
| 4346 | break; |
| 4347 | case NSEMI: |
| 4348 | p = "; "; |
| 4349 | goto binop; |
| 4350 | case NAND: |
| 4351 | p = " && "; |
| 4352 | goto binop; |
| 4353 | case NOR: |
| 4354 | p = " || "; |
| 4355 | binop: |
| 4356 | cmdtxt(n->nbinary.ch1); |
| 4357 | cmdputs(p); |
| 4358 | n = n->nbinary.ch2; |
| 4359 | goto donode; |
| 4360 | case NREDIR: |
| 4361 | case NBACKGND: |
| 4362 | n = n->nredir.n; |
| 4363 | goto donode; |
| 4364 | case NNOT: |
| 4365 | cmdputs("!"); |
| 4366 | n = n->nnot.com; |
| 4367 | donode: |
| 4368 | cmdtxt(n); |
| 4369 | break; |
| 4370 | case NIF: |
| 4371 | cmdputs("if "); |
| 4372 | cmdtxt(n->nif.test); |
| 4373 | cmdputs("; then "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4374 | if (n->nif.elsepart) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4375 | cmdtxt(n->nif.ifpart); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4376 | cmdputs("; else "); |
| 4377 | n = n->nif.elsepart; |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4378 | } else { |
| 4379 | n = n->nif.ifpart; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4380 | } |
| 4381 | p = "; fi"; |
| 4382 | goto dotail; |
| 4383 | case NSUBSHELL: |
| 4384 | cmdputs("("); |
| 4385 | n = n->nredir.n; |
| 4386 | p = ")"; |
| 4387 | goto dotail; |
| 4388 | case NWHILE: |
| 4389 | p = "while "; |
| 4390 | goto until; |
| 4391 | case NUNTIL: |
| 4392 | p = "until "; |
| 4393 | until: |
| 4394 | cmdputs(p); |
| 4395 | cmdtxt(n->nbinary.ch1); |
| 4396 | n = n->nbinary.ch2; |
| 4397 | p = "; done"; |
| 4398 | dodo: |
| 4399 | cmdputs("; do "); |
| 4400 | dotail: |
| 4401 | cmdtxt(n); |
| 4402 | goto dotail2; |
| 4403 | case NFOR: |
| 4404 | cmdputs("for "); |
| 4405 | cmdputs(n->nfor.var); |
| 4406 | cmdputs(" in "); |
| 4407 | cmdlist(n->nfor.args, 1); |
| 4408 | n = n->nfor.body; |
| 4409 | p = "; done"; |
| 4410 | goto dodo; |
| 4411 | case NDEFUN: |
| 4412 | cmdputs(n->narg.text); |
| 4413 | p = "() { ... }"; |
| 4414 | goto dotail2; |
| 4415 | case NCMD: |
| 4416 | cmdlist(n->ncmd.args, 1); |
| 4417 | cmdlist(n->ncmd.redirect, 0); |
| 4418 | break; |
| 4419 | case NARG: |
| 4420 | p = n->narg.text; |
| 4421 | dotail2: |
| 4422 | cmdputs(p); |
| 4423 | break; |
| 4424 | case NHERE: |
| 4425 | case NXHERE: |
| 4426 | p = "<<..."; |
| 4427 | goto dotail2; |
| 4428 | case NCASE: |
| 4429 | cmdputs("case "); |
| 4430 | cmdputs(n->ncase.expr->narg.text); |
| 4431 | cmdputs(" in "); |
| 4432 | for (np = n->ncase.cases; np; np = np->nclist.next) { |
| 4433 | cmdtxt(np->nclist.pattern); |
| 4434 | cmdputs(") "); |
| 4435 | cmdtxt(np->nclist.body); |
| 4436 | cmdputs(";; "); |
| 4437 | } |
| 4438 | p = "esac"; |
| 4439 | goto dotail2; |
| 4440 | case NTO: |
| 4441 | p = ">"; |
| 4442 | goto redir; |
| 4443 | case NCLOBBER: |
| 4444 | p = ">|"; |
| 4445 | goto redir; |
| 4446 | case NAPPEND: |
| 4447 | p = ">>"; |
| 4448 | goto redir; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 4449 | #if ENABLE_ASH_BASH_COMPAT |
| 4450 | case NTO2: |
| 4451 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4452 | case NTOFD: |
| 4453 | p = ">&"; |
| 4454 | goto redir; |
| 4455 | case NFROM: |
| 4456 | p = "<"; |
| 4457 | goto redir; |
| 4458 | case NFROMFD: |
| 4459 | p = "<&"; |
| 4460 | goto redir; |
| 4461 | case NFROMTO: |
| 4462 | p = "<>"; |
| 4463 | redir: |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4464 | cmdputs(utoa(n->nfile.fd)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4465 | cmdputs(p); |
| 4466 | if (n->type == NTOFD || n->type == NFROMFD) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4467 | cmdputs(utoa(n->ndup.dupfd)); |
| 4468 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4469 | } |
| 4470 | n = n->nfile.fname; |
| 4471 | goto donode; |
| 4472 | } |
| 4473 | } |
| 4474 | |
| 4475 | static char * |
| 4476 | commandtext(union node *n) |
| 4477 | { |
| 4478 | char *name; |
| 4479 | |
| 4480 | STARTSTACKSTR(cmdnextc); |
| 4481 | cmdtxt(n); |
| 4482 | name = stackblock(); |
| 4483 | TRACE(("commandtext: name %p, end %p\n\t\"%s\"\n", |
| 4484 | name, cmdnextc, cmdnextc)); |
| 4485 | return ckstrdup(name); |
| 4486 | } |
| 4487 | #endif /* JOBS */ |
| 4488 | |
| 4489 | /* |
| 4490 | * Fork off a subshell. If we are doing job control, give the subshell its |
| 4491 | * own process group. Jp is a job structure that the job is to be added to. |
| 4492 | * N is the command that will be evaluated by the child. Both jp and n may |
| 4493 | * be NULL. The mode parameter can be one of the following: |
| 4494 | * FORK_FG - Fork off a foreground process. |
| 4495 | * FORK_BG - Fork off a background process. |
| 4496 | * FORK_NOJOB - Like FORK_FG, but don't give the process its own |
| 4497 | * process group even if job control is on. |
| 4498 | * |
| 4499 | * When job control is turned off, background processes have their standard |
| 4500 | * input redirected to /dev/null (except for the second and later processes |
| 4501 | * in a pipeline). |
| 4502 | * |
| 4503 | * Called with interrupts off. |
| 4504 | */ |
| 4505 | /* |
| 4506 | * Clear traps on a fork. |
| 4507 | */ |
| 4508 | static void |
| 4509 | clear_traps(void) |
| 4510 | { |
| 4511 | char **tp; |
| 4512 | |
| 4513 | for (tp = trap; tp < &trap[NSIG]; tp++) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4514 | if (*tp && **tp) { /* trap not NULL or "" (SIG_IGN) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4515 | INT_OFF; |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 4516 | if (trap_ptr == trap) |
| 4517 | free(*tp); |
| 4518 | /* else: it "belongs" to trap_ptr vector, don't free */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4519 | *tp = NULL; |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 4520 | if ((tp - trap) != 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4521 | setsignal(tp - trap); |
| 4522 | INT_ON; |
| 4523 | } |
| 4524 | } |
| 4525 | } |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4526 | |
| 4527 | /* Lives far away from here, needed for forkchild */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4528 | static void closescript(void); |
Denis Vlasenko | 4177022 | 2007-10-07 18:02:52 +0000 | [diff] [blame] | 4529 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4530 | /* Called after fork(), in child */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 4531 | static NOINLINE void |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4532 | forkchild(struct job *jp, union node *n, int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4533 | { |
| 4534 | int oldlvl; |
| 4535 | |
| 4536 | TRACE(("Child shell %d\n", getpid())); |
| 4537 | oldlvl = shlvl; |
| 4538 | shlvl++; |
| 4539 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4540 | /* man bash: "Non-builtin commands run by bash have signal handlers |
| 4541 | * set to the values inherited by the shell from its parent". |
| 4542 | * Do we do it correctly? */ |
| 4543 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4544 | closescript(); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4545 | |
| 4546 | if (mode == FORK_NOJOB /* is it `xxx` ? */ |
| 4547 | && n && n->type == NCMD /* is it single cmd? */ |
| 4548 | /* && n->ncmd.args->type == NARG - always true? */ |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 4549 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "trap") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4550 | && n->ncmd.args->narg.next == NULL /* "trap" with no arguments */ |
| 4551 | /* && n->ncmd.args->narg.backquote == NULL - do we need to check this? */ |
| 4552 | ) { |
| 4553 | TRACE(("Trap hack\n")); |
| 4554 | /* Awful hack for `trap` or $(trap). |
| 4555 | * |
| 4556 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 4557 | * contains an example where "trap" is executed in a subshell: |
| 4558 | * |
| 4559 | * save_traps=$(trap) |
| 4560 | * ... |
| 4561 | * eval "$save_traps" |
| 4562 | * |
| 4563 | * Standard does not say that "trap" in subshell shall print |
| 4564 | * parent shell's traps. It only says that its output |
| 4565 | * must have suitable form, but then, in the above example |
| 4566 | * (which is not supposed to be normative), it implies that. |
| 4567 | * |
| 4568 | * bash (and probably other shell) does implement it |
| 4569 | * (traps are reset to defaults, but "trap" still shows them), |
| 4570 | * but as a result, "trap" logic is hopelessly messed up: |
| 4571 | * |
| 4572 | * # trap |
| 4573 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 4574 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 4575 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 4576 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 4577 | * trap -- 'echo Ho' SIGWINCH |
| 4578 | * # echo `(trap)` <--- in subshell in subshell - output |
| 4579 | * trap -- 'echo Ho' SIGWINCH |
| 4580 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 4581 | * trap -- 'echo Ho' SIGWINCH |
| 4582 | * |
| 4583 | * The rules when to forget and when to not forget traps |
| 4584 | * get really complex and nonsensical. |
| 4585 | * |
| 4586 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 4587 | */ |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 4588 | /* Save trap handler strings for trap builtin to print */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 4589 | trap_ptr = memcpy(xmalloc(sizeof(trap)), trap, sizeof(trap)); |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 4590 | /* Fall through into clearing traps */ |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4591 | } |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 4592 | clear_traps(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4593 | #if JOBS |
| 4594 | /* do job control only in root shell */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4595 | doing_jobctl = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4596 | if (mode != FORK_NOJOB && jp->jobctl && !oldlvl) { |
| 4597 | pid_t pgrp; |
| 4598 | |
| 4599 | if (jp->nprocs == 0) |
| 4600 | pgrp = getpid(); |
| 4601 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4602 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4603 | /* this can fail because we are doing it in the parent also */ |
| 4604 | setpgid(0, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4605 | if (mode == FORK_FG) |
| 4606 | xtcsetpgrp(ttyfd, pgrp); |
| 4607 | setsignal(SIGTSTP); |
| 4608 | setsignal(SIGTTOU); |
| 4609 | } else |
| 4610 | #endif |
| 4611 | if (mode == FORK_BG) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4612 | /* man bash: "When job control is not in effect, |
| 4613 | * asynchronous commands ignore SIGINT and SIGQUIT" */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4614 | ignoresig(SIGINT); |
| 4615 | ignoresig(SIGQUIT); |
| 4616 | if (jp->nprocs == 0) { |
| 4617 | close(0); |
| 4618 | if (open(bb_dev_null, O_RDONLY) != 0) |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 4619 | ash_msg_and_raise_error("can't open '%s'", bb_dev_null); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4620 | } |
| 4621 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4622 | if (!oldlvl) { |
| 4623 | if (iflag) { /* why if iflag only? */ |
| 4624 | setsignal(SIGINT); |
| 4625 | setsignal(SIGTERM); |
| 4626 | } |
| 4627 | /* man bash: |
| 4628 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 4629 | * commands run by bash have signal handlers |
| 4630 | * set to the values inherited by the shell |
| 4631 | * from its parent". |
| 4632 | * Take care of the second rule: */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4633 | setsignal(SIGQUIT); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4634 | } |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4635 | #if JOBS |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4636 | if (n && n->type == NCMD |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 4637 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "jobs") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4638 | ) { |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4639 | TRACE(("Job hack\n")); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4640 | /* "jobs": we do not want to clear job list for it, |
| 4641 | * instead we remove only _its_ own_ job from job list. |
| 4642 | * This makes "jobs .... | cat" more useful. |
| 4643 | */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4644 | freejob(curjob); |
| 4645 | return; |
| 4646 | } |
| 4647 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4648 | for (jp = curjob; jp; jp = jp->prev_job) |
| 4649 | freejob(jp); |
| 4650 | jobless = 0; |
| 4651 | } |
| 4652 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4653 | /* Called after fork(), in parent */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 4654 | #if !JOBS |
| 4655 | #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid) |
| 4656 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4657 | static void |
| 4658 | forkparent(struct job *jp, union node *n, int mode, pid_t pid) |
| 4659 | { |
| 4660 | TRACE(("In parent shell: child = %d\n", pid)); |
| 4661 | if (!jp) { |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4662 | while (jobless && dowait(DOWAIT_NONBLOCK, NULL) > 0) |
| 4663 | continue; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4664 | jobless++; |
| 4665 | return; |
| 4666 | } |
| 4667 | #if JOBS |
| 4668 | if (mode != FORK_NOJOB && jp->jobctl) { |
| 4669 | int pgrp; |
| 4670 | |
| 4671 | if (jp->nprocs == 0) |
| 4672 | pgrp = pid; |
| 4673 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4674 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4675 | /* This can fail because we are doing it in the child also */ |
| 4676 | setpgid(pid, pgrp); |
| 4677 | } |
| 4678 | #endif |
| 4679 | if (mode == FORK_BG) { |
| 4680 | backgndpid = pid; /* set $! */ |
| 4681 | set_curjob(jp, CUR_RUNNING); |
| 4682 | } |
| 4683 | if (jp) { |
| 4684 | struct procstat *ps = &jp->ps[jp->nprocs++]; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4685 | ps->ps_pid = pid; |
| 4686 | ps->ps_status = -1; |
| 4687 | ps->ps_cmd = nullstr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4688 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4689 | if (doing_jobctl && n) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4690 | ps->ps_cmd = commandtext(n); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4691 | #endif |
| 4692 | } |
| 4693 | } |
| 4694 | |
| 4695 | static int |
| 4696 | forkshell(struct job *jp, union node *n, int mode) |
| 4697 | { |
| 4698 | int pid; |
| 4699 | |
| 4700 | TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode)); |
| 4701 | pid = fork(); |
| 4702 | if (pid < 0) { |
| 4703 | TRACE(("Fork failed, errno=%d", errno)); |
| 4704 | if (jp) |
| 4705 | freejob(jp); |
Denis Vlasenko | fa0b56d | 2008-07-01 16:09:07 +0000 | [diff] [blame] | 4706 | ash_msg_and_raise_error("can't fork"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4707 | } |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4708 | if (pid == 0) { |
| 4709 | CLEAR_RANDOM_T(&random_gen); /* or else $RANDOM repeats in child */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4710 | forkchild(jp, n, mode); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4711 | } else { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4712 | forkparent(jp, n, mode, pid); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4713 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4714 | return pid; |
| 4715 | } |
| 4716 | |
| 4717 | /* |
| 4718 | * Wait for job to finish. |
| 4719 | * |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4720 | * Under job control we have the problem that while a child process |
| 4721 | * is running interrupts generated by the user are sent to the child |
| 4722 | * but not to the shell. This means that an infinite loop started by |
| 4723 | * an interactive user may be hard to kill. With job control turned off, |
| 4724 | * an interactive user may place an interactive program inside a loop. |
| 4725 | * If the interactive program catches interrupts, the user doesn't want |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4726 | * these interrupts to also abort the loop. The approach we take here |
| 4727 | * is to have the shell ignore interrupt signals while waiting for a |
| 4728 | * foreground process to terminate, and then send itself an interrupt |
| 4729 | * signal if the child process was terminated by an interrupt signal. |
| 4730 | * Unfortunately, some programs want to do a bit of cleanup and then |
| 4731 | * exit on interrupt; unless these processes terminate themselves by |
| 4732 | * sending a signal to themselves (instead of calling exit) they will |
| 4733 | * confuse this approach. |
| 4734 | * |
| 4735 | * Called with interrupts off. |
| 4736 | */ |
| 4737 | static int |
| 4738 | waitforjob(struct job *jp) |
| 4739 | { |
| 4740 | int st; |
| 4741 | |
| 4742 | TRACE(("waitforjob(%%%d) called\n", jobno(jp))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4743 | |
| 4744 | INT_OFF; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4745 | while (jp->state == JOBRUNNING) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4746 | /* In non-interactive shells, we _can_ get |
| 4747 | * a keyboard signal here and be EINTRed, |
| 4748 | * but we just loop back, waiting for command to complete. |
| 4749 | * |
| 4750 | * man bash: |
| 4751 | * "If bash is waiting for a command to complete and receives |
| 4752 | * a signal for which a trap has been set, the trap |
| 4753 | * will not be executed until the command completes." |
| 4754 | * |
| 4755 | * Reality is that even if trap is not set, bash |
| 4756 | * will not act on the signal until command completes. |
| 4757 | * Try this. sleep5intoff.c: |
| 4758 | * #include <signal.h> |
| 4759 | * #include <unistd.h> |
| 4760 | * int main() { |
| 4761 | * sigset_t set; |
| 4762 | * sigemptyset(&set); |
| 4763 | * sigaddset(&set, SIGINT); |
| 4764 | * sigaddset(&set, SIGQUIT); |
| 4765 | * sigprocmask(SIG_BLOCK, &set, NULL); |
| 4766 | * sleep(5); |
| 4767 | * return 0; |
| 4768 | * } |
| 4769 | * $ bash -c './sleep5intoff; echo hi' |
| 4770 | * ^C^C^C^C <--- pressing ^C once a second |
| 4771 | * $ _ |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4772 | * $ bash -c './sleep5intoff; echo hi' |
| 4773 | * ^\^\^\^\hi <--- pressing ^\ (SIGQUIT) |
| 4774 | * $ _ |
| 4775 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4776 | dowait(DOWAIT_BLOCK, jp); |
| 4777 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4778 | INT_ON; |
| 4779 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4780 | st = getstatus(jp); |
| 4781 | #if JOBS |
| 4782 | if (jp->jobctl) { |
| 4783 | xtcsetpgrp(ttyfd, rootpid); |
| 4784 | /* |
| 4785 | * This is truly gross. |
| 4786 | * If we're doing job control, then we did a TIOCSPGRP which |
| 4787 | * caused us (the shell) to no longer be in the controlling |
| 4788 | * session -- so we wouldn't have seen any ^C/SIGINT. So, we |
| 4789 | * intuit from the subprocess exit status whether a SIGINT |
| 4790 | * occurred, and if so interrupt ourselves. Yuck. - mycroft |
| 4791 | */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4792 | if (jp->sigint) /* TODO: do the same with all signals */ |
| 4793 | raise(SIGINT); /* ... by raise(jp->sig) instead? */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4794 | } |
| 4795 | if (jp->state == JOBDONE) |
| 4796 | #endif |
| 4797 | freejob(jp); |
| 4798 | return st; |
| 4799 | } |
| 4800 | |
| 4801 | /* |
| 4802 | * return 1 if there are stopped jobs, otherwise 0 |
| 4803 | */ |
| 4804 | static int |
| 4805 | stoppedjobs(void) |
| 4806 | { |
| 4807 | struct job *jp; |
| 4808 | int retval; |
| 4809 | |
| 4810 | retval = 0; |
| 4811 | if (job_warning) |
| 4812 | goto out; |
| 4813 | jp = curjob; |
| 4814 | if (jp && jp->state == JOBSTOPPED) { |
| 4815 | out2str("You have stopped jobs.\n"); |
| 4816 | job_warning = 2; |
| 4817 | retval++; |
| 4818 | } |
| 4819 | out: |
| 4820 | return retval; |
| 4821 | } |
| 4822 | |
| 4823 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4824 | /* ============ redir.c |
| 4825 | * |
| 4826 | * Code for dealing with input/output redirection. |
| 4827 | */ |
| 4828 | |
| 4829 | #define EMPTY -2 /* marks an unused slot in redirtab */ |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4830 | #define CLOSED -3 /* marks a slot of previously-closed fd */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4831 | |
| 4832 | /* |
| 4833 | * Open a file in noclobber mode. |
| 4834 | * The code was copied from bash. |
| 4835 | */ |
| 4836 | static int |
| 4837 | noclobberopen(const char *fname) |
| 4838 | { |
| 4839 | int r, fd; |
| 4840 | struct stat finfo, finfo2; |
| 4841 | |
| 4842 | /* |
| 4843 | * If the file exists and is a regular file, return an error |
| 4844 | * immediately. |
| 4845 | */ |
| 4846 | r = stat(fname, &finfo); |
| 4847 | if (r == 0 && S_ISREG(finfo.st_mode)) { |
| 4848 | errno = EEXIST; |
| 4849 | return -1; |
| 4850 | } |
| 4851 | |
| 4852 | /* |
| 4853 | * If the file was not present (r != 0), make sure we open it |
| 4854 | * exclusively so that if it is created before we open it, our open |
| 4855 | * will fail. Make sure that we do not truncate an existing file. |
| 4856 | * Note that we don't turn on O_EXCL unless the stat failed -- if the |
| 4857 | * file was not a regular file, we leave O_EXCL off. |
| 4858 | */ |
| 4859 | if (r != 0) |
| 4860 | return open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 4861 | fd = open(fname, O_WRONLY|O_CREAT, 0666); |
| 4862 | |
| 4863 | /* If the open failed, return the file descriptor right away. */ |
| 4864 | if (fd < 0) |
| 4865 | return fd; |
| 4866 | |
| 4867 | /* |
| 4868 | * OK, the open succeeded, but the file may have been changed from a |
| 4869 | * non-regular file to a regular file between the stat and the open. |
| 4870 | * We are assuming that the O_EXCL open handles the case where FILENAME |
| 4871 | * did not exist and is symlinked to an existing file between the stat |
| 4872 | * and open. |
| 4873 | */ |
| 4874 | |
| 4875 | /* |
| 4876 | * If we can open it and fstat the file descriptor, and neither check |
| 4877 | * revealed that it was a regular file, and the file has not been |
| 4878 | * replaced, return the file descriptor. |
| 4879 | */ |
| 4880 | if (fstat(fd, &finfo2) == 0 && !S_ISREG(finfo2.st_mode) |
| 4881 | && finfo.st_dev == finfo2.st_dev && finfo.st_ino == finfo2.st_ino) |
| 4882 | return fd; |
| 4883 | |
| 4884 | /* The file has been replaced. badness. */ |
| 4885 | close(fd); |
| 4886 | errno = EEXIST; |
| 4887 | return -1; |
| 4888 | } |
| 4889 | |
| 4890 | /* |
| 4891 | * Handle here documents. Normally we fork off a process to write the |
| 4892 | * data to a pipe. If the document is short, we can stuff the data in |
| 4893 | * the pipe without forking. |
| 4894 | */ |
| 4895 | /* openhere needs this forward reference */ |
| 4896 | static void expandhere(union node *arg, int fd); |
| 4897 | static int |
| 4898 | openhere(union node *redir) |
| 4899 | { |
| 4900 | int pip[2]; |
| 4901 | size_t len = 0; |
| 4902 | |
| 4903 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 4904 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4905 | if (redir->type == NHERE) { |
| 4906 | len = strlen(redir->nhere.doc->narg.text); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4907 | if (len <= PIPE_BUF) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4908 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
| 4909 | goto out; |
| 4910 | } |
| 4911 | } |
| 4912 | if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4913 | /* child */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4914 | close(pip[0]); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4915 | ignoresig(SIGINT); //signal(SIGINT, SIG_IGN); |
| 4916 | ignoresig(SIGQUIT); //signal(SIGQUIT, SIG_IGN); |
| 4917 | ignoresig(SIGHUP); //signal(SIGHUP, SIG_IGN); |
| 4918 | ignoresig(SIGTSTP); //signal(SIGTSTP, SIG_IGN); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4919 | signal(SIGPIPE, SIG_DFL); |
| 4920 | if (redir->type == NHERE) |
| 4921 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4922 | else /* NXHERE */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4923 | expandhere(redir->nhere.doc, pip[1]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 4924 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4925 | } |
| 4926 | out: |
| 4927 | close(pip[1]); |
| 4928 | return pip[0]; |
| 4929 | } |
| 4930 | |
| 4931 | static int |
| 4932 | openredirect(union node *redir) |
| 4933 | { |
| 4934 | char *fname; |
| 4935 | int f; |
| 4936 | |
| 4937 | switch (redir->nfile.type) { |
| 4938 | case NFROM: |
| 4939 | fname = redir->nfile.expfname; |
| 4940 | f = open(fname, O_RDONLY); |
| 4941 | if (f < 0) |
| 4942 | goto eopen; |
| 4943 | break; |
| 4944 | case NFROMTO: |
| 4945 | fname = redir->nfile.expfname; |
| 4946 | f = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666); |
| 4947 | if (f < 0) |
| 4948 | goto ecreate; |
| 4949 | break; |
| 4950 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 4951 | #if ENABLE_ASH_BASH_COMPAT |
| 4952 | case NTO2: |
| 4953 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4954 | /* Take care of noclobber mode. */ |
| 4955 | if (Cflag) { |
| 4956 | fname = redir->nfile.expfname; |
| 4957 | f = noclobberopen(fname); |
| 4958 | if (f < 0) |
| 4959 | goto ecreate; |
| 4960 | break; |
| 4961 | } |
| 4962 | /* FALLTHROUGH */ |
| 4963 | case NCLOBBER: |
| 4964 | fname = redir->nfile.expfname; |
| 4965 | f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 4966 | if (f < 0) |
| 4967 | goto ecreate; |
| 4968 | break; |
| 4969 | case NAPPEND: |
| 4970 | fname = redir->nfile.expfname; |
| 4971 | f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666); |
| 4972 | if (f < 0) |
| 4973 | goto ecreate; |
| 4974 | break; |
| 4975 | default: |
| 4976 | #if DEBUG |
| 4977 | abort(); |
| 4978 | #endif |
| 4979 | /* Fall through to eliminate warning. */ |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 4980 | /* Our single caller does this itself */ |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 4981 | // case NTOFD: |
| 4982 | // case NFROMFD: |
| 4983 | // f = -1; |
| 4984 | // break; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4985 | case NHERE: |
| 4986 | case NXHERE: |
| 4987 | f = openhere(redir); |
| 4988 | break; |
| 4989 | } |
| 4990 | |
| 4991 | return f; |
| 4992 | ecreate: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 4993 | ash_msg_and_raise_error("can't create %s: %s", fname, errmsg(errno, "nonexistent directory")); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4994 | eopen: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 4995 | ash_msg_and_raise_error("can't open %s: %s", fname, errmsg(errno, "no such file")); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4996 | } |
| 4997 | |
| 4998 | /* |
| 4999 | * Copy a file descriptor to be >= to. Returns -1 |
| 5000 | * if the source file descriptor is closed, EMPTY if there are no unused |
| 5001 | * file descriptors left. |
| 5002 | */ |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5003 | /* 0x800..00: bit to set in "to" to request dup2 instead of fcntl(F_DUPFD). |
| 5004 | * old code was doing close(to) prior to copyfd() to achieve the same */ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5005 | enum { |
| 5006 | COPYFD_EXACT = (int)~(INT_MAX), |
| 5007 | COPYFD_RESTORE = (int)((unsigned)COPYFD_EXACT >> 1), |
| 5008 | }; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5009 | static int |
| 5010 | copyfd(int from, int to) |
| 5011 | { |
| 5012 | int newfd; |
| 5013 | |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5014 | if (to & COPYFD_EXACT) { |
| 5015 | to &= ~COPYFD_EXACT; |
| 5016 | /*if (from != to)*/ |
| 5017 | newfd = dup2(from, to); |
| 5018 | } else { |
| 5019 | newfd = fcntl(from, F_DUPFD, to); |
| 5020 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5021 | if (newfd < 0) { |
| 5022 | if (errno == EMFILE) |
| 5023 | return EMPTY; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5024 | /* Happens when source fd is not open: try "echo >&99" */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5025 | ash_msg_and_raise_error("%d: %m", from); |
| 5026 | } |
| 5027 | return newfd; |
| 5028 | } |
| 5029 | |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5030 | /* Struct def and variable are moved down to the first usage site */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5031 | struct two_fd_t { |
| 5032 | int orig, copy; |
| 5033 | }; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5034 | struct redirtab { |
| 5035 | struct redirtab *next; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5036 | int nullredirs; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5037 | int pair_count; |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 5038 | struct two_fd_t two_fd[]; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5039 | }; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5040 | #define redirlist (G_var.redirlist) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5041 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5042 | static int need_to_remember(struct redirtab *rp, int fd) |
| 5043 | { |
| 5044 | int i; |
| 5045 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5046 | if (!rp) /* remembering was not requested */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5047 | return 0; |
| 5048 | |
| 5049 | for (i = 0; i < rp->pair_count; i++) { |
| 5050 | if (rp->two_fd[i].orig == fd) { |
| 5051 | /* already remembered */ |
| 5052 | return 0; |
| 5053 | } |
| 5054 | } |
| 5055 | return 1; |
| 5056 | } |
| 5057 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5058 | /* "hidden" fd is a fd used to read scripts, or a copy of such */ |
| 5059 | static int is_hidden_fd(struct redirtab *rp, int fd) |
| 5060 | { |
| 5061 | int i; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5062 | struct parsefile *pf; |
| 5063 | |
| 5064 | if (fd == -1) |
| 5065 | return 0; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5066 | /* Check open scripts' fds */ |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5067 | pf = g_parsefile; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5068 | while (pf) { |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 5069 | /* We skip pf_fd == 0 case because of the following case: |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5070 | * $ ash # running ash interactively |
| 5071 | * $ . ./script.sh |
| 5072 | * and in script.sh: "exec 9>&0". |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 5073 | * Even though top-level pf_fd _is_ 0, |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5074 | * it's still ok to use it: "read" builtin uses it, |
| 5075 | * why should we cripple "exec" builtin? |
| 5076 | */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 5077 | if (pf->pf_fd > 0 && fd == pf->pf_fd) { |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5078 | return 1; |
| 5079 | } |
| 5080 | pf = pf->prev; |
| 5081 | } |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5082 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5083 | if (!rp) |
| 5084 | return 0; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5085 | /* Check saved fds of redirects */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5086 | fd |= COPYFD_RESTORE; |
| 5087 | for (i = 0; i < rp->pair_count; i++) { |
| 5088 | if (rp->two_fd[i].copy == fd) { |
| 5089 | return 1; |
| 5090 | } |
| 5091 | } |
| 5092 | return 0; |
| 5093 | } |
| 5094 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5095 | /* |
| 5096 | * Process a list of redirection commands. If the REDIR_PUSH flag is set, |
| 5097 | * old file descriptors are stashed away so that the redirection can be |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5098 | * undone by calling popredir. |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5099 | */ |
| 5100 | /* flags passed to redirect */ |
| 5101 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ |
| 5102 | #define REDIR_SAVEFD2 03 /* set preverrout */ |
| 5103 | static void |
| 5104 | redirect(union node *redir, int flags) |
| 5105 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5106 | struct redirtab *sv; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5107 | int sv_pos; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5108 | int i; |
| 5109 | int fd; |
| 5110 | int newfd; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5111 | int copied_fd2 = -1; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5112 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5113 | g_nullredirs++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5114 | if (!redir) { |
| 5115 | return; |
| 5116 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5117 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5118 | sv = NULL; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5119 | sv_pos = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5120 | INT_OFF; |
| 5121 | if (flags & REDIR_PUSH) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5122 | union node *tmp = redir; |
| 5123 | do { |
| 5124 | sv_pos++; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5125 | #if ENABLE_ASH_BASH_COMPAT |
Chris Metcalf | c3c1fb6 | 2010-01-08 13:18:06 +0100 | [diff] [blame] | 5126 | if (tmp->nfile.type == NTO2) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5127 | sv_pos++; |
| 5128 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5129 | tmp = tmp->nfile.next; |
| 5130 | } while (tmp); |
| 5131 | sv = ckmalloc(sizeof(*sv) + sv_pos * sizeof(sv->two_fd[0])); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5132 | sv->next = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5133 | sv->pair_count = sv_pos; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5134 | redirlist = sv; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5135 | sv->nullredirs = g_nullredirs - 1; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5136 | g_nullredirs = 0; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5137 | while (sv_pos > 0) { |
| 5138 | sv_pos--; |
| 5139 | sv->two_fd[sv_pos].orig = sv->two_fd[sv_pos].copy = EMPTY; |
| 5140 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5141 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5142 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5143 | do { |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5144 | int right_fd = -1; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5145 | fd = redir->nfile.fd; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5146 | if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) { |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5147 | right_fd = redir->ndup.dupfd; |
| 5148 | //bb_error_msg("doing %d > %d", fd, right_fd); |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5149 | /* redirect from/to same file descriptor? */ |
| 5150 | if (right_fd == fd) |
| 5151 | continue; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5152 | /* "echo >&10" and 10 is a fd opened to a sh script? */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5153 | if (is_hidden_fd(sv, right_fd)) { |
| 5154 | errno = EBADF; /* as if it is closed */ |
| 5155 | ash_msg_and_raise_error("%d: %m", right_fd); |
| 5156 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5157 | newfd = -1; |
| 5158 | } else { |
| 5159 | newfd = openredirect(redir); /* always >= 0 */ |
| 5160 | if (fd == newfd) { |
| 5161 | /* Descriptor wasn't open before redirect. |
| 5162 | * Mark it for close in the future */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5163 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5164 | goto remember_to_close; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5165 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5166 | continue; |
| 5167 | } |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5168 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5169 | #if ENABLE_ASH_BASH_COMPAT |
| 5170 | redirect_more: |
| 5171 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5172 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5173 | /* Copy old descriptor */ |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5174 | /* Careful to not accidentally "save" |
| 5175 | * to the same fd as right side fd in N>&M */ |
| 5176 | int minfd = right_fd < 10 ? 10 : right_fd + 1; |
| 5177 | i = fcntl(fd, F_DUPFD, minfd); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5178 | /* You'd expect copy to be CLOEXECed. Currently these extra "saved" fds |
| 5179 | * are closed in popredir() in the child, preventing them from leaking |
| 5180 | * into child. (popredir() also cleans up the mess in case of failures) |
| 5181 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5182 | if (i == -1) { |
| 5183 | i = errno; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5184 | if (i != EBADF) { |
| 5185 | /* Strange error (e.g. "too many files" EMFILE?) */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5186 | if (newfd >= 0) |
| 5187 | close(newfd); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5188 | errno = i; |
| 5189 | ash_msg_and_raise_error("%d: %m", fd); |
| 5190 | /* NOTREACHED */ |
| 5191 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5192 | /* EBADF: it is not open - good, remember to close it */ |
| 5193 | remember_to_close: |
| 5194 | i = CLOSED; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5195 | } else { /* fd is open, save its copy */ |
| 5196 | /* "exec fd>&-" should not close fds |
| 5197 | * which point to script file(s). |
| 5198 | * Force them to be restored afterwards */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5199 | if (is_hidden_fd(sv, fd)) |
| 5200 | i |= COPYFD_RESTORE; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5201 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5202 | if (fd == 2) |
| 5203 | copied_fd2 = i; |
| 5204 | sv->two_fd[sv_pos].orig = fd; |
| 5205 | sv->two_fd[sv_pos].copy = i; |
| 5206 | sv_pos++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5207 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5208 | if (newfd < 0) { |
| 5209 | /* NTOFD/NFROMFD: copy redir->ndup.dupfd to fd */ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5210 | if (redir->ndup.dupfd < 0) { /* "fd>&-" */ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5211 | /* Don't want to trigger debugging */ |
| 5212 | if (fd != -1) |
| 5213 | close(fd); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5214 | } else { |
| 5215 | copyfd(redir->ndup.dupfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5216 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5217 | } else if (fd != newfd) { /* move newfd to fd */ |
| 5218 | copyfd(newfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5219 | #if ENABLE_ASH_BASH_COMPAT |
| 5220 | if (!(redir->nfile.type == NTO2 && fd == 2)) |
| 5221 | #endif |
| 5222 | close(newfd); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5223 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5224 | #if ENABLE_ASH_BASH_COMPAT |
| 5225 | if (redir->nfile.type == NTO2 && fd == 1) { |
| 5226 | /* We already redirected it to fd 1, now copy it to 2 */ |
| 5227 | newfd = 1; |
| 5228 | fd = 2; |
| 5229 | goto redirect_more; |
| 5230 | } |
| 5231 | #endif |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5232 | } while ((redir = redir->nfile.next) != NULL); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5233 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5234 | INT_ON; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5235 | if ((flags & REDIR_SAVEFD2) && copied_fd2 >= 0) |
| 5236 | preverrout_fd = copied_fd2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5237 | } |
| 5238 | |
| 5239 | /* |
| 5240 | * Undo the effects of the last redirection. |
| 5241 | */ |
| 5242 | static void |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5243 | popredir(int drop, int restore) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5244 | { |
| 5245 | struct redirtab *rp; |
| 5246 | int i; |
| 5247 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5248 | if (--g_nullredirs >= 0) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5249 | return; |
| 5250 | INT_OFF; |
| 5251 | rp = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5252 | for (i = 0; i < rp->pair_count; i++) { |
| 5253 | int fd = rp->two_fd[i].orig; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5254 | int copy = rp->two_fd[i].copy; |
| 5255 | if (copy == CLOSED) { |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5256 | if (!drop) |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5257 | close(fd); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5258 | continue; |
| 5259 | } |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5260 | if (copy != EMPTY) { |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5261 | if (!drop || (restore && (copy & COPYFD_RESTORE))) { |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5262 | copy &= ~COPYFD_RESTORE; |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5263 | /*close(fd);*/ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5264 | copyfd(copy, fd | COPYFD_EXACT); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5265 | } |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5266 | close(copy & ~COPYFD_RESTORE); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5267 | } |
| 5268 | } |
| 5269 | redirlist = rp->next; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5270 | g_nullredirs = rp->nullredirs; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5271 | free(rp); |
| 5272 | INT_ON; |
| 5273 | } |
| 5274 | |
| 5275 | /* |
| 5276 | * Undo all redirections. Called on error or interrupt. |
| 5277 | */ |
| 5278 | |
| 5279 | /* |
| 5280 | * Discard all saved file descriptors. |
| 5281 | */ |
| 5282 | static void |
| 5283 | clearredir(int drop) |
| 5284 | { |
| 5285 | for (;;) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5286 | g_nullredirs = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5287 | if (!redirlist) |
| 5288 | break; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5289 | popredir(drop, /*restore:*/ 0); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5290 | } |
| 5291 | } |
| 5292 | |
| 5293 | static int |
| 5294 | redirectsafe(union node *redir, int flags) |
| 5295 | { |
| 5296 | int err; |
| 5297 | volatile int saveint; |
| 5298 | struct jmploc *volatile savehandler = exception_handler; |
| 5299 | struct jmploc jmploc; |
| 5300 | |
| 5301 | SAVE_INT(saveint); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5302 | /* "echo 9>/dev/null; echo >&9; echo result: $?" - result should be 1, not 2! */ |
| 5303 | err = setjmp(jmploc.loc); // huh?? was = setjmp(jmploc.loc) * 2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5304 | if (!err) { |
| 5305 | exception_handler = &jmploc; |
| 5306 | redirect(redir, flags); |
| 5307 | } |
| 5308 | exception_handler = savehandler; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 5309 | if (err && exception_type != EXERROR) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5310 | longjmp(exception_handler->loc, 1); |
| 5311 | RESTORE_INT(saveint); |
| 5312 | return err; |
| 5313 | } |
| 5314 | |
| 5315 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5316 | /* ============ Routines to expand arguments to commands |
| 5317 | * |
| 5318 | * We have to deal with backquotes, shell variables, and file metacharacters. |
| 5319 | */ |
| 5320 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5321 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5322 | static arith_t |
| 5323 | ash_arith(const char *s) |
| 5324 | { |
| 5325 | arith_eval_hooks_t math_hooks; |
| 5326 | arith_t result; |
| 5327 | int errcode = 0; |
| 5328 | |
| 5329 | math_hooks.lookupvar = lookupvar; |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 5330 | math_hooks.setvar = setvar2; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5331 | math_hooks.endofname = endofname; |
| 5332 | |
| 5333 | INT_OFF; |
| 5334 | result = arith(s, &errcode, &math_hooks); |
| 5335 | if (errcode < 0) { |
| 5336 | if (errcode == -3) |
| 5337 | ash_msg_and_raise_error("exponent less than 0"); |
| 5338 | if (errcode == -2) |
| 5339 | ash_msg_and_raise_error("divide by zero"); |
| 5340 | if (errcode == -5) |
| 5341 | ash_msg_and_raise_error("expression recursion loop detected"); |
| 5342 | raise_error_syntax(s); |
| 5343 | } |
| 5344 | INT_ON; |
| 5345 | |
| 5346 | return result; |
| 5347 | } |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5348 | #endif |
| 5349 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5350 | /* |
| 5351 | * expandarg flags |
| 5352 | */ |
| 5353 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ |
| 5354 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ |
| 5355 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ |
| 5356 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ |
| 5357 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ |
| 5358 | #define EXP_RECORD 0x20 /* need to record arguments for ifs breakup */ |
| 5359 | #define EXP_VARTILDE2 0x40 /* expand tildes after colons only */ |
| 5360 | #define EXP_WORD 0x80 /* expand word in parameter expansion */ |
| 5361 | #define EXP_QWORD 0x100 /* expand word in quoted parameter expansion */ |
| 5362 | /* |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5363 | * rmescape() flags |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5364 | */ |
| 5365 | #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */ |
| 5366 | #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ |
| 5367 | #define RMESCAPE_QUOTED 0x4 /* Remove CTLESC unless in quotes */ |
| 5368 | #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ |
| 5369 | #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ |
| 5370 | |
| 5371 | /* |
| 5372 | * Structure specifying which parts of the string should be searched |
| 5373 | * for IFS characters. |
| 5374 | */ |
| 5375 | struct ifsregion { |
| 5376 | struct ifsregion *next; /* next region in list */ |
| 5377 | int begoff; /* offset of start of region */ |
| 5378 | int endoff; /* offset of end of region */ |
| 5379 | int nulonly; /* search for nul bytes only */ |
| 5380 | }; |
| 5381 | |
| 5382 | struct arglist { |
| 5383 | struct strlist *list; |
| 5384 | struct strlist **lastp; |
| 5385 | }; |
| 5386 | |
| 5387 | /* output of current string */ |
| 5388 | static char *expdest; |
| 5389 | /* list of back quote expressions */ |
| 5390 | static struct nodelist *argbackq; |
| 5391 | /* first struct in list of ifs regions */ |
| 5392 | static struct ifsregion ifsfirst; |
| 5393 | /* last struct in list */ |
| 5394 | static struct ifsregion *ifslastp; |
| 5395 | /* holds expanded arg list */ |
| 5396 | static struct arglist exparg; |
| 5397 | |
| 5398 | /* |
| 5399 | * Our own itoa(). |
| 5400 | */ |
| 5401 | static int |
| 5402 | cvtnum(arith_t num) |
| 5403 | { |
| 5404 | int len; |
| 5405 | |
| 5406 | expdest = makestrspace(32, expdest); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5407 | len = fmtstr(expdest, 32, arith_t_fmt, num); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5408 | STADJUST(len, expdest); |
| 5409 | return len; |
| 5410 | } |
| 5411 | |
| 5412 | static size_t |
| 5413 | esclen(const char *start, const char *p) |
| 5414 | { |
| 5415 | size_t esc = 0; |
| 5416 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5417 | while (p > start && (unsigned char)*--p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5418 | esc++; |
| 5419 | } |
| 5420 | return esc; |
| 5421 | } |
| 5422 | |
| 5423 | /* |
| 5424 | * Remove any CTLESC characters from a string. |
| 5425 | */ |
| 5426 | static char * |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5427 | rmescapes(char *str, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5428 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5429 | static const char qchars[] ALIGN1 = { CTLESC, CTLQUOTEMARK, '\0' }; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 5430 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5431 | char *p, *q, *r; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5432 | unsigned inquotes; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5433 | unsigned protect_against_glob; |
| 5434 | unsigned globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5435 | |
| 5436 | p = strpbrk(str, qchars); |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5437 | if (!p) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5438 | return str; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5439 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5440 | q = p; |
| 5441 | r = str; |
| 5442 | if (flag & RMESCAPE_ALLOC) { |
| 5443 | size_t len = p - str; |
| 5444 | size_t fulllen = len + strlen(p) + 1; |
| 5445 | |
| 5446 | if (flag & RMESCAPE_GROW) { |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 5447 | int strloc = str - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5448 | r = makestrspace(fulllen, expdest); |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 5449 | /* p and str may be invalidated by makestrspace */ |
| 5450 | str = (char *)stackblock() + strloc; |
| 5451 | p = str + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5452 | } else if (flag & RMESCAPE_HEAP) { |
| 5453 | r = ckmalloc(fulllen); |
| 5454 | } else { |
| 5455 | r = stalloc(fulllen); |
| 5456 | } |
| 5457 | q = r; |
| 5458 | if (len > 0) { |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5459 | q = (char *)memcpy(q, str, len) + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5460 | } |
| 5461 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5462 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5463 | inquotes = (flag & RMESCAPE_QUOTED) ^ RMESCAPE_QUOTED; |
| 5464 | globbing = flag & RMESCAPE_GLOB; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5465 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5466 | while (*p) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5467 | if ((unsigned char)*p == CTLQUOTEMARK) { |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5468 | // TODO: if no RMESCAPE_QUOTED in flags, inquotes never becomes 0 |
| 5469 | // (alternates between RMESCAPE_QUOTED and ~RMESCAPE_QUOTED). Is it ok? |
| 5470 | // Note: both inquotes and protect_against_glob only affect whether |
| 5471 | // CTLESC,<ch> gets converted to <ch> or to \<ch> |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5472 | inquotes = ~inquotes; |
| 5473 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5474 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5475 | continue; |
| 5476 | } |
| 5477 | if (*p == '\\') { |
| 5478 | /* naked back slash */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5479 | protect_against_glob = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5480 | goto copy; |
| 5481 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5482 | if ((unsigned char)*p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5483 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5484 | if (protect_against_glob && inquotes && *p != '/') { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5485 | *q++ = '\\'; |
| 5486 | } |
| 5487 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5488 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5489 | copy: |
| 5490 | *q++ = *p++; |
| 5491 | } |
| 5492 | *q = '\0'; |
| 5493 | if (flag & RMESCAPE_GROW) { |
| 5494 | expdest = r; |
| 5495 | STADJUST(q - r + 1, expdest); |
| 5496 | } |
| 5497 | return r; |
| 5498 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5499 | #define pmatch(a, b) !fnmatch((a), (b), 0) |
| 5500 | |
| 5501 | /* |
| 5502 | * Prepare a pattern for a expmeta (internal glob(3)) call. |
| 5503 | * |
| 5504 | * Returns an stalloced string. |
| 5505 | */ |
| 5506 | static char * |
| 5507 | preglob(const char *pattern, int quoted, int flag) |
| 5508 | { |
| 5509 | flag |= RMESCAPE_GLOB; |
| 5510 | if (quoted) { |
| 5511 | flag |= RMESCAPE_QUOTED; |
| 5512 | } |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5513 | return rmescapes((char *)pattern, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5514 | } |
| 5515 | |
| 5516 | /* |
| 5517 | * Put a string on the stack. |
| 5518 | */ |
| 5519 | static void |
| 5520 | memtodest(const char *p, size_t len, int syntax, int quotes) |
| 5521 | { |
| 5522 | char *q = expdest; |
| 5523 | |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5524 | q = makestrspace(quotes ? len * 2 : len, q); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5525 | |
| 5526 | while (len--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5527 | unsigned char c = *p++; |
| 5528 | if (c == '\0') |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5529 | continue; |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5530 | if (quotes) { |
| 5531 | int n = SIT(c, syntax); |
| 5532 | if (n == CCTL || n == CBACK) |
| 5533 | USTPUTC(CTLESC, q); |
| 5534 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5535 | USTPUTC(c, q); |
| 5536 | } |
| 5537 | |
| 5538 | expdest = q; |
| 5539 | } |
| 5540 | |
| 5541 | static void |
| 5542 | strtodest(const char *p, int syntax, int quotes) |
| 5543 | { |
| 5544 | memtodest(p, strlen(p), syntax, quotes); |
| 5545 | } |
| 5546 | |
| 5547 | /* |
| 5548 | * Record the fact that we have to scan this region of the |
| 5549 | * string for IFS characters. |
| 5550 | */ |
| 5551 | static void |
| 5552 | recordregion(int start, int end, int nulonly) |
| 5553 | { |
| 5554 | struct ifsregion *ifsp; |
| 5555 | |
| 5556 | if (ifslastp == NULL) { |
| 5557 | ifsp = &ifsfirst; |
| 5558 | } else { |
| 5559 | INT_OFF; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 5560 | ifsp = ckzalloc(sizeof(*ifsp)); |
| 5561 | /*ifsp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5562 | ifslastp->next = ifsp; |
| 5563 | INT_ON; |
| 5564 | } |
| 5565 | ifslastp = ifsp; |
| 5566 | ifslastp->begoff = start; |
| 5567 | ifslastp->endoff = end; |
| 5568 | ifslastp->nulonly = nulonly; |
| 5569 | } |
| 5570 | |
| 5571 | static void |
| 5572 | removerecordregions(int endoff) |
| 5573 | { |
| 5574 | if (ifslastp == NULL) |
| 5575 | return; |
| 5576 | |
| 5577 | if (ifsfirst.endoff > endoff) { |
| 5578 | while (ifsfirst.next != NULL) { |
| 5579 | struct ifsregion *ifsp; |
| 5580 | INT_OFF; |
| 5581 | ifsp = ifsfirst.next->next; |
| 5582 | free(ifsfirst.next); |
| 5583 | ifsfirst.next = ifsp; |
| 5584 | INT_ON; |
| 5585 | } |
| 5586 | if (ifsfirst.begoff > endoff) |
| 5587 | ifslastp = NULL; |
| 5588 | else { |
| 5589 | ifslastp = &ifsfirst; |
| 5590 | ifsfirst.endoff = endoff; |
| 5591 | } |
| 5592 | return; |
| 5593 | } |
| 5594 | |
| 5595 | ifslastp = &ifsfirst; |
| 5596 | while (ifslastp->next && ifslastp->next->begoff < endoff) |
| 5597 | ifslastp=ifslastp->next; |
| 5598 | while (ifslastp->next != NULL) { |
| 5599 | struct ifsregion *ifsp; |
| 5600 | INT_OFF; |
| 5601 | ifsp = ifslastp->next->next; |
| 5602 | free(ifslastp->next); |
| 5603 | ifslastp->next = ifsp; |
| 5604 | INT_ON; |
| 5605 | } |
| 5606 | if (ifslastp->endoff > endoff) |
| 5607 | ifslastp->endoff = endoff; |
| 5608 | } |
| 5609 | |
| 5610 | static char * |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5611 | exptilde(char *startp, char *p, int flags) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5612 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5613 | unsigned char c; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5614 | char *name; |
| 5615 | struct passwd *pw; |
| 5616 | const char *home; |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 5617 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5618 | int startloc; |
| 5619 | |
| 5620 | name = p + 1; |
| 5621 | |
| 5622 | while ((c = *++p) != '\0') { |
| 5623 | switch (c) { |
| 5624 | case CTLESC: |
| 5625 | return startp; |
| 5626 | case CTLQUOTEMARK: |
| 5627 | return startp; |
| 5628 | case ':': |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5629 | if (flags & EXP_VARTILDE) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5630 | goto done; |
| 5631 | break; |
| 5632 | case '/': |
| 5633 | case CTLENDVAR: |
| 5634 | goto done; |
| 5635 | } |
| 5636 | } |
| 5637 | done: |
| 5638 | *p = '\0'; |
| 5639 | if (*name == '\0') { |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 5640 | home = lookupvar("HOME"); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5641 | } else { |
| 5642 | pw = getpwnam(name); |
| 5643 | if (pw == NULL) |
| 5644 | goto lose; |
| 5645 | home = pw->pw_dir; |
| 5646 | } |
| 5647 | if (!home || !*home) |
| 5648 | goto lose; |
| 5649 | *p = c; |
| 5650 | startloc = expdest - (char *)stackblock(); |
| 5651 | strtodest(home, SQSYNTAX, quotes); |
| 5652 | recordregion(startloc, expdest - (char *)stackblock(), 0); |
| 5653 | return p; |
| 5654 | lose: |
| 5655 | *p = c; |
| 5656 | return startp; |
| 5657 | } |
| 5658 | |
| 5659 | /* |
| 5660 | * Execute a command inside back quotes. If it's a builtin command, we |
| 5661 | * want to save its output in a block obtained from malloc. Otherwise |
| 5662 | * we fork off a subprocess and get the output of the command via a pipe. |
| 5663 | * Should be called with interrupts off. |
| 5664 | */ |
| 5665 | struct backcmd { /* result of evalbackcmd */ |
| 5666 | int fd; /* file descriptor to read from */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5667 | int nleft; /* number of chars in buffer */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 5668 | char *buf; /* buffer */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5669 | struct job *jp; /* job structure for command */ |
| 5670 | }; |
| 5671 | |
| 5672 | /* These forward decls are needed to use "eval" code for backticks handling: */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5673 | static uint8_t back_exitstatus; /* exit status of backquoted command */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5674 | #define EV_EXIT 01 /* exit after evaluating tree */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 5675 | static void evaltree(union node *, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5676 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 5677 | static void FAST_FUNC |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5678 | evalbackcmd(union node *n, struct backcmd *result) |
| 5679 | { |
| 5680 | int saveherefd; |
| 5681 | |
| 5682 | result->fd = -1; |
| 5683 | result->buf = NULL; |
| 5684 | result->nleft = 0; |
| 5685 | result->jp = NULL; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5686 | if (n == NULL) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5687 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5688 | |
| 5689 | saveherefd = herefd; |
| 5690 | herefd = -1; |
| 5691 | |
| 5692 | { |
| 5693 | int pip[2]; |
| 5694 | struct job *jp; |
| 5695 | |
| 5696 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 5697 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 5698 | jp = makejob(/*n,*/ 1); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5699 | if (forkshell(jp, n, FORK_NOJOB) == 0) { |
| 5700 | FORCE_INT_ON; |
| 5701 | close(pip[0]); |
| 5702 | if (pip[1] != 1) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5703 | /*close(1);*/ |
| 5704 | copyfd(pip[1], 1 | COPYFD_EXACT); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5705 | close(pip[1]); |
| 5706 | } |
| 5707 | eflag = 0; |
| 5708 | evaltree(n, EV_EXIT); /* actually evaltreenr... */ |
| 5709 | /* NOTREACHED */ |
| 5710 | } |
| 5711 | close(pip[1]); |
| 5712 | result->fd = pip[0]; |
| 5713 | result->jp = jp; |
| 5714 | } |
| 5715 | herefd = saveherefd; |
| 5716 | out: |
| 5717 | TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
| 5718 | result->fd, result->buf, result->nleft, result->jp)); |
| 5719 | } |
| 5720 | |
| 5721 | /* |
| 5722 | * Expand stuff in backwards quotes. |
| 5723 | */ |
| 5724 | static void |
| 5725 | expbackq(union node *cmd, int quoted, int quotes) |
| 5726 | { |
| 5727 | struct backcmd in; |
| 5728 | int i; |
| 5729 | char buf[128]; |
| 5730 | char *p; |
| 5731 | char *dest; |
| 5732 | int startloc; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5733 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5734 | struct stackmark smark; |
| 5735 | |
| 5736 | INT_OFF; |
| 5737 | setstackmark(&smark); |
| 5738 | dest = expdest; |
| 5739 | startloc = dest - (char *)stackblock(); |
| 5740 | grabstackstr(dest); |
| 5741 | evalbackcmd(cmd, &in); |
| 5742 | popstackmark(&smark); |
| 5743 | |
| 5744 | p = in.buf; |
| 5745 | i = in.nleft; |
| 5746 | if (i == 0) |
| 5747 | goto read; |
| 5748 | for (;;) { |
| 5749 | memtodest(p, i, syntax, quotes); |
| 5750 | read: |
| 5751 | if (in.fd < 0) |
| 5752 | break; |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 5753 | i = nonblock_safe_read(in.fd, buf, sizeof(buf)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5754 | TRACE(("expbackq: read returns %d\n", i)); |
| 5755 | if (i <= 0) |
| 5756 | break; |
| 5757 | p = buf; |
| 5758 | } |
| 5759 | |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 5760 | free(in.buf); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5761 | if (in.fd >= 0) { |
| 5762 | close(in.fd); |
| 5763 | back_exitstatus = waitforjob(in.jp); |
| 5764 | } |
| 5765 | INT_ON; |
| 5766 | |
| 5767 | /* Eat all trailing newlines */ |
| 5768 | dest = expdest; |
| 5769 | for (; dest > (char *)stackblock() && dest[-1] == '\n';) |
| 5770 | STUNPUTC(dest); |
| 5771 | expdest = dest; |
| 5772 | |
| 5773 | if (quoted == 0) |
| 5774 | recordregion(startloc, dest - (char *)stackblock(), 0); |
| 5775 | TRACE(("evalbackq: size=%d: \"%.*s\"\n", |
| 5776 | (dest - (char *)stackblock()) - startloc, |
| 5777 | (dest - (char *)stackblock()) - startloc, |
| 5778 | stackblock() + startloc)); |
| 5779 | } |
| 5780 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5781 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5782 | /* |
| 5783 | * Expand arithmetic expression. Backup to start of expression, |
| 5784 | * evaluate, place result in (backed up) result, adjust string position. |
| 5785 | */ |
| 5786 | static void |
| 5787 | expari(int quotes) |
| 5788 | { |
| 5789 | char *p, *start; |
| 5790 | int begoff; |
| 5791 | int flag; |
| 5792 | int len; |
| 5793 | |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5794 | /* ifsfree(); */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5795 | |
| 5796 | /* |
| 5797 | * This routine is slightly over-complicated for |
| 5798 | * efficiency. Next we scan backwards looking for the |
| 5799 | * start of arithmetic. |
| 5800 | */ |
| 5801 | start = stackblock(); |
| 5802 | p = expdest - 1; |
| 5803 | *p = '\0'; |
| 5804 | p--; |
| 5805 | do { |
| 5806 | int esc; |
| 5807 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5808 | while ((unsigned char)*p != CTLARI) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5809 | p--; |
| 5810 | #if DEBUG |
| 5811 | if (p < start) { |
| 5812 | ash_msg_and_raise_error("missing CTLARI (shouldn't happen)"); |
| 5813 | } |
| 5814 | #endif |
| 5815 | } |
| 5816 | |
| 5817 | esc = esclen(start, p); |
| 5818 | if (!(esc % 2)) { |
| 5819 | break; |
| 5820 | } |
| 5821 | |
| 5822 | p -= esc + 1; |
| 5823 | } while (1); |
| 5824 | |
| 5825 | begoff = p - start; |
| 5826 | |
| 5827 | removerecordregions(begoff); |
| 5828 | |
| 5829 | flag = p[1]; |
| 5830 | |
| 5831 | expdest = p; |
| 5832 | |
| 5833 | if (quotes) |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5834 | rmescapes(p + 2, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5835 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5836 | len = cvtnum(ash_arith(p + 2)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5837 | |
| 5838 | if (flag != '"') |
| 5839 | recordregion(begoff, begoff + len, 0); |
| 5840 | } |
| 5841 | #endif |
| 5842 | |
| 5843 | /* argstr needs it */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5844 | static char *evalvar(char *p, int flags, struct strlist *var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5845 | |
| 5846 | /* |
| 5847 | * Perform variable and command substitution. If EXP_FULL is set, output CTLESC |
| 5848 | * characters to allow for further processing. Otherwise treat |
| 5849 | * $@ like $* since no splitting will be performed. |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5850 | * |
| 5851 | * var_str_list (can be NULL) is a list of "VAR=val" strings which take precedence |
| 5852 | * over shell varables. Needed for "A=a B=$A; echo $B" case - we use it |
| 5853 | * for correct expansion of "B=$A" word. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5854 | */ |
| 5855 | static void |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5856 | argstr(char *p, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5857 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5858 | static const char spclchars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5859 | '=', |
| 5860 | ':', |
| 5861 | CTLQUOTEMARK, |
| 5862 | CTLENDVAR, |
| 5863 | CTLESC, |
| 5864 | CTLVAR, |
| 5865 | CTLBACKQ, |
| 5866 | CTLBACKQ | CTLQUOTE, |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5867 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5868 | CTLENDARI, |
| 5869 | #endif |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5870 | '\0' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5871 | }; |
| 5872 | const char *reject = spclchars; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5873 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */ |
| 5874 | int breakall = flags & EXP_WORD; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5875 | int inquotes; |
| 5876 | size_t length; |
| 5877 | int startloc; |
| 5878 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5879 | if (!(flags & EXP_VARTILDE)) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5880 | reject += 2; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5881 | } else if (flags & EXP_VARTILDE2) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5882 | reject++; |
| 5883 | } |
| 5884 | inquotes = 0; |
| 5885 | length = 0; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5886 | if (flags & EXP_TILDE) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5887 | char *q; |
| 5888 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5889 | flags &= ~EXP_TILDE; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5890 | tilde: |
| 5891 | q = p; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5892 | if (*q == CTLESC && (flags & EXP_QWORD)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5893 | q++; |
| 5894 | if (*q == '~') |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5895 | p = exptilde(p, q, flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5896 | } |
| 5897 | start: |
| 5898 | startloc = expdest - (char *)stackblock(); |
| 5899 | for (;;) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5900 | unsigned char c; |
| 5901 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5902 | length += strcspn(p + length, reject); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5903 | c = p[length]; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5904 | if (c) { |
| 5905 | if (!(c & 0x80) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5906 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5907 | || c == CTLENDARI |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5908 | #endif |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5909 | ) { |
| 5910 | /* c == '=' || c == ':' || c == CTLENDARI */ |
| 5911 | length++; |
| 5912 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5913 | } |
| 5914 | if (length > 0) { |
| 5915 | int newloc; |
| 5916 | expdest = stack_nputstr(p, length, expdest); |
| 5917 | newloc = expdest - (char *)stackblock(); |
| 5918 | if (breakall && !inquotes && newloc > startloc) { |
| 5919 | recordregion(startloc, newloc, 0); |
| 5920 | } |
| 5921 | startloc = newloc; |
| 5922 | } |
| 5923 | p += length + 1; |
| 5924 | length = 0; |
| 5925 | |
| 5926 | switch (c) { |
| 5927 | case '\0': |
| 5928 | goto breakloop; |
| 5929 | case '=': |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5930 | if (flags & EXP_VARTILDE2) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5931 | p--; |
| 5932 | continue; |
| 5933 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5934 | flags |= EXP_VARTILDE2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5935 | reject++; |
| 5936 | /* fall through */ |
| 5937 | case ':': |
| 5938 | /* |
| 5939 | * sort of a hack - expand tildes in variable |
| 5940 | * assignments (after the first '=' and after ':'s). |
| 5941 | */ |
| 5942 | if (*--p == '~') { |
| 5943 | goto tilde; |
| 5944 | } |
| 5945 | continue; |
| 5946 | } |
| 5947 | |
| 5948 | switch (c) { |
| 5949 | case CTLENDVAR: /* ??? */ |
| 5950 | goto breakloop; |
| 5951 | case CTLQUOTEMARK: |
| 5952 | /* "$@" syntax adherence hack */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5953 | if (!inquotes |
| 5954 | && memcmp(p, dolatstr, 4) == 0 |
| 5955 | && ( p[4] == CTLQUOTEMARK |
| 5956 | || (p[4] == CTLENDVAR && p[5] == CTLQUOTEMARK) |
| 5957 | ) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5958 | ) { |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5959 | p = evalvar(p + 1, flags, /* var_str_list: */ NULL) + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5960 | goto start; |
| 5961 | } |
| 5962 | inquotes = !inquotes; |
| 5963 | addquote: |
| 5964 | if (quotes) { |
| 5965 | p--; |
| 5966 | length++; |
| 5967 | startloc++; |
| 5968 | } |
| 5969 | break; |
| 5970 | case CTLESC: |
| 5971 | startloc++; |
| 5972 | length++; |
| 5973 | goto addquote; |
| 5974 | case CTLVAR: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5975 | p = evalvar(p, flags, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5976 | goto start; |
| 5977 | case CTLBACKQ: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5978 | c = '\0'; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5979 | case CTLBACKQ|CTLQUOTE: |
| 5980 | expbackq(argbackq->n, c, quotes); |
| 5981 | argbackq = argbackq->next; |
| 5982 | goto start; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5983 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5984 | case CTLENDARI: |
| 5985 | p--; |
| 5986 | expari(quotes); |
| 5987 | goto start; |
| 5988 | #endif |
| 5989 | } |
| 5990 | } |
| 5991 | breakloop: |
| 5992 | ; |
| 5993 | } |
| 5994 | |
| 5995 | static char * |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 5996 | scanleft(char *startp, char *rmesc, char *rmescend UNUSED_PARAM, char *str, int quotes, |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5997 | int zero) |
| 5998 | { |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 5999 | // This commented out code was added by James Simmons <jsimmons@infradead.org> |
| 6000 | // as part of a larger change when he added support for ${var/a/b}. |
| 6001 | // However, it broke # and % operators: |
| 6002 | // |
| 6003 | //var=ababcdcd |
| 6004 | // ok bad |
| 6005 | //echo ${var#ab} abcdcd abcdcd |
| 6006 | //echo ${var##ab} abcdcd abcdcd |
| 6007 | //echo ${var#a*b} abcdcd ababcdcd (!) |
| 6008 | //echo ${var##a*b} cdcd cdcd |
| 6009 | //echo ${var#?} babcdcd ababcdcd (!) |
| 6010 | //echo ${var##?} babcdcd babcdcd |
| 6011 | //echo ${var#*} ababcdcd babcdcd (!) |
| 6012 | //echo ${var##*} |
| 6013 | //echo ${var%cd} ababcd ababcd |
| 6014 | //echo ${var%%cd} ababcd abab (!) |
| 6015 | //echo ${var%c*d} ababcd ababcd |
| 6016 | //echo ${var%%c*d} abab ababcdcd (!) |
| 6017 | //echo ${var%?} ababcdc ababcdc |
| 6018 | //echo ${var%%?} ababcdc ababcdcd (!) |
| 6019 | //echo ${var%*} ababcdcd ababcdcd |
| 6020 | //echo ${var%%*} |
| 6021 | // |
| 6022 | // Commenting it back out helped. Remove it completely if it really |
| 6023 | // is not needed. |
| 6024 | |
| 6025 | char *loc, *loc2; //, *full; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6026 | char c; |
| 6027 | |
| 6028 | loc = startp; |
| 6029 | loc2 = rmesc; |
| 6030 | do { |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 6031 | int match; // = strlen(str); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6032 | const char *s = loc2; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6033 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6034 | c = *loc2; |
| 6035 | if (zero) { |
| 6036 | *loc2 = '\0'; |
| 6037 | s = rmesc; |
| 6038 | } |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 6039 | match = pmatch(str, s); // this line was deleted |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6040 | |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 6041 | // // chop off end if its '*' |
| 6042 | // full = strrchr(str, '*'); |
| 6043 | // if (full && full != str) |
| 6044 | // match--; |
| 6045 | // |
| 6046 | // // If str starts with '*' replace with s. |
| 6047 | // if ((*str == '*') && strlen(s) >= match) { |
| 6048 | // full = xstrdup(s); |
| 6049 | // strncpy(full+strlen(s)-match+1, str+1, match-1); |
| 6050 | // } else |
| 6051 | // full = xstrndup(str, match); |
| 6052 | // match = strncmp(s, full, strlen(full)); |
| 6053 | // free(full); |
| 6054 | // |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6055 | *loc2 = c; |
Denis Vlasenko | c7131c3 | 2008-04-14 01:59:53 +0000 | [diff] [blame] | 6056 | if (match) // if (!match) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6057 | return loc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6058 | if (quotes && (unsigned char)*loc == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6059 | loc++; |
| 6060 | loc++; |
| 6061 | loc2++; |
| 6062 | } while (c); |
| 6063 | return 0; |
| 6064 | } |
| 6065 | |
| 6066 | static char * |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6067 | scanright(char *startp, char *rmesc, char *rmescend, char *pattern, int quotes, int match_at_start) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6068 | { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6069 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6070 | int try2optimize = match_at_start; |
| 6071 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6072 | int esc = 0; |
| 6073 | char *loc; |
| 6074 | char *loc2; |
| 6075 | |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6076 | /* If we called by "${v/pattern/repl}" or "${v//pattern/repl}": |
| 6077 | * startp="escaped_value_of_v" rmesc="raw_value_of_v" |
| 6078 | * rmescend=""(ptr to NUL in rmesc) pattern="pattern" quotes=match_at_start=1 |
| 6079 | * Logic: |
| 6080 | * loc starts at NUL at the end of startp, loc2 starts at the end of rmesc, |
| 6081 | * and on each iteration they go back two/one char until they reach the beginning. |
| 6082 | * We try to find a match in "raw_value_of_v", "raw_value_of_", "raw_value_of" etc. |
| 6083 | */ |
| 6084 | /* TODO: document in what other circumstances we are called. */ |
| 6085 | |
| 6086 | for (loc = pattern - 1, loc2 = rmescend; loc >= startp; loc2--) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6087 | int match; |
| 6088 | char c = *loc2; |
| 6089 | const char *s = loc2; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6090 | if (match_at_start) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6091 | *loc2 = '\0'; |
| 6092 | s = rmesc; |
| 6093 | } |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6094 | match = pmatch(pattern, s); |
| 6095 | //bb_error_msg("pmatch(pattern:'%s',s:'%s'):%d", pattern, s, match); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6096 | *loc2 = c; |
| 6097 | if (match) |
| 6098 | return loc; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6099 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6100 | if (try2optimize) { |
| 6101 | /* Maybe we can optimize this: |
| 6102 | * if pattern ends with unescaped *, we can avoid checking |
| 6103 | * shorter strings: if "foo*" doesnt match "raw_value_of_v", |
| 6104 | * it wont match truncated "raw_value_of_" strings too. |
| 6105 | */ |
| 6106 | unsigned plen = strlen(pattern); |
| 6107 | /* Does it end with "*"? */ |
| 6108 | if (plen != 0 && pattern[--plen] == '*') { |
| 6109 | /* "xxxx*" is not escaped */ |
| 6110 | /* "xxx\*" is escaped */ |
| 6111 | /* "xx\\*" is not escaped */ |
| 6112 | /* "x\\\*" is escaped */ |
| 6113 | int slashes = 0; |
| 6114 | while (plen != 0 && pattern[--plen] == '\\') |
| 6115 | slashes++; |
| 6116 | if (!(slashes & 1)) |
| 6117 | break; /* ends with unescaped "*" */ |
| 6118 | } |
| 6119 | try2optimize = 0; |
| 6120 | } |
| 6121 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6122 | loc--; |
| 6123 | if (quotes) { |
| 6124 | if (--esc < 0) { |
| 6125 | esc = esclen(startp, loc); |
| 6126 | } |
| 6127 | if (esc % 2) { |
| 6128 | esc--; |
| 6129 | loc--; |
| 6130 | } |
| 6131 | } |
| 6132 | } |
| 6133 | return 0; |
| 6134 | } |
| 6135 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6136 | static void varunset(const char *, const char *, const char *, int) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6137 | static void |
| 6138 | varunset(const char *end, const char *var, const char *umsg, int varflags) |
| 6139 | { |
| 6140 | const char *msg; |
| 6141 | const char *tail; |
| 6142 | |
| 6143 | tail = nullstr; |
| 6144 | msg = "parameter not set"; |
| 6145 | if (umsg) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6146 | if ((unsigned char)*end == CTLENDVAR) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6147 | if (varflags & VSNUL) |
| 6148 | tail = " or null"; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6149 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6150 | msg = umsg; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6151 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6152 | } |
| 6153 | ash_msg_and_raise_error("%.*s: %s%s", end - var - 1, var, msg, tail); |
| 6154 | } |
| 6155 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6156 | #if ENABLE_ASH_BASH_COMPAT |
| 6157 | static char * |
| 6158 | parse_sub_pattern(char *arg, int inquotes) |
| 6159 | { |
| 6160 | char *idx, *repl = NULL; |
| 6161 | unsigned char c; |
| 6162 | |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6163 | idx = arg; |
| 6164 | while (1) { |
| 6165 | c = *arg; |
| 6166 | if (!c) |
| 6167 | break; |
| 6168 | if (c == '/') { |
| 6169 | /* Only the first '/' seen is our separator */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6170 | if (!repl) { |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6171 | repl = idx + 1; |
| 6172 | c = '\0'; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6173 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6174 | } |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6175 | *idx++ = c; |
| 6176 | if (!inquotes && c == '\\' && arg[1] == '\\') |
| 6177 | arg++; /* skip both \\, not just first one */ |
| 6178 | arg++; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6179 | } |
Denis Vlasenko | 29038c0 | 2008-06-14 06:14:02 +0000 | [diff] [blame] | 6180 | *idx = c; /* NUL */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6181 | |
| 6182 | return repl; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6183 | } |
| 6184 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6185 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6186 | static const char * |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6187 | subevalvar(char *p, char *str, int strloc, int subtype, |
| 6188 | int startloc, int varflags, int quotes, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6189 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6190 | struct nodelist *saveargbackq = argbackq; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6191 | char *startp; |
| 6192 | char *loc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6193 | char *rmesc, *rmescend; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6194 | IF_ASH_BASH_COMPAT(char *repl = NULL;) |
| 6195 | IF_ASH_BASH_COMPAT(char null = '\0';) |
| 6196 | IF_ASH_BASH_COMPAT(int pos, len, orig_len;) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6197 | int saveherefd = herefd; |
| 6198 | int amount, workloc, resetloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6199 | int zero; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6200 | char *(*scan)(char*, char*, char*, char*, int, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6201 | |
| 6202 | herefd = -1; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6203 | argstr(p, (subtype != VSASSIGN && subtype != VSQUESTION) ? EXP_CASE : 0, |
| 6204 | var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6205 | STPUTC('\0', expdest); |
| 6206 | herefd = saveherefd; |
| 6207 | argbackq = saveargbackq; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6208 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6209 | |
| 6210 | switch (subtype) { |
| 6211 | case VSASSIGN: |
| 6212 | setvar(str, startp, 0); |
| 6213 | amount = startp - expdest; |
| 6214 | STADJUST(amount, expdest); |
| 6215 | return startp; |
| 6216 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6217 | #if ENABLE_ASH_BASH_COMPAT |
| 6218 | case VSSUBSTR: |
| 6219 | loc = str = stackblock() + strloc; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6220 | /* Read POS in ${var:POS:LEN} */ |
| 6221 | pos = atoi(loc); /* number(loc) errors out on "1:4" */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6222 | len = str - startp - 1; |
| 6223 | |
| 6224 | /* *loc != '\0', guaranteed by parser */ |
| 6225 | if (quotes) { |
| 6226 | char *ptr; |
| 6227 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6228 | /* Adjust the length by the number of escapes */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6229 | for (ptr = startp; ptr < (str - 1); ptr++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6230 | if ((unsigned char)*ptr == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6231 | len--; |
| 6232 | ptr++; |
| 6233 | } |
| 6234 | } |
| 6235 | } |
| 6236 | orig_len = len; |
| 6237 | |
| 6238 | if (*loc++ == ':') { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6239 | /* ${var::LEN} */ |
| 6240 | len = number(loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6241 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6242 | /* Skip POS in ${var:POS:LEN} */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6243 | len = orig_len; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6244 | while (*loc && *loc != ':') { |
| 6245 | /* TODO? |
| 6246 | * bash complains on: var=qwe; echo ${var:1a:123} |
| 6247 | if (!isdigit(*loc)) |
| 6248 | ash_msg_and_raise_error(msg_illnum, str); |
| 6249 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6250 | loc++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6251 | } |
| 6252 | if (*loc++ == ':') { |
| 6253 | len = number(loc); |
| 6254 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6255 | } |
| 6256 | if (pos >= orig_len) { |
| 6257 | pos = 0; |
| 6258 | len = 0; |
| 6259 | } |
| 6260 | if (len > (orig_len - pos)) |
| 6261 | len = orig_len - pos; |
| 6262 | |
| 6263 | for (str = startp; pos; str++, pos--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6264 | if (quotes && (unsigned char)*str == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6265 | str++; |
| 6266 | } |
| 6267 | for (loc = startp; len; len--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6268 | if (quotes && (unsigned char)*str == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6269 | *loc++ = *str++; |
| 6270 | *loc++ = *str++; |
| 6271 | } |
| 6272 | *loc = '\0'; |
| 6273 | amount = loc - expdest; |
| 6274 | STADJUST(amount, expdest); |
| 6275 | return loc; |
| 6276 | #endif |
| 6277 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6278 | case VSQUESTION: |
| 6279 | varunset(p, str, startp, varflags); |
| 6280 | /* NOTREACHED */ |
| 6281 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6282 | resetloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6283 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6284 | /* We'll comeback here if we grow the stack while handling |
| 6285 | * a VSREPLACE or VSREPLACEALL, since our pointers into the |
| 6286 | * stack will need rebasing, and we'll need to remove our work |
| 6287 | * areas each time |
| 6288 | */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6289 | IF_ASH_BASH_COMPAT(restart:) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6290 | |
| 6291 | amount = expdest - ((char *)stackblock() + resetloc); |
| 6292 | STADJUST(-amount, expdest); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6293 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6294 | |
| 6295 | rmesc = startp; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6296 | rmescend = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6297 | if (quotes) { |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 6298 | rmesc = rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6299 | if (rmesc != startp) { |
| 6300 | rmescend = expdest; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6301 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6302 | } |
| 6303 | } |
| 6304 | rmescend--; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6305 | str = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6306 | preglob(str, varflags & VSQUOTE, 0); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6307 | workloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6308 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6309 | #if ENABLE_ASH_BASH_COMPAT |
| 6310 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6311 | char *idx, *end; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6312 | |
Denis Vlasenko | d6855d1 | 2008-09-27 14:03:25 +0000 | [diff] [blame] | 6313 | if (!repl) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6314 | repl = parse_sub_pattern(str, varflags & VSQUOTE); |
| 6315 | if (!repl) |
| 6316 | repl = &null; |
| 6317 | } |
| 6318 | |
| 6319 | /* If there's no pattern to match, return the expansion unmolested */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6320 | if (str[0] == '\0') |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6321 | return 0; |
| 6322 | |
| 6323 | len = 0; |
| 6324 | idx = startp; |
| 6325 | end = str - 1; |
| 6326 | while (idx < end) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6327 | try_to_match: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6328 | loc = scanright(idx, rmesc, rmescend, str, quotes, 1); |
| 6329 | if (!loc) { |
| 6330 | /* No match, advance */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6331 | char *restart_detect = stackblock(); |
| 6332 | skip_matching: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6333 | STPUTC(*idx, expdest); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6334 | if (quotes && (unsigned char)*idx == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6335 | idx++; |
| 6336 | len++; |
| 6337 | STPUTC(*idx, expdest); |
| 6338 | } |
| 6339 | if (stackblock() != restart_detect) |
| 6340 | goto restart; |
| 6341 | idx++; |
| 6342 | len++; |
| 6343 | rmesc++; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6344 | /* continue; - prone to quadratic behavior, smarter code: */ |
| 6345 | if (idx >= end) |
| 6346 | break; |
| 6347 | if (str[0] == '*') { |
| 6348 | /* Pattern is "*foo". If "*foo" does not match "long_string", |
| 6349 | * it would never match "ong_string" etc, no point in trying. |
| 6350 | */ |
| 6351 | goto skip_matching; |
| 6352 | } |
| 6353 | goto try_to_match; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6354 | } |
| 6355 | |
| 6356 | if (subtype == VSREPLACEALL) { |
| 6357 | while (idx < loc) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6358 | if (quotes && (unsigned char)*idx == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6359 | idx++; |
| 6360 | idx++; |
| 6361 | rmesc++; |
| 6362 | } |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6363 | } else { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6364 | idx = loc; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6365 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6366 | |
| 6367 | for (loc = repl; *loc; loc++) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6368 | char *restart_detect = stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6369 | STPUTC(*loc, expdest); |
| 6370 | if (stackblock() != restart_detect) |
| 6371 | goto restart; |
| 6372 | len++; |
| 6373 | } |
| 6374 | |
| 6375 | if (subtype == VSREPLACE) { |
| 6376 | while (*idx) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6377 | char *restart_detect = stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6378 | STPUTC(*idx, expdest); |
| 6379 | if (stackblock() != restart_detect) |
| 6380 | goto restart; |
| 6381 | len++; |
| 6382 | idx++; |
| 6383 | } |
| 6384 | break; |
| 6385 | } |
| 6386 | } |
| 6387 | |
| 6388 | /* We've put the replaced text into a buffer at workloc, now |
| 6389 | * move it to the right place and adjust the stack. |
| 6390 | */ |
| 6391 | startp = stackblock() + startloc; |
| 6392 | STPUTC('\0', expdest); |
| 6393 | memmove(startp, stackblock() + workloc, len); |
| 6394 | startp[len++] = '\0'; |
| 6395 | amount = expdest - ((char *)stackblock() + startloc + len - 1); |
| 6396 | STADJUST(-amount, expdest); |
| 6397 | return startp; |
| 6398 | } |
| 6399 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6400 | |
| 6401 | subtype -= VSTRIMRIGHT; |
| 6402 | #if DEBUG |
| 6403 | if (subtype < 0 || subtype > 7) |
| 6404 | abort(); |
| 6405 | #endif |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6406 | /* zero = (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX) */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6407 | zero = subtype >> 1; |
| 6408 | /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */ |
| 6409 | scan = (subtype & 1) ^ zero ? scanleft : scanright; |
| 6410 | |
| 6411 | loc = scan(startp, rmesc, rmescend, str, quotes, zero); |
| 6412 | if (loc) { |
| 6413 | if (zero) { |
| 6414 | memmove(startp, loc, str - loc); |
| 6415 | loc = startp + (str - loc) - 1; |
| 6416 | } |
| 6417 | *loc = '\0'; |
| 6418 | amount = loc - expdest; |
| 6419 | STADJUST(amount, expdest); |
| 6420 | } |
| 6421 | return loc; |
| 6422 | } |
| 6423 | |
| 6424 | /* |
| 6425 | * Add the value of a specialized variable to the stack string. |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6426 | * name parameter (examples): |
| 6427 | * ash -c 'echo $1' name:'1=' |
| 6428 | * ash -c 'echo $qwe' name:'qwe=' |
| 6429 | * ash -c 'echo $$' name:'$=' |
| 6430 | * ash -c 'echo ${$}' name:'$=' |
| 6431 | * ash -c 'echo ${$##q}' name:'$=q' |
| 6432 | * ash -c 'echo ${#$}' name:'$=' |
| 6433 | * note: examples with bad shell syntax: |
| 6434 | * ash -c 'echo ${#$1}' name:'$=1' |
| 6435 | * ash -c 'echo ${#1#}' name:'1=#' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6436 | */ |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 6437 | static NOINLINE ssize_t |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6438 | varvalue(char *name, int varflags, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6439 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6440 | const char *p; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6441 | int num; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6442 | int i; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6443 | int sepq = 0; |
| 6444 | ssize_t len = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6445 | int subtype = varflags & VSTYPE; |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 6446 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6447 | int quoted = varflags & VSQUOTE; |
| 6448 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6449 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6450 | switch (*name) { |
| 6451 | case '$': |
| 6452 | num = rootpid; |
| 6453 | goto numvar; |
| 6454 | case '?': |
| 6455 | num = exitstatus; |
| 6456 | goto numvar; |
| 6457 | case '#': |
| 6458 | num = shellparam.nparam; |
| 6459 | goto numvar; |
| 6460 | case '!': |
| 6461 | num = backgndpid; |
| 6462 | if (num == 0) |
| 6463 | return -1; |
| 6464 | numvar: |
| 6465 | len = cvtnum(num); |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6466 | goto check_1char_name; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6467 | case '-': |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6468 | expdest = makestrspace(NOPTS, expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6469 | for (i = NOPTS - 1; i >= 0; i--) { |
| 6470 | if (optlist[i]) { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6471 | USTPUTC(optletters(i), expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6472 | len++; |
| 6473 | } |
| 6474 | } |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6475 | check_1char_name: |
| 6476 | #if 0 |
| 6477 | /* handles cases similar to ${#$1} */ |
| 6478 | if (name[2] != '\0') |
| 6479 | raise_error_syntax("bad substitution"); |
| 6480 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6481 | break; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6482 | case '@': { |
| 6483 | char **ap; |
| 6484 | int sep; |
| 6485 | |
| 6486 | if (quoted && (flags & EXP_FULL)) { |
| 6487 | /* note: this is not meant as PEOF value */ |
| 6488 | sep = 1 << CHAR_BIT; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6489 | goto param; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6490 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6491 | /* fall through */ |
| 6492 | case '*': |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6493 | sep = ifsset() ? (unsigned char)(ifsval()[0]) : ' '; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6494 | i = SIT(sep, syntax); |
| 6495 | if (quotes && (i == CCTL || i == CBACK)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6496 | sepq = 1; |
| 6497 | param: |
| 6498 | ap = shellparam.p; |
| 6499 | if (!ap) |
| 6500 | return -1; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6501 | while ((p = *ap++) != NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6502 | size_t partlen; |
| 6503 | |
| 6504 | partlen = strlen(p); |
| 6505 | len += partlen; |
| 6506 | |
| 6507 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6508 | memtodest(p, partlen, syntax, quotes); |
| 6509 | |
| 6510 | if (*ap && sep) { |
| 6511 | char *q; |
| 6512 | |
| 6513 | len++; |
| 6514 | if (subtype == VSPLUS || subtype == VSLENGTH) { |
| 6515 | continue; |
| 6516 | } |
| 6517 | q = expdest; |
| 6518 | if (sepq) |
| 6519 | STPUTC(CTLESC, q); |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6520 | /* note: may put NUL despite sep != 0 |
| 6521 | * (see sep = 1 << CHAR_BIT above) */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6522 | STPUTC(sep, q); |
| 6523 | expdest = q; |
| 6524 | } |
| 6525 | } |
| 6526 | return len; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6527 | } /* case '@' and '*' */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6528 | case '0': |
| 6529 | case '1': |
| 6530 | case '2': |
| 6531 | case '3': |
| 6532 | case '4': |
| 6533 | case '5': |
| 6534 | case '6': |
| 6535 | case '7': |
| 6536 | case '8': |
| 6537 | case '9': |
Denys Vlasenko | a00329c | 2009-08-30 20:05:10 +0200 | [diff] [blame] | 6538 | num = atoi(name); /* number(name) fails on ${N#str} etc */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6539 | if (num < 0 || num > shellparam.nparam) |
| 6540 | return -1; |
| 6541 | p = num ? shellparam.p[num - 1] : arg0; |
| 6542 | goto value; |
| 6543 | default: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6544 | /* NB: name has form "VAR=..." */ |
| 6545 | |
| 6546 | /* "A=a B=$A" case: var_str_list is a list of "A=a" strings |
| 6547 | * which should be considered before we check variables. */ |
| 6548 | if (var_str_list) { |
| 6549 | unsigned name_len = (strchrnul(name, '=') - name) + 1; |
| 6550 | p = NULL; |
| 6551 | do { |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 6552 | char *str, *eq; |
| 6553 | str = var_str_list->text; |
| 6554 | eq = strchr(str, '='); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6555 | if (!eq) /* stop at first non-assignment */ |
| 6556 | break; |
| 6557 | eq++; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 6558 | if (name_len == (unsigned)(eq - str) |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6559 | && strncmp(str, name, name_len) == 0 |
| 6560 | ) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6561 | p = eq; |
| 6562 | /* goto value; - WRONG! */ |
| 6563 | /* think "A=1 A=2 B=$A" */ |
| 6564 | } |
| 6565 | var_str_list = var_str_list->next; |
| 6566 | } while (var_str_list); |
| 6567 | if (p) |
| 6568 | goto value; |
| 6569 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6570 | p = lookupvar(name); |
| 6571 | value: |
| 6572 | if (!p) |
| 6573 | return -1; |
| 6574 | |
| 6575 | len = strlen(p); |
| 6576 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6577 | memtodest(p, len, syntax, quotes); |
| 6578 | return len; |
| 6579 | } |
| 6580 | |
| 6581 | if (subtype == VSPLUS || subtype == VSLENGTH) |
| 6582 | STADJUST(-len, expdest); |
| 6583 | return len; |
| 6584 | } |
| 6585 | |
| 6586 | /* |
| 6587 | * Expand a variable, and return a pointer to the next character in the |
| 6588 | * input string. |
| 6589 | */ |
| 6590 | static char * |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6591 | evalvar(char *p, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6592 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6593 | char varflags; |
| 6594 | char subtype; |
| 6595 | char quoted; |
| 6596 | char easy; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6597 | char *var; |
| 6598 | int patloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6599 | int startloc; |
| 6600 | ssize_t varlen; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6601 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6602 | varflags = (unsigned char) *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6603 | subtype = varflags & VSTYPE; |
| 6604 | quoted = varflags & VSQUOTE; |
| 6605 | var = p; |
| 6606 | easy = (!quoted || (*var == '@' && shellparam.nparam)); |
| 6607 | startloc = expdest - (char *)stackblock(); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 6608 | p = strchr(p, '=') + 1; //TODO: use var_end(p)? |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6609 | |
| 6610 | again: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6611 | varlen = varvalue(var, varflags, flags, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6612 | if (varflags & VSNUL) |
| 6613 | varlen--; |
| 6614 | |
| 6615 | if (subtype == VSPLUS) { |
| 6616 | varlen = -1 - varlen; |
| 6617 | goto vsplus; |
| 6618 | } |
| 6619 | |
| 6620 | if (subtype == VSMINUS) { |
| 6621 | vsplus: |
| 6622 | if (varlen < 0) { |
| 6623 | argstr( |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6624 | p, flags | EXP_TILDE | |
| 6625 | (quoted ? EXP_QWORD : EXP_WORD), |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6626 | var_str_list |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6627 | ); |
| 6628 | goto end; |
| 6629 | } |
| 6630 | if (easy) |
| 6631 | goto record; |
| 6632 | goto end; |
| 6633 | } |
| 6634 | |
| 6635 | if (subtype == VSASSIGN || subtype == VSQUESTION) { |
| 6636 | if (varlen < 0) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6637 | if (subevalvar(p, var, /* strloc: */ 0, |
| 6638 | subtype, startloc, varflags, |
| 6639 | /* quotes: */ 0, |
| 6640 | var_str_list) |
| 6641 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6642 | varflags &= ~VSNUL; |
| 6643 | /* |
| 6644 | * Remove any recorded regions beyond |
| 6645 | * start of variable |
| 6646 | */ |
| 6647 | removerecordregions(startloc); |
| 6648 | goto again; |
| 6649 | } |
| 6650 | goto end; |
| 6651 | } |
| 6652 | if (easy) |
| 6653 | goto record; |
| 6654 | goto end; |
| 6655 | } |
| 6656 | |
| 6657 | if (varlen < 0 && uflag) |
| 6658 | varunset(p, var, 0, 0); |
| 6659 | |
| 6660 | if (subtype == VSLENGTH) { |
| 6661 | cvtnum(varlen > 0 ? varlen : 0); |
| 6662 | goto record; |
| 6663 | } |
| 6664 | |
| 6665 | if (subtype == VSNORMAL) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6666 | if (easy) |
| 6667 | goto record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6668 | goto end; |
| 6669 | } |
| 6670 | |
| 6671 | #if DEBUG |
| 6672 | switch (subtype) { |
| 6673 | case VSTRIMLEFT: |
| 6674 | case VSTRIMLEFTMAX: |
| 6675 | case VSTRIMRIGHT: |
| 6676 | case VSTRIMRIGHTMAX: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6677 | #if ENABLE_ASH_BASH_COMPAT |
| 6678 | case VSSUBSTR: |
| 6679 | case VSREPLACE: |
| 6680 | case VSREPLACEALL: |
| 6681 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6682 | break; |
| 6683 | default: |
| 6684 | abort(); |
| 6685 | } |
| 6686 | #endif |
| 6687 | |
| 6688 | if (varlen >= 0) { |
| 6689 | /* |
| 6690 | * Terminate the string and start recording the pattern |
| 6691 | * right after it |
| 6692 | */ |
| 6693 | STPUTC('\0', expdest); |
| 6694 | patloc = expdest - (char *)stackblock(); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6695 | if (0 == subevalvar(p, /* str: */ NULL, patloc, subtype, |
| 6696 | startloc, varflags, |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 6697 | //TODO: | EXP_REDIR too? All other such places do it too |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6698 | /* quotes: */ flags & (EXP_FULL | EXP_CASE), |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6699 | var_str_list) |
| 6700 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6701 | int amount = expdest - ( |
| 6702 | (char *)stackblock() + patloc - 1 |
| 6703 | ); |
| 6704 | STADJUST(-amount, expdest); |
| 6705 | } |
| 6706 | /* Remove any recorded regions beyond start of variable */ |
| 6707 | removerecordregions(startloc); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6708 | record: |
| 6709 | recordregion(startloc, expdest - (char *)stackblock(), quoted); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6710 | } |
| 6711 | |
| 6712 | end: |
| 6713 | if (subtype != VSNORMAL) { /* skip to end of alternative */ |
| 6714 | int nesting = 1; |
| 6715 | for (;;) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6716 | unsigned char c = *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6717 | if (c == CTLESC) |
| 6718 | p++; |
| 6719 | else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { |
| 6720 | if (varlen >= 0) |
| 6721 | argbackq = argbackq->next; |
| 6722 | } else if (c == CTLVAR) { |
| 6723 | if ((*p++ & VSTYPE) != VSNORMAL) |
| 6724 | nesting++; |
| 6725 | } else if (c == CTLENDVAR) { |
| 6726 | if (--nesting == 0) |
| 6727 | break; |
| 6728 | } |
| 6729 | } |
| 6730 | } |
| 6731 | return p; |
| 6732 | } |
| 6733 | |
| 6734 | /* |
| 6735 | * Break the argument string into pieces based upon IFS and add the |
| 6736 | * strings to the argument list. The regions of the string to be |
| 6737 | * searched for IFS characters have been stored by recordregion. |
| 6738 | */ |
| 6739 | static void |
| 6740 | ifsbreakup(char *string, struct arglist *arglist) |
| 6741 | { |
| 6742 | struct ifsregion *ifsp; |
| 6743 | struct strlist *sp; |
| 6744 | char *start; |
| 6745 | char *p; |
| 6746 | char *q; |
| 6747 | const char *ifs, *realifs; |
| 6748 | int ifsspc; |
| 6749 | int nulonly; |
| 6750 | |
| 6751 | start = string; |
| 6752 | if (ifslastp != NULL) { |
| 6753 | ifsspc = 0; |
| 6754 | nulonly = 0; |
| 6755 | realifs = ifsset() ? ifsval() : defifs; |
| 6756 | ifsp = &ifsfirst; |
| 6757 | do { |
| 6758 | p = string + ifsp->begoff; |
| 6759 | nulonly = ifsp->nulonly; |
| 6760 | ifs = nulonly ? nullstr : realifs; |
| 6761 | ifsspc = 0; |
| 6762 | while (p < string + ifsp->endoff) { |
| 6763 | q = p; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6764 | if ((unsigned char)*p == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6765 | p++; |
| 6766 | if (!strchr(ifs, *p)) { |
| 6767 | p++; |
| 6768 | continue; |
| 6769 | } |
| 6770 | if (!nulonly) |
| 6771 | ifsspc = (strchr(defifs, *p) != NULL); |
| 6772 | /* Ignore IFS whitespace at start */ |
| 6773 | if (q == start && ifsspc) { |
| 6774 | p++; |
| 6775 | start = p; |
| 6776 | continue; |
| 6777 | } |
| 6778 | *q = '\0'; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6779 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6780 | sp->text = start; |
| 6781 | *arglist->lastp = sp; |
| 6782 | arglist->lastp = &sp->next; |
| 6783 | p++; |
| 6784 | if (!nulonly) { |
| 6785 | for (;;) { |
| 6786 | if (p >= string + ifsp->endoff) { |
| 6787 | break; |
| 6788 | } |
| 6789 | q = p; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6790 | if ((unsigned char)*p == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6791 | p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 6792 | if (strchr(ifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6793 | p = q; |
| 6794 | break; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6795 | } |
| 6796 | if (strchr(defifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6797 | if (ifsspc) { |
| 6798 | p++; |
| 6799 | ifsspc = 0; |
| 6800 | } else { |
| 6801 | p = q; |
| 6802 | break; |
| 6803 | } |
| 6804 | } else |
| 6805 | p++; |
| 6806 | } |
| 6807 | } |
| 6808 | start = p; |
| 6809 | } /* while */ |
| 6810 | ifsp = ifsp->next; |
| 6811 | } while (ifsp != NULL); |
| 6812 | if (nulonly) |
| 6813 | goto add; |
| 6814 | } |
| 6815 | |
| 6816 | if (!*start) |
| 6817 | return; |
| 6818 | |
| 6819 | add: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6820 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6821 | sp->text = start; |
| 6822 | *arglist->lastp = sp; |
| 6823 | arglist->lastp = &sp->next; |
| 6824 | } |
| 6825 | |
| 6826 | static void |
| 6827 | ifsfree(void) |
| 6828 | { |
| 6829 | struct ifsregion *p; |
| 6830 | |
| 6831 | INT_OFF; |
| 6832 | p = ifsfirst.next; |
| 6833 | do { |
| 6834 | struct ifsregion *ifsp; |
| 6835 | ifsp = p->next; |
| 6836 | free(p); |
| 6837 | p = ifsp; |
| 6838 | } while (p); |
| 6839 | ifslastp = NULL; |
| 6840 | ifsfirst.next = NULL; |
| 6841 | INT_ON; |
| 6842 | } |
| 6843 | |
| 6844 | /* |
| 6845 | * Add a file name to the list. |
| 6846 | */ |
| 6847 | static void |
| 6848 | addfname(const char *name) |
| 6849 | { |
| 6850 | struct strlist *sp; |
| 6851 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6852 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6853 | sp->text = ststrdup(name); |
| 6854 | *exparg.lastp = sp; |
| 6855 | exparg.lastp = &sp->next; |
| 6856 | } |
| 6857 | |
| 6858 | static char *expdir; |
| 6859 | |
| 6860 | /* |
| 6861 | * Do metacharacter (i.e. *, ?, [...]) expansion. |
| 6862 | */ |
| 6863 | static void |
| 6864 | expmeta(char *enddir, char *name) |
| 6865 | { |
| 6866 | char *p; |
| 6867 | const char *cp; |
| 6868 | char *start; |
| 6869 | char *endname; |
| 6870 | int metaflag; |
| 6871 | struct stat statb; |
| 6872 | DIR *dirp; |
| 6873 | struct dirent *dp; |
| 6874 | int atend; |
| 6875 | int matchdot; |
| 6876 | |
| 6877 | metaflag = 0; |
| 6878 | start = name; |
| 6879 | for (p = name; *p; p++) { |
| 6880 | if (*p == '*' || *p == '?') |
| 6881 | metaflag = 1; |
| 6882 | else if (*p == '[') { |
| 6883 | char *q = p + 1; |
| 6884 | if (*q == '!') |
| 6885 | q++; |
| 6886 | for (;;) { |
| 6887 | if (*q == '\\') |
| 6888 | q++; |
| 6889 | if (*q == '/' || *q == '\0') |
| 6890 | break; |
| 6891 | if (*++q == ']') { |
| 6892 | metaflag = 1; |
| 6893 | break; |
| 6894 | } |
| 6895 | } |
| 6896 | } else if (*p == '\\') |
| 6897 | p++; |
| 6898 | else if (*p == '/') { |
| 6899 | if (metaflag) |
| 6900 | goto out; |
| 6901 | start = p + 1; |
| 6902 | } |
| 6903 | } |
| 6904 | out: |
| 6905 | if (metaflag == 0) { /* we've reached the end of the file name */ |
| 6906 | if (enddir != expdir) |
| 6907 | metaflag++; |
| 6908 | p = name; |
| 6909 | do { |
| 6910 | if (*p == '\\') |
| 6911 | p++; |
| 6912 | *enddir++ = *p; |
| 6913 | } while (*p++); |
| 6914 | if (metaflag == 0 || lstat(expdir, &statb) >= 0) |
| 6915 | addfname(expdir); |
| 6916 | return; |
| 6917 | } |
| 6918 | endname = p; |
| 6919 | if (name < start) { |
| 6920 | p = name; |
| 6921 | do { |
| 6922 | if (*p == '\\') |
| 6923 | p++; |
| 6924 | *enddir++ = *p++; |
| 6925 | } while (p < start); |
| 6926 | } |
| 6927 | if (enddir == expdir) { |
| 6928 | cp = "."; |
| 6929 | } else if (enddir == expdir + 1 && *expdir == '/') { |
| 6930 | cp = "/"; |
| 6931 | } else { |
| 6932 | cp = expdir; |
| 6933 | enddir[-1] = '\0'; |
| 6934 | } |
| 6935 | dirp = opendir(cp); |
| 6936 | if (dirp == NULL) |
| 6937 | return; |
| 6938 | if (enddir != expdir) |
| 6939 | enddir[-1] = '/'; |
| 6940 | if (*endname == 0) { |
| 6941 | atend = 1; |
| 6942 | } else { |
| 6943 | atend = 0; |
| 6944 | *endname++ = '\0'; |
| 6945 | } |
| 6946 | matchdot = 0; |
| 6947 | p = start; |
| 6948 | if (*p == '\\') |
| 6949 | p++; |
| 6950 | if (*p == '.') |
| 6951 | matchdot++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6952 | while (!pending_int && (dp = readdir(dirp)) != NULL) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 6953 | if (dp->d_name[0] == '.' && !matchdot) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6954 | continue; |
| 6955 | if (pmatch(start, dp->d_name)) { |
| 6956 | if (atend) { |
| 6957 | strcpy(enddir, dp->d_name); |
| 6958 | addfname(expdir); |
| 6959 | } else { |
| 6960 | for (p = enddir, cp = dp->d_name; (*p++ = *cp++) != '\0';) |
| 6961 | continue; |
| 6962 | p[-1] = '/'; |
| 6963 | expmeta(p, endname); |
| 6964 | } |
| 6965 | } |
| 6966 | } |
| 6967 | closedir(dirp); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 6968 | if (!atend) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6969 | endname[-1] = '/'; |
| 6970 | } |
| 6971 | |
| 6972 | static struct strlist * |
| 6973 | msort(struct strlist *list, int len) |
| 6974 | { |
| 6975 | struct strlist *p, *q = NULL; |
| 6976 | struct strlist **lpp; |
| 6977 | int half; |
| 6978 | int n; |
| 6979 | |
| 6980 | if (len <= 1) |
| 6981 | return list; |
| 6982 | half = len >> 1; |
| 6983 | p = list; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 6984 | for (n = half; --n >= 0;) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6985 | q = p; |
| 6986 | p = p->next; |
| 6987 | } |
| 6988 | q->next = NULL; /* terminate first half of list */ |
| 6989 | q = msort(list, half); /* sort first half of list */ |
| 6990 | p = msort(p, len - half); /* sort second half */ |
| 6991 | lpp = &list; |
| 6992 | for (;;) { |
| 6993 | #if ENABLE_LOCALE_SUPPORT |
| 6994 | if (strcoll(p->text, q->text) < 0) |
| 6995 | #else |
| 6996 | if (strcmp(p->text, q->text) < 0) |
| 6997 | #endif |
| 6998 | { |
| 6999 | *lpp = p; |
| 7000 | lpp = &p->next; |
| 7001 | p = *lpp; |
| 7002 | if (p == NULL) { |
| 7003 | *lpp = q; |
| 7004 | break; |
| 7005 | } |
| 7006 | } else { |
| 7007 | *lpp = q; |
| 7008 | lpp = &q->next; |
| 7009 | q = *lpp; |
| 7010 | if (q == NULL) { |
| 7011 | *lpp = p; |
| 7012 | break; |
| 7013 | } |
| 7014 | } |
| 7015 | } |
| 7016 | return list; |
| 7017 | } |
| 7018 | |
| 7019 | /* |
| 7020 | * Sort the results of file name expansion. It calculates the number of |
| 7021 | * strings to sort and then calls msort (short for merge sort) to do the |
| 7022 | * work. |
| 7023 | */ |
| 7024 | static struct strlist * |
| 7025 | expsort(struct strlist *str) |
| 7026 | { |
| 7027 | int len; |
| 7028 | struct strlist *sp; |
| 7029 | |
| 7030 | len = 0; |
| 7031 | for (sp = str; sp; sp = sp->next) |
| 7032 | len++; |
| 7033 | return msort(str, len); |
| 7034 | } |
| 7035 | |
| 7036 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7037 | expandmeta(struct strlist *str /*, int flag*/) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7038 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 7039 | static const char metachars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7040 | '*', '?', '[', 0 |
| 7041 | }; |
| 7042 | /* TODO - EXP_REDIR */ |
| 7043 | |
| 7044 | while (str) { |
| 7045 | struct strlist **savelastp; |
| 7046 | struct strlist *sp; |
| 7047 | char *p; |
| 7048 | |
| 7049 | if (fflag) |
| 7050 | goto nometa; |
| 7051 | if (!strpbrk(str->text, metachars)) |
| 7052 | goto nometa; |
| 7053 | savelastp = exparg.lastp; |
| 7054 | |
| 7055 | INT_OFF; |
| 7056 | p = preglob(str->text, 0, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
| 7057 | { |
| 7058 | int i = strlen(str->text); |
| 7059 | expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ |
| 7060 | } |
| 7061 | |
| 7062 | expmeta(expdir, p); |
| 7063 | free(expdir); |
| 7064 | if (p != str->text) |
| 7065 | free(p); |
| 7066 | INT_ON; |
| 7067 | if (exparg.lastp == savelastp) { |
| 7068 | /* |
| 7069 | * no matches |
| 7070 | */ |
| 7071 | nometa: |
| 7072 | *exparg.lastp = str; |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 7073 | rmescapes(str->text, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7074 | exparg.lastp = &str->next; |
| 7075 | } else { |
| 7076 | *exparg.lastp = NULL; |
| 7077 | *savelastp = sp = expsort(*savelastp); |
| 7078 | while (sp->next != NULL) |
| 7079 | sp = sp->next; |
| 7080 | exparg.lastp = &sp->next; |
| 7081 | } |
| 7082 | str = str->next; |
| 7083 | } |
| 7084 | } |
| 7085 | |
| 7086 | /* |
| 7087 | * Perform variable substitution and command substitution on an argument, |
| 7088 | * placing the resulting list of arguments in arglist. If EXP_FULL is true, |
| 7089 | * perform splitting and file name expansion. When arglist is NULL, perform |
| 7090 | * here document expansion. |
| 7091 | */ |
| 7092 | static void |
| 7093 | expandarg(union node *arg, struct arglist *arglist, int flag) |
| 7094 | { |
| 7095 | struct strlist *sp; |
| 7096 | char *p; |
| 7097 | |
| 7098 | argbackq = arg->narg.backquote; |
| 7099 | STARTSTACKSTR(expdest); |
| 7100 | ifsfirst.next = NULL; |
| 7101 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7102 | argstr(arg->narg.text, flag, |
| 7103 | /* var_str_list: */ arglist ? arglist->list : NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7104 | p = _STPUTC('\0', expdest); |
| 7105 | expdest = p - 1; |
| 7106 | if (arglist == NULL) { |
| 7107 | return; /* here document expanded */ |
| 7108 | } |
| 7109 | p = grabstackstr(p); |
| 7110 | exparg.lastp = &exparg.list; |
| 7111 | /* |
| 7112 | * TODO - EXP_REDIR |
| 7113 | */ |
| 7114 | if (flag & EXP_FULL) { |
| 7115 | ifsbreakup(p, &exparg); |
| 7116 | *exparg.lastp = NULL; |
| 7117 | exparg.lastp = &exparg.list; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7118 | expandmeta(exparg.list /*, flag*/); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7119 | } else { |
| 7120 | if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 7121 | rmescapes(p, 0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7122 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7123 | sp->text = p; |
| 7124 | *exparg.lastp = sp; |
| 7125 | exparg.lastp = &sp->next; |
| 7126 | } |
| 7127 | if (ifsfirst.next) |
| 7128 | ifsfree(); |
| 7129 | *exparg.lastp = NULL; |
| 7130 | if (exparg.list) { |
| 7131 | *arglist->lastp = exparg.list; |
| 7132 | arglist->lastp = exparg.lastp; |
| 7133 | } |
| 7134 | } |
| 7135 | |
| 7136 | /* |
| 7137 | * Expand shell variables and backquotes inside a here document. |
| 7138 | */ |
| 7139 | static void |
| 7140 | expandhere(union node *arg, int fd) |
| 7141 | { |
| 7142 | herefd = fd; |
| 7143 | expandarg(arg, (struct arglist *)NULL, 0); |
| 7144 | full_write(fd, stackblock(), expdest - (char *)stackblock()); |
| 7145 | } |
| 7146 | |
| 7147 | /* |
| 7148 | * Returns true if the pattern matches the string. |
| 7149 | */ |
| 7150 | static int |
| 7151 | patmatch(char *pattern, const char *string) |
| 7152 | { |
| 7153 | return pmatch(preglob(pattern, 0, 0), string); |
| 7154 | } |
| 7155 | |
| 7156 | /* |
| 7157 | * See if a pattern matches in a case statement. |
| 7158 | */ |
| 7159 | static int |
| 7160 | casematch(union node *pattern, char *val) |
| 7161 | { |
| 7162 | struct stackmark smark; |
| 7163 | int result; |
| 7164 | |
| 7165 | setstackmark(&smark); |
| 7166 | argbackq = pattern->narg.backquote; |
| 7167 | STARTSTACKSTR(expdest); |
| 7168 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7169 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE, |
| 7170 | /* var_str_list: */ NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7171 | STACKSTRNUL(expdest); |
| 7172 | result = patmatch(stackblock(), val); |
| 7173 | popstackmark(&smark); |
| 7174 | return result; |
| 7175 | } |
| 7176 | |
| 7177 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7178 | /* ============ find_command */ |
| 7179 | |
| 7180 | struct builtincmd { |
| 7181 | const char *name; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7182 | int (*builtin)(int, char **) FAST_FUNC; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7183 | /* unsigned flags; */ |
| 7184 | }; |
| 7185 | #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1) |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 7186 | /* "regular" builtins always take precedence over commands, |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7187 | * regardless of PATH=....%builtin... position */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7188 | #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7189 | #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7190 | |
| 7191 | struct cmdentry { |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7192 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7193 | union param { |
| 7194 | int index; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7195 | /* index >= 0 for commands without path (slashes) */ |
| 7196 | /* (TODO: what exactly does the value mean? PATH position?) */ |
| 7197 | /* index == -1 for commands with slashes */ |
| 7198 | /* index == (-2 - applet_no) for NOFORK applets */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7199 | const struct builtincmd *cmd; |
| 7200 | struct funcnode *func; |
| 7201 | } u; |
| 7202 | }; |
| 7203 | /* values of cmdtype */ |
| 7204 | #define CMDUNKNOWN -1 /* no entry in table for command */ |
| 7205 | #define CMDNORMAL 0 /* command is an executable program */ |
| 7206 | #define CMDFUNCTION 1 /* command is a shell function */ |
| 7207 | #define CMDBUILTIN 2 /* command is a shell builtin */ |
| 7208 | |
| 7209 | /* action to find_command() */ |
| 7210 | #define DO_ERR 0x01 /* prints errors */ |
| 7211 | #define DO_ABS 0x02 /* checks absolute paths */ |
| 7212 | #define DO_NOFUNC 0x04 /* don't return shell functions, for command */ |
| 7213 | #define DO_ALTPATH 0x08 /* using alternate path */ |
| 7214 | #define DO_ALTBLTIN 0x20 /* %builtin in alt. path */ |
| 7215 | |
| 7216 | static void find_command(char *, struct cmdentry *, int, const char *); |
| 7217 | |
| 7218 | |
| 7219 | /* ============ Hashing commands */ |
| 7220 | |
| 7221 | /* |
| 7222 | * When commands are first encountered, they are entered in a hash table. |
| 7223 | * This ensures that a full path search will not have to be done for them |
| 7224 | * on each invocation. |
| 7225 | * |
| 7226 | * We should investigate converting to a linear search, even though that |
| 7227 | * would make the command name "hash" a misnomer. |
| 7228 | */ |
| 7229 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7230 | struct tblentry { |
| 7231 | struct tblentry *next; /* next entry in hash chain */ |
| 7232 | union param param; /* definition of builtin function */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7233 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7234 | char rehash; /* if set, cd done since entry created */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7235 | char cmdname[1]; /* name of command */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7236 | }; |
| 7237 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 7238 | static struct tblentry **cmdtable; |
| 7239 | #define INIT_G_cmdtable() do { \ |
| 7240 | cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \ |
| 7241 | } while (0) |
| 7242 | |
| 7243 | static int builtinloc = -1; /* index in path of %builtin, or -1 */ |
| 7244 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7245 | |
| 7246 | static void |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7247 | tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) char *cmd, char **argv, char **envp) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7248 | { |
| 7249 | int repeated = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7250 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7251 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7252 | if (applet_no >= 0) { |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7253 | if (APPLET_IS_NOEXEC(applet_no)) { |
| 7254 | while (*envp) |
| 7255 | putenv(*envp++); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7256 | run_applet_no_and_exit(applet_no, argv); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7257 | } |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7258 | /* re-exec ourselves with the new arguments */ |
| 7259 | execve(bb_busybox_exec_path, argv, envp); |
| 7260 | /* If they called chroot or otherwise made the binary no longer |
| 7261 | * executable, fall through */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7262 | } |
| 7263 | #endif |
| 7264 | |
| 7265 | repeat: |
| 7266 | #ifdef SYSV |
| 7267 | do { |
| 7268 | execve(cmd, argv, envp); |
| 7269 | } while (errno == EINTR); |
| 7270 | #else |
| 7271 | execve(cmd, argv, envp); |
| 7272 | #endif |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7273 | if (repeated) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7274 | free(argv); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7275 | return; |
| 7276 | } |
| 7277 | if (errno == ENOEXEC) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7278 | char **ap; |
| 7279 | char **new; |
| 7280 | |
| 7281 | for (ap = argv; *ap; ap++) |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7282 | continue; |
| 7283 | ap = new = ckmalloc((ap - argv + 2) * sizeof(ap[0])); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7284 | ap[1] = cmd; |
Denis Vlasenko | c44ab01 | 2007-04-09 03:11:58 +0000 | [diff] [blame] | 7285 | ap[0] = cmd = (char *)DEFAULT_SHELL; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7286 | ap += 2; |
| 7287 | argv++; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7288 | while ((*ap++ = *argv++) != NULL) |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7289 | continue; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7290 | argv = new; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7291 | repeated++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7292 | goto repeat; |
| 7293 | } |
| 7294 | } |
| 7295 | |
| 7296 | /* |
| 7297 | * Exec a program. Never returns. If you change this routine, you may |
| 7298 | * have to change the find_command routine as well. |
| 7299 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7300 | static void shellexec(char **, const char *, int) NORETURN; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7301 | static void |
| 7302 | shellexec(char **argv, const char *path, int idx) |
| 7303 | { |
| 7304 | char *cmdname; |
| 7305 | int e; |
| 7306 | char **envp; |
| 7307 | int exerrno; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7308 | #if ENABLE_FEATURE_SH_STANDALONE |
| 7309 | int applet_no = -1; |
| 7310 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7311 | |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 7312 | clearredir(/*drop:*/ 1); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7313 | envp = listvars(VEXPORT, VUNSET, 0); |
| 7314 | if (strchr(argv[0], '/') != NULL |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7315 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7316 | || (applet_no = find_applet_by_name(argv[0])) >= 0 |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7317 | #endif |
| 7318 | ) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7319 | tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) argv[0], argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7320 | e = errno; |
| 7321 | } else { |
| 7322 | e = ENOENT; |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7323 | while ((cmdname = path_advance(&path, argv[0])) != NULL) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7324 | if (--idx < 0 && pathopt == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7325 | tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7326 | if (errno != ENOENT && errno != ENOTDIR) |
| 7327 | e = errno; |
| 7328 | } |
| 7329 | stunalloc(cmdname); |
| 7330 | } |
| 7331 | } |
| 7332 | |
| 7333 | /* Map to POSIX errors */ |
| 7334 | switch (e) { |
| 7335 | case EACCES: |
| 7336 | exerrno = 126; |
| 7337 | break; |
| 7338 | case ENOENT: |
| 7339 | exerrno = 127; |
| 7340 | break; |
| 7341 | default: |
| 7342 | exerrno = 2; |
| 7343 | break; |
| 7344 | } |
| 7345 | exitstatus = exerrno; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7346 | TRACE(("shellexec failed for %s, errno %d, suppress_int %d\n", |
| 7347 | argv[0], e, suppress_int)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7348 | ash_msg_and_raise(EXEXEC, "%s: %s", argv[0], errmsg(e, "not found")); |
| 7349 | /* NOTREACHED */ |
| 7350 | } |
| 7351 | |
| 7352 | static void |
| 7353 | printentry(struct tblentry *cmdp) |
| 7354 | { |
| 7355 | int idx; |
| 7356 | const char *path; |
| 7357 | char *name; |
| 7358 | |
| 7359 | idx = cmdp->param.index; |
| 7360 | path = pathval(); |
| 7361 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7362 | name = path_advance(&path, cmdp->cmdname); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7363 | stunalloc(name); |
| 7364 | } while (--idx >= 0); |
| 7365 | out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr)); |
| 7366 | } |
| 7367 | |
| 7368 | /* |
| 7369 | * Clear out command entries. The argument specifies the first entry in |
| 7370 | * PATH which has changed. |
| 7371 | */ |
| 7372 | static void |
| 7373 | clearcmdentry(int firstchange) |
| 7374 | { |
| 7375 | struct tblentry **tblp; |
| 7376 | struct tblentry **pp; |
| 7377 | struct tblentry *cmdp; |
| 7378 | |
| 7379 | INT_OFF; |
| 7380 | for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) { |
| 7381 | pp = tblp; |
| 7382 | while ((cmdp = *pp) != NULL) { |
| 7383 | if ((cmdp->cmdtype == CMDNORMAL && |
| 7384 | cmdp->param.index >= firstchange) |
| 7385 | || (cmdp->cmdtype == CMDBUILTIN && |
| 7386 | builtinloc >= firstchange) |
| 7387 | ) { |
| 7388 | *pp = cmdp->next; |
| 7389 | free(cmdp); |
| 7390 | } else { |
| 7391 | pp = &cmdp->next; |
| 7392 | } |
| 7393 | } |
| 7394 | } |
| 7395 | INT_ON; |
| 7396 | } |
| 7397 | |
| 7398 | /* |
| 7399 | * Locate a command in the command hash table. If "add" is nonzero, |
| 7400 | * add the command to the table if it is not already present. The |
| 7401 | * variable "lastcmdentry" is set to point to the address of the link |
| 7402 | * pointing to the entry, so that delete_cmd_entry can delete the |
| 7403 | * entry. |
| 7404 | * |
| 7405 | * Interrupts must be off if called with add != 0. |
| 7406 | */ |
| 7407 | static struct tblentry **lastcmdentry; |
| 7408 | |
| 7409 | static struct tblentry * |
| 7410 | cmdlookup(const char *name, int add) |
| 7411 | { |
| 7412 | unsigned int hashval; |
| 7413 | const char *p; |
| 7414 | struct tblentry *cmdp; |
| 7415 | struct tblentry **pp; |
| 7416 | |
| 7417 | p = name; |
| 7418 | hashval = (unsigned char)*p << 4; |
| 7419 | while (*p) |
| 7420 | hashval += (unsigned char)*p++; |
| 7421 | hashval &= 0x7FFF; |
| 7422 | pp = &cmdtable[hashval % CMDTABLESIZE]; |
| 7423 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7424 | if (strcmp(cmdp->cmdname, name) == 0) |
| 7425 | break; |
| 7426 | pp = &cmdp->next; |
| 7427 | } |
| 7428 | if (add && cmdp == NULL) { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7429 | cmdp = *pp = ckzalloc(sizeof(struct tblentry) |
| 7430 | + strlen(name) |
| 7431 | /* + 1 - already done because |
| 7432 | * tblentry::cmdname is char[1] */); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7433 | /*cmdp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7434 | cmdp->cmdtype = CMDUNKNOWN; |
| 7435 | strcpy(cmdp->cmdname, name); |
| 7436 | } |
| 7437 | lastcmdentry = pp; |
| 7438 | return cmdp; |
| 7439 | } |
| 7440 | |
| 7441 | /* |
| 7442 | * Delete the command entry returned on the last lookup. |
| 7443 | */ |
| 7444 | static void |
| 7445 | delete_cmd_entry(void) |
| 7446 | { |
| 7447 | struct tblentry *cmdp; |
| 7448 | |
| 7449 | INT_OFF; |
| 7450 | cmdp = *lastcmdentry; |
| 7451 | *lastcmdentry = cmdp->next; |
| 7452 | if (cmdp->cmdtype == CMDFUNCTION) |
| 7453 | freefunc(cmdp->param.func); |
| 7454 | free(cmdp); |
| 7455 | INT_ON; |
| 7456 | } |
| 7457 | |
| 7458 | /* |
| 7459 | * Add a new command entry, replacing any existing command entry for |
| 7460 | * the same name - except special builtins. |
| 7461 | */ |
| 7462 | static void |
| 7463 | addcmdentry(char *name, struct cmdentry *entry) |
| 7464 | { |
| 7465 | struct tblentry *cmdp; |
| 7466 | |
| 7467 | cmdp = cmdlookup(name, 1); |
| 7468 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 7469 | freefunc(cmdp->param.func); |
| 7470 | } |
| 7471 | cmdp->cmdtype = entry->cmdtype; |
| 7472 | cmdp->param = entry->u; |
| 7473 | cmdp->rehash = 0; |
| 7474 | } |
| 7475 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7476 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7477 | hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7478 | { |
| 7479 | struct tblentry **pp; |
| 7480 | struct tblentry *cmdp; |
| 7481 | int c; |
| 7482 | struct cmdentry entry; |
| 7483 | char *name; |
| 7484 | |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7485 | if (nextopt("r") != '\0') { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7486 | clearcmdentry(0); |
| 7487 | return 0; |
| 7488 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7489 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7490 | if (*argptr == NULL) { |
| 7491 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7492 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7493 | if (cmdp->cmdtype == CMDNORMAL) |
| 7494 | printentry(cmdp); |
| 7495 | } |
| 7496 | } |
| 7497 | return 0; |
| 7498 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7499 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7500 | c = 0; |
| 7501 | while ((name = *argptr) != NULL) { |
| 7502 | cmdp = cmdlookup(name, 0); |
| 7503 | if (cmdp != NULL |
| 7504 | && (cmdp->cmdtype == CMDNORMAL |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7505 | || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)) |
| 7506 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7507 | delete_cmd_entry(); |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7508 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7509 | find_command(name, &entry, DO_ERR, pathval()); |
| 7510 | if (entry.cmdtype == CMDUNKNOWN) |
| 7511 | c = 1; |
| 7512 | argptr++; |
| 7513 | } |
| 7514 | return c; |
| 7515 | } |
| 7516 | |
| 7517 | /* |
| 7518 | * Called when a cd is done. Marks all commands so the next time they |
| 7519 | * are executed they will be rehashed. |
| 7520 | */ |
| 7521 | static void |
| 7522 | hashcd(void) |
| 7523 | { |
| 7524 | struct tblentry **pp; |
| 7525 | struct tblentry *cmdp; |
| 7526 | |
| 7527 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7528 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7529 | if (cmdp->cmdtype == CMDNORMAL |
| 7530 | || (cmdp->cmdtype == CMDBUILTIN |
| 7531 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
| 7532 | && builtinloc > 0) |
| 7533 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7534 | cmdp->rehash = 1; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7535 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7536 | } |
| 7537 | } |
| 7538 | } |
| 7539 | |
| 7540 | /* |
| 7541 | * Fix command hash table when PATH changed. |
| 7542 | * Called before PATH is changed. The argument is the new value of PATH; |
| 7543 | * pathval() still returns the old value at this point. |
| 7544 | * Called with interrupts off. |
| 7545 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7546 | static void FAST_FUNC |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7547 | changepath(const char *new) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7548 | { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7549 | const char *old; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7550 | int firstchange; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7551 | int idx; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7552 | int idx_bltin; |
| 7553 | |
| 7554 | old = pathval(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7555 | firstchange = 9999; /* assume no change */ |
| 7556 | idx = 0; |
| 7557 | idx_bltin = -1; |
| 7558 | for (;;) { |
| 7559 | if (*old != *new) { |
| 7560 | firstchange = idx; |
| 7561 | if ((*old == '\0' && *new == ':') |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7562 | || (*old == ':' && *new == '\0') |
| 7563 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7564 | firstchange++; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7565 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7566 | old = new; /* ignore subsequent differences */ |
| 7567 | } |
| 7568 | if (*new == '\0') |
| 7569 | break; |
| 7570 | if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin")) |
| 7571 | idx_bltin = idx; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7572 | if (*new == ':') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7573 | idx++; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7574 | new++; |
| 7575 | old++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7576 | } |
| 7577 | if (builtinloc < 0 && idx_bltin >= 0) |
| 7578 | builtinloc = idx_bltin; /* zap builtins */ |
| 7579 | if (builtinloc >= 0 && idx_bltin < 0) |
| 7580 | firstchange = 0; |
| 7581 | clearcmdentry(firstchange); |
| 7582 | builtinloc = idx_bltin; |
| 7583 | } |
| 7584 | |
| 7585 | #define TEOF 0 |
| 7586 | #define TNL 1 |
| 7587 | #define TREDIR 2 |
| 7588 | #define TWORD 3 |
| 7589 | #define TSEMI 4 |
| 7590 | #define TBACKGND 5 |
| 7591 | #define TAND 6 |
| 7592 | #define TOR 7 |
| 7593 | #define TPIPE 8 |
| 7594 | #define TLP 9 |
| 7595 | #define TRP 10 |
| 7596 | #define TENDCASE 11 |
| 7597 | #define TENDBQUOTE 12 |
| 7598 | #define TNOT 13 |
| 7599 | #define TCASE 14 |
| 7600 | #define TDO 15 |
| 7601 | #define TDONE 16 |
| 7602 | #define TELIF 17 |
| 7603 | #define TELSE 18 |
| 7604 | #define TESAC 19 |
| 7605 | #define TFI 20 |
| 7606 | #define TFOR 21 |
| 7607 | #define TIF 22 |
| 7608 | #define TIN 23 |
| 7609 | #define TTHEN 24 |
| 7610 | #define TUNTIL 25 |
| 7611 | #define TWHILE 26 |
| 7612 | #define TBEGIN 27 |
| 7613 | #define TEND 28 |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7614 | typedef smallint token_id_t; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7615 | |
| 7616 | /* first char is indicating which tokens mark the end of a list */ |
| 7617 | static const char *const tokname_array[] = { |
| 7618 | "\1end of file", |
| 7619 | "\0newline", |
| 7620 | "\0redirection", |
| 7621 | "\0word", |
| 7622 | "\0;", |
| 7623 | "\0&", |
| 7624 | "\0&&", |
| 7625 | "\0||", |
| 7626 | "\0|", |
| 7627 | "\0(", |
| 7628 | "\1)", |
| 7629 | "\1;;", |
| 7630 | "\1`", |
| 7631 | #define KWDOFFSET 13 |
| 7632 | /* the following are keywords */ |
| 7633 | "\0!", |
| 7634 | "\0case", |
| 7635 | "\1do", |
| 7636 | "\1done", |
| 7637 | "\1elif", |
| 7638 | "\1else", |
| 7639 | "\1esac", |
| 7640 | "\1fi", |
| 7641 | "\0for", |
| 7642 | "\0if", |
| 7643 | "\0in", |
| 7644 | "\1then", |
| 7645 | "\0until", |
| 7646 | "\0while", |
| 7647 | "\0{", |
| 7648 | "\1}", |
| 7649 | }; |
| 7650 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7651 | /* Wrapper around strcmp for qsort/bsearch/... */ |
| 7652 | static int |
| 7653 | pstrcmp(const void *a, const void *b) |
| 7654 | { |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 7655 | return strcmp((char*) a, (*(char**) b) + 1); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7656 | } |
| 7657 | |
| 7658 | static const char *const * |
| 7659 | findkwd(const char *s) |
| 7660 | { |
| 7661 | return bsearch(s, tokname_array + KWDOFFSET, |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 7662 | ARRAY_SIZE(tokname_array) - KWDOFFSET, |
| 7663 | sizeof(tokname_array[0]), pstrcmp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7664 | } |
| 7665 | |
| 7666 | /* |
| 7667 | * Locate and print what a word is... |
| 7668 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7669 | static int |
| 7670 | describe_command(char *command, int describe_command_verbose) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7671 | { |
| 7672 | struct cmdentry entry; |
| 7673 | struct tblentry *cmdp; |
| 7674 | #if ENABLE_ASH_ALIAS |
| 7675 | const struct alias *ap; |
| 7676 | #endif |
| 7677 | const char *path = pathval(); |
| 7678 | |
| 7679 | if (describe_command_verbose) { |
| 7680 | out1str(command); |
| 7681 | } |
| 7682 | |
| 7683 | /* First look at the keywords */ |
| 7684 | if (findkwd(command)) { |
| 7685 | out1str(describe_command_verbose ? " is a shell keyword" : command); |
| 7686 | goto out; |
| 7687 | } |
| 7688 | |
| 7689 | #if ENABLE_ASH_ALIAS |
| 7690 | /* Then look at the aliases */ |
| 7691 | ap = lookupalias(command, 0); |
| 7692 | if (ap != NULL) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7693 | if (!describe_command_verbose) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7694 | out1str("alias "); |
| 7695 | printalias(ap); |
| 7696 | return 0; |
| 7697 | } |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7698 | out1fmt(" is an alias for %s", ap->val); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7699 | goto out; |
| 7700 | } |
| 7701 | #endif |
| 7702 | /* Then check if it is a tracked alias */ |
| 7703 | cmdp = cmdlookup(command, 0); |
| 7704 | if (cmdp != NULL) { |
| 7705 | entry.cmdtype = cmdp->cmdtype; |
| 7706 | entry.u = cmdp->param; |
| 7707 | } else { |
| 7708 | /* Finally use brute force */ |
| 7709 | find_command(command, &entry, DO_ABS, path); |
| 7710 | } |
| 7711 | |
| 7712 | switch (entry.cmdtype) { |
| 7713 | case CMDNORMAL: { |
| 7714 | int j = entry.u.index; |
| 7715 | char *p; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7716 | if (j < 0) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7717 | p = command; |
| 7718 | } else { |
| 7719 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7720 | p = path_advance(&path, command); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7721 | stunalloc(p); |
| 7722 | } while (--j >= 0); |
| 7723 | } |
| 7724 | if (describe_command_verbose) { |
| 7725 | out1fmt(" is%s %s", |
| 7726 | (cmdp ? " a tracked alias for" : nullstr), p |
| 7727 | ); |
| 7728 | } else { |
| 7729 | out1str(p); |
| 7730 | } |
| 7731 | break; |
| 7732 | } |
| 7733 | |
| 7734 | case CMDFUNCTION: |
| 7735 | if (describe_command_verbose) { |
| 7736 | out1str(" is a shell function"); |
| 7737 | } else { |
| 7738 | out1str(command); |
| 7739 | } |
| 7740 | break; |
| 7741 | |
| 7742 | case CMDBUILTIN: |
| 7743 | if (describe_command_verbose) { |
| 7744 | out1fmt(" is a %sshell builtin", |
| 7745 | IS_BUILTIN_SPECIAL(entry.u.cmd) ? |
| 7746 | "special " : nullstr |
| 7747 | ); |
| 7748 | } else { |
| 7749 | out1str(command); |
| 7750 | } |
| 7751 | break; |
| 7752 | |
| 7753 | default: |
| 7754 | if (describe_command_verbose) { |
| 7755 | out1str(": not found\n"); |
| 7756 | } |
| 7757 | return 127; |
| 7758 | } |
| 7759 | out: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 7760 | out1str("\n"); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7761 | return 0; |
| 7762 | } |
| 7763 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7764 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7765 | typecmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7766 | { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7767 | int i = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7768 | int err = 0; |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7769 | int verbose = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7770 | |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7771 | /* type -p ... ? (we don't bother checking for 'p') */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 7772 | if (argv[1] && argv[1][0] == '-') { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7773 | i++; |
| 7774 | verbose = 0; |
| 7775 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7776 | while (argv[i]) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7777 | err |= describe_command(argv[i++], verbose); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7778 | } |
| 7779 | return err; |
| 7780 | } |
| 7781 | |
| 7782 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7783 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7784 | commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7785 | { |
| 7786 | int c; |
| 7787 | enum { |
| 7788 | VERIFY_BRIEF = 1, |
| 7789 | VERIFY_VERBOSE = 2, |
| 7790 | } verify = 0; |
| 7791 | |
| 7792 | while ((c = nextopt("pvV")) != '\0') |
| 7793 | if (c == 'V') |
| 7794 | verify |= VERIFY_VERBOSE; |
| 7795 | else if (c == 'v') |
| 7796 | verify |= VERIFY_BRIEF; |
| 7797 | #if DEBUG |
| 7798 | else if (c != 'p') |
| 7799 | abort(); |
| 7800 | #endif |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 7801 | /* Mimic bash: just "command -v" doesn't complain, it's a nop */ |
| 7802 | if (verify && (*argptr != NULL)) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7803 | return describe_command(*argptr, verify - VERIFY_BRIEF); |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 7804 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7805 | |
| 7806 | return 0; |
| 7807 | } |
| 7808 | #endif |
| 7809 | |
| 7810 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7811 | /* ============ eval.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7812 | |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7813 | static int funcblocksize; /* size of structures in function */ |
| 7814 | static int funcstringsize; /* size of strings in node */ |
| 7815 | static void *funcblock; /* block to allocate function from */ |
| 7816 | static char *funcstring; /* block to allocate strings from */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7817 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7818 | /* flags in argument to evaltree */ |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7819 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 7820 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7821 | #define EV_BACKCMD 04 /* command executing within back quotes */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7822 | |
Denys Vlasenko | 0e5e4ea | 2009-10-11 00:36:20 +0200 | [diff] [blame] | 7823 | static const uint8_t nodesize[N_NUMBER] = { |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7824 | [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)), |
| 7825 | [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)), |
| 7826 | [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7827 | [NBACKGND ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7828 | [NSUBSHELL] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7829 | [NAND ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7830 | [NOR ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7831 | [NSEMI ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7832 | [NIF ] = SHELL_ALIGN(sizeof(struct nif)), |
| 7833 | [NWHILE ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7834 | [NUNTIL ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7835 | [NFOR ] = SHELL_ALIGN(sizeof(struct nfor)), |
| 7836 | [NCASE ] = SHELL_ALIGN(sizeof(struct ncase)), |
| 7837 | [NCLIST ] = SHELL_ALIGN(sizeof(struct nclist)), |
| 7838 | [NDEFUN ] = SHELL_ALIGN(sizeof(struct narg)), |
| 7839 | [NARG ] = SHELL_ALIGN(sizeof(struct narg)), |
| 7840 | [NTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 7841 | #if ENABLE_ASH_BASH_COMPAT |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7842 | [NTO2 ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 7843 | #endif |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7844 | [NCLOBBER ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7845 | [NFROM ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7846 | [NFROMTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7847 | [NAPPEND ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7848 | [NTOFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 7849 | [NFROMFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 7850 | [NHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 7851 | [NXHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 7852 | [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7853 | }; |
| 7854 | |
| 7855 | static void calcsize(union node *n); |
| 7856 | |
| 7857 | static void |
| 7858 | sizenodelist(struct nodelist *lp) |
| 7859 | { |
| 7860 | while (lp) { |
| 7861 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); |
| 7862 | calcsize(lp->n); |
| 7863 | lp = lp->next; |
| 7864 | } |
| 7865 | } |
| 7866 | |
| 7867 | static void |
| 7868 | calcsize(union node *n) |
| 7869 | { |
| 7870 | if (n == NULL) |
| 7871 | return; |
| 7872 | funcblocksize += nodesize[n->type]; |
| 7873 | switch (n->type) { |
| 7874 | case NCMD: |
| 7875 | calcsize(n->ncmd.redirect); |
| 7876 | calcsize(n->ncmd.args); |
| 7877 | calcsize(n->ncmd.assign); |
| 7878 | break; |
| 7879 | case NPIPE: |
| 7880 | sizenodelist(n->npipe.cmdlist); |
| 7881 | break; |
| 7882 | case NREDIR: |
| 7883 | case NBACKGND: |
| 7884 | case NSUBSHELL: |
| 7885 | calcsize(n->nredir.redirect); |
| 7886 | calcsize(n->nredir.n); |
| 7887 | break; |
| 7888 | case NAND: |
| 7889 | case NOR: |
| 7890 | case NSEMI: |
| 7891 | case NWHILE: |
| 7892 | case NUNTIL: |
| 7893 | calcsize(n->nbinary.ch2); |
| 7894 | calcsize(n->nbinary.ch1); |
| 7895 | break; |
| 7896 | case NIF: |
| 7897 | calcsize(n->nif.elsepart); |
| 7898 | calcsize(n->nif.ifpart); |
| 7899 | calcsize(n->nif.test); |
| 7900 | break; |
| 7901 | case NFOR: |
| 7902 | funcstringsize += strlen(n->nfor.var) + 1; |
| 7903 | calcsize(n->nfor.body); |
| 7904 | calcsize(n->nfor.args); |
| 7905 | break; |
| 7906 | case NCASE: |
| 7907 | calcsize(n->ncase.cases); |
| 7908 | calcsize(n->ncase.expr); |
| 7909 | break; |
| 7910 | case NCLIST: |
| 7911 | calcsize(n->nclist.body); |
| 7912 | calcsize(n->nclist.pattern); |
| 7913 | calcsize(n->nclist.next); |
| 7914 | break; |
| 7915 | case NDEFUN: |
| 7916 | case NARG: |
| 7917 | sizenodelist(n->narg.backquote); |
| 7918 | funcstringsize += strlen(n->narg.text) + 1; |
| 7919 | calcsize(n->narg.next); |
| 7920 | break; |
| 7921 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 7922 | #if ENABLE_ASH_BASH_COMPAT |
| 7923 | case NTO2: |
| 7924 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7925 | case NCLOBBER: |
| 7926 | case NFROM: |
| 7927 | case NFROMTO: |
| 7928 | case NAPPEND: |
| 7929 | calcsize(n->nfile.fname); |
| 7930 | calcsize(n->nfile.next); |
| 7931 | break; |
| 7932 | case NTOFD: |
| 7933 | case NFROMFD: |
| 7934 | calcsize(n->ndup.vname); |
| 7935 | calcsize(n->ndup.next); |
| 7936 | break; |
| 7937 | case NHERE: |
| 7938 | case NXHERE: |
| 7939 | calcsize(n->nhere.doc); |
| 7940 | calcsize(n->nhere.next); |
| 7941 | break; |
| 7942 | case NNOT: |
| 7943 | calcsize(n->nnot.com); |
| 7944 | break; |
| 7945 | }; |
| 7946 | } |
| 7947 | |
| 7948 | static char * |
| 7949 | nodeckstrdup(char *s) |
| 7950 | { |
| 7951 | char *rtn = funcstring; |
| 7952 | |
| 7953 | strcpy(funcstring, s); |
| 7954 | funcstring += strlen(s) + 1; |
| 7955 | return rtn; |
| 7956 | } |
| 7957 | |
| 7958 | static union node *copynode(union node *); |
| 7959 | |
| 7960 | static struct nodelist * |
| 7961 | copynodelist(struct nodelist *lp) |
| 7962 | { |
| 7963 | struct nodelist *start; |
| 7964 | struct nodelist **lpp; |
| 7965 | |
| 7966 | lpp = &start; |
| 7967 | while (lp) { |
| 7968 | *lpp = funcblock; |
| 7969 | funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist)); |
| 7970 | (*lpp)->n = copynode(lp->n); |
| 7971 | lp = lp->next; |
| 7972 | lpp = &(*lpp)->next; |
| 7973 | } |
| 7974 | *lpp = NULL; |
| 7975 | return start; |
| 7976 | } |
| 7977 | |
| 7978 | static union node * |
| 7979 | copynode(union node *n) |
| 7980 | { |
| 7981 | union node *new; |
| 7982 | |
| 7983 | if (n == NULL) |
| 7984 | return NULL; |
| 7985 | new = funcblock; |
| 7986 | funcblock = (char *) funcblock + nodesize[n->type]; |
| 7987 | |
| 7988 | switch (n->type) { |
| 7989 | case NCMD: |
| 7990 | new->ncmd.redirect = copynode(n->ncmd.redirect); |
| 7991 | new->ncmd.args = copynode(n->ncmd.args); |
| 7992 | new->ncmd.assign = copynode(n->ncmd.assign); |
| 7993 | break; |
| 7994 | case NPIPE: |
| 7995 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7996 | new->npipe.pipe_backgnd = n->npipe.pipe_backgnd; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7997 | break; |
| 7998 | case NREDIR: |
| 7999 | case NBACKGND: |
| 8000 | case NSUBSHELL: |
| 8001 | new->nredir.redirect = copynode(n->nredir.redirect); |
| 8002 | new->nredir.n = copynode(n->nredir.n); |
| 8003 | break; |
| 8004 | case NAND: |
| 8005 | case NOR: |
| 8006 | case NSEMI: |
| 8007 | case NWHILE: |
| 8008 | case NUNTIL: |
| 8009 | new->nbinary.ch2 = copynode(n->nbinary.ch2); |
| 8010 | new->nbinary.ch1 = copynode(n->nbinary.ch1); |
| 8011 | break; |
| 8012 | case NIF: |
| 8013 | new->nif.elsepart = copynode(n->nif.elsepart); |
| 8014 | new->nif.ifpart = copynode(n->nif.ifpart); |
| 8015 | new->nif.test = copynode(n->nif.test); |
| 8016 | break; |
| 8017 | case NFOR: |
| 8018 | new->nfor.var = nodeckstrdup(n->nfor.var); |
| 8019 | new->nfor.body = copynode(n->nfor.body); |
| 8020 | new->nfor.args = copynode(n->nfor.args); |
| 8021 | break; |
| 8022 | case NCASE: |
| 8023 | new->ncase.cases = copynode(n->ncase.cases); |
| 8024 | new->ncase.expr = copynode(n->ncase.expr); |
| 8025 | break; |
| 8026 | case NCLIST: |
| 8027 | new->nclist.body = copynode(n->nclist.body); |
| 8028 | new->nclist.pattern = copynode(n->nclist.pattern); |
| 8029 | new->nclist.next = copynode(n->nclist.next); |
| 8030 | break; |
| 8031 | case NDEFUN: |
| 8032 | case NARG: |
| 8033 | new->narg.backquote = copynodelist(n->narg.backquote); |
| 8034 | new->narg.text = nodeckstrdup(n->narg.text); |
| 8035 | new->narg.next = copynode(n->narg.next); |
| 8036 | break; |
| 8037 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8038 | #if ENABLE_ASH_BASH_COMPAT |
| 8039 | case NTO2: |
| 8040 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8041 | case NCLOBBER: |
| 8042 | case NFROM: |
| 8043 | case NFROMTO: |
| 8044 | case NAPPEND: |
| 8045 | new->nfile.fname = copynode(n->nfile.fname); |
| 8046 | new->nfile.fd = n->nfile.fd; |
| 8047 | new->nfile.next = copynode(n->nfile.next); |
| 8048 | break; |
| 8049 | case NTOFD: |
| 8050 | case NFROMFD: |
| 8051 | new->ndup.vname = copynode(n->ndup.vname); |
| 8052 | new->ndup.dupfd = n->ndup.dupfd; |
| 8053 | new->ndup.fd = n->ndup.fd; |
| 8054 | new->ndup.next = copynode(n->ndup.next); |
| 8055 | break; |
| 8056 | case NHERE: |
| 8057 | case NXHERE: |
| 8058 | new->nhere.doc = copynode(n->nhere.doc); |
| 8059 | new->nhere.fd = n->nhere.fd; |
| 8060 | new->nhere.next = copynode(n->nhere.next); |
| 8061 | break; |
| 8062 | case NNOT: |
| 8063 | new->nnot.com = copynode(n->nnot.com); |
| 8064 | break; |
| 8065 | }; |
| 8066 | new->type = n->type; |
| 8067 | return new; |
| 8068 | } |
| 8069 | |
| 8070 | /* |
| 8071 | * Make a copy of a parse tree. |
| 8072 | */ |
| 8073 | static struct funcnode * |
| 8074 | copyfunc(union node *n) |
| 8075 | { |
| 8076 | struct funcnode *f; |
| 8077 | size_t blocksize; |
| 8078 | |
| 8079 | funcblocksize = offsetof(struct funcnode, n); |
| 8080 | funcstringsize = 0; |
| 8081 | calcsize(n); |
| 8082 | blocksize = funcblocksize; |
| 8083 | f = ckmalloc(blocksize + funcstringsize); |
| 8084 | funcblock = (char *) f + offsetof(struct funcnode, n); |
| 8085 | funcstring = (char *) f + blocksize; |
| 8086 | copynode(n); |
| 8087 | f->count = 0; |
| 8088 | return f; |
| 8089 | } |
| 8090 | |
| 8091 | /* |
| 8092 | * Define a shell function. |
| 8093 | */ |
| 8094 | static void |
| 8095 | defun(char *name, union node *func) |
| 8096 | { |
| 8097 | struct cmdentry entry; |
| 8098 | |
| 8099 | INT_OFF; |
| 8100 | entry.cmdtype = CMDFUNCTION; |
| 8101 | entry.u.func = copyfunc(func); |
| 8102 | addcmdentry(name, &entry); |
| 8103 | INT_ON; |
| 8104 | } |
| 8105 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8106 | /* Reasons for skipping commands (see comment on breakcmd routine) */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8107 | #define SKIPBREAK (1 << 0) |
| 8108 | #define SKIPCONT (1 << 1) |
| 8109 | #define SKIPFUNC (1 << 2) |
| 8110 | #define SKIPFILE (1 << 3) |
| 8111 | #define SKIPEVAL (1 << 4) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8112 | static smallint evalskip; /* set to SKIPxxx if we are skipping commands */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8113 | static int skipcount; /* number of levels to skip */ |
| 8114 | static int funcnest; /* depth of function calls */ |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 8115 | static int loopnest; /* current loop nesting level */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8116 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8117 | /* Forward decl way out to parsing code - dotrap needs it */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8118 | static int evalstring(char *s, int mask); |
| 8119 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8120 | /* Called to execute a trap. |
| 8121 | * Single callsite - at the end of evaltree(). |
| 8122 | * If we return non-zero, exaltree raises EXEXIT exception. |
| 8123 | * |
| 8124 | * Perhaps we should avoid entering new trap handlers |
| 8125 | * while we are executing a trap handler. [is it a TODO?] |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8126 | */ |
| 8127 | static int |
| 8128 | dotrap(void) |
| 8129 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8130 | uint8_t *g; |
| 8131 | int sig; |
| 8132 | uint8_t savestatus; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8133 | |
| 8134 | savestatus = exitstatus; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8135 | pending_sig = 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8136 | xbarrier(); |
| 8137 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8138 | TRACE(("dotrap entered\n")); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8139 | for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) { |
| 8140 | int want_exexit; |
| 8141 | char *t; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8142 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8143 | if (*g == 0) |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8144 | continue; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8145 | t = trap[sig]; |
| 8146 | /* non-trapped SIGINT is handled separately by raise_interrupt, |
| 8147 | * don't upset it by resetting gotsig[SIGINT-1] */ |
| 8148 | if (sig == SIGINT && !t) |
| 8149 | continue; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8150 | |
| 8151 | TRACE(("sig %d is active, will run handler '%s'\n", sig, t)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8152 | *g = 0; |
| 8153 | if (!t) |
| 8154 | continue; |
| 8155 | want_exexit = evalstring(t, SKIPEVAL); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8156 | exitstatus = savestatus; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8157 | if (want_exexit) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8158 | TRACE(("dotrap returns %d\n", want_exexit)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8159 | return want_exexit; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8160 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8161 | } |
| 8162 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8163 | TRACE(("dotrap returns 0\n")); |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 8164 | return 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8165 | } |
| 8166 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8167 | /* forward declarations - evaluation is fairly recursive business... */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8168 | static void evalloop(union node *, int); |
| 8169 | static void evalfor(union node *, int); |
| 8170 | static void evalcase(union node *, int); |
| 8171 | static void evalsubshell(union node *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8172 | static void expredir(union node *); |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8173 | static void evalpipe(union node *, int); |
| 8174 | static void evalcommand(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8175 | static int evalbltin(const struct builtincmd *, int, char **); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8176 | static void prehash(union node *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8177 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8178 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8179 | * Evaluate a parse tree. The value is left in the global variable |
| 8180 | * exitstatus. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8181 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8182 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8183 | evaltree(union node *n, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8184 | { |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8185 | struct jmploc *volatile savehandler = exception_handler; |
| 8186 | struct jmploc jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8187 | int checkexit = 0; |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8188 | void (*evalfn)(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8189 | int status; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8190 | int int_level; |
| 8191 | |
| 8192 | SAVE_INT(int_level); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8193 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8194 | if (n == NULL) { |
| 8195 | TRACE(("evaltree(NULL) called\n")); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8196 | goto out1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8197 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8198 | TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8199 | |
| 8200 | exception_handler = &jmploc; |
| 8201 | { |
| 8202 | int err = setjmp(jmploc.loc); |
| 8203 | if (err) { |
| 8204 | /* if it was a signal, check for trap handlers */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8205 | if (exception_type == EXSIG) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8206 | TRACE(("exception %d (EXSIG) in evaltree, err=%d\n", |
| 8207 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8208 | goto out; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8209 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8210 | /* continue on the way out */ |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8211 | TRACE(("exception %d in evaltree, propagating err=%d\n", |
| 8212 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8213 | exception_handler = savehandler; |
| 8214 | longjmp(exception_handler->loc, err); |
| 8215 | } |
| 8216 | } |
| 8217 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8218 | switch (n->type) { |
| 8219 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 8220 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8221 | out1fmt("Node type = %d\n", n->type); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8222 | fflush_all(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8223 | break; |
| 8224 | #endif |
| 8225 | case NNOT: |
| 8226 | evaltree(n->nnot.com, EV_TESTED); |
| 8227 | status = !exitstatus; |
| 8228 | goto setstatus; |
| 8229 | case NREDIR: |
| 8230 | expredir(n->nredir.redirect); |
| 8231 | status = redirectsafe(n->nredir.redirect, REDIR_PUSH); |
| 8232 | if (!status) { |
| 8233 | evaltree(n->nredir.n, flags & EV_TESTED); |
| 8234 | status = exitstatus; |
| 8235 | } |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 8236 | popredir(/*drop:*/ 0, /*restore:*/ 0 /* not sure */); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8237 | goto setstatus; |
| 8238 | case NCMD: |
| 8239 | evalfn = evalcommand; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8240 | checkexit: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8241 | if (eflag && !(flags & EV_TESTED)) |
| 8242 | checkexit = ~0; |
| 8243 | goto calleval; |
| 8244 | case NFOR: |
| 8245 | evalfn = evalfor; |
| 8246 | goto calleval; |
| 8247 | case NWHILE: |
| 8248 | case NUNTIL: |
| 8249 | evalfn = evalloop; |
| 8250 | goto calleval; |
| 8251 | case NSUBSHELL: |
| 8252 | case NBACKGND: |
| 8253 | evalfn = evalsubshell; |
| 8254 | goto calleval; |
| 8255 | case NPIPE: |
| 8256 | evalfn = evalpipe; |
| 8257 | goto checkexit; |
| 8258 | case NCASE: |
| 8259 | evalfn = evalcase; |
| 8260 | goto calleval; |
| 8261 | case NAND: |
| 8262 | case NOR: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8263 | case NSEMI: { |
| 8264 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8265 | #if NAND + 1 != NOR |
| 8266 | #error NAND + 1 != NOR |
| 8267 | #endif |
| 8268 | #if NOR + 1 != NSEMI |
| 8269 | #error NOR + 1 != NSEMI |
| 8270 | #endif |
Denis Vlasenko | 87d5fd9 | 2008-07-26 13:48:35 +0000 | [diff] [blame] | 8271 | unsigned is_or = n->type - NAND; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8272 | evaltree( |
| 8273 | n->nbinary.ch1, |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8274 | (flags | ((is_or >> 1) - 1)) & EV_TESTED |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8275 | ); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8276 | if (!exitstatus == is_or) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8277 | break; |
| 8278 | if (!evalskip) { |
| 8279 | n = n->nbinary.ch2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8280 | evaln: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8281 | evalfn = evaltree; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8282 | calleval: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8283 | evalfn(n, flags); |
| 8284 | break; |
| 8285 | } |
| 8286 | break; |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8287 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8288 | case NIF: |
| 8289 | evaltree(n->nif.test, EV_TESTED); |
| 8290 | if (evalskip) |
| 8291 | break; |
| 8292 | if (exitstatus == 0) { |
| 8293 | n = n->nif.ifpart; |
| 8294 | goto evaln; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8295 | } |
| 8296 | if (n->nif.elsepart) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8297 | n = n->nif.elsepart; |
| 8298 | goto evaln; |
| 8299 | } |
| 8300 | goto success; |
| 8301 | case NDEFUN: |
| 8302 | defun(n->narg.text, n->narg.next); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8303 | success: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8304 | status = 0; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8305 | setstatus: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8306 | exitstatus = status; |
| 8307 | break; |
| 8308 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8309 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8310 | out: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8311 | exception_handler = savehandler; |
| 8312 | out1: |
| 8313 | if (checkexit & exitstatus) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8314 | evalskip |= SKIPEVAL; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8315 | else if (pending_sig && dotrap()) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8316 | goto exexit; |
| 8317 | |
| 8318 | if (flags & EV_EXIT) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8319 | exexit: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8320 | raise_exception(EXEXIT); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8321 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8322 | |
| 8323 | RESTORE_INT(int_level); |
| 8324 | TRACE(("leaving evaltree (no interrupts)\n")); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8325 | } |
| 8326 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8327 | #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3) |
| 8328 | static |
| 8329 | #endif |
| 8330 | void evaltreenr(union node *, int) __attribute__ ((alias("evaltree"),__noreturn__)); |
| 8331 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8332 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8333 | evalloop(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8334 | { |
| 8335 | int status; |
| 8336 | |
| 8337 | loopnest++; |
| 8338 | status = 0; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8339 | flags &= EV_TESTED; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8340 | for (;;) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8341 | int i; |
| 8342 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8343 | evaltree(n->nbinary.ch1, EV_TESTED); |
| 8344 | if (evalskip) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8345 | skipping: |
| 8346 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8347 | evalskip = 0; |
| 8348 | continue; |
| 8349 | } |
| 8350 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8351 | evalskip = 0; |
| 8352 | break; |
| 8353 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8354 | i = exitstatus; |
| 8355 | if (n->type != NWHILE) |
| 8356 | i = !i; |
| 8357 | if (i != 0) |
| 8358 | break; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8359 | evaltree(n->nbinary.ch2, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8360 | status = exitstatus; |
| 8361 | if (evalskip) |
| 8362 | goto skipping; |
| 8363 | } |
| 8364 | loopnest--; |
| 8365 | exitstatus = status; |
| 8366 | } |
| 8367 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8368 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8369 | evalfor(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8370 | { |
| 8371 | struct arglist arglist; |
| 8372 | union node *argp; |
| 8373 | struct strlist *sp; |
| 8374 | struct stackmark smark; |
| 8375 | |
| 8376 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8377 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8378 | arglist.lastp = &arglist.list; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8379 | for (argp = n->nfor.args; argp; argp = argp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8380 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8381 | /* XXX */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8382 | if (evalskip) |
| 8383 | goto out; |
| 8384 | } |
| 8385 | *arglist.lastp = NULL; |
| 8386 | |
| 8387 | exitstatus = 0; |
| 8388 | loopnest++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8389 | flags &= EV_TESTED; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8390 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8391 | setvar(n->nfor.var, sp->text, 0); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8392 | evaltree(n->nfor.body, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8393 | if (evalskip) { |
| 8394 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
| 8395 | evalskip = 0; |
| 8396 | continue; |
| 8397 | } |
| 8398 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8399 | evalskip = 0; |
| 8400 | break; |
| 8401 | } |
| 8402 | } |
| 8403 | loopnest--; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8404 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8405 | popstackmark(&smark); |
| 8406 | } |
| 8407 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8408 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8409 | evalcase(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8410 | { |
| 8411 | union node *cp; |
| 8412 | union node *patp; |
| 8413 | struct arglist arglist; |
| 8414 | struct stackmark smark; |
| 8415 | |
| 8416 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8417 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8418 | arglist.lastp = &arglist.list; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8419 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8420 | exitstatus = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8421 | for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) { |
| 8422 | for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8423 | if (casematch(patp, arglist.list->text)) { |
| 8424 | if (evalskip == 0) { |
| 8425 | evaltree(cp->nclist.body, flags); |
| 8426 | } |
| 8427 | goto out; |
| 8428 | } |
| 8429 | } |
| 8430 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8431 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8432 | popstackmark(&smark); |
| 8433 | } |
| 8434 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8435 | /* |
| 8436 | * Kick off a subshell to evaluate a tree. |
| 8437 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8438 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8439 | evalsubshell(union node *n, int flags) |
| 8440 | { |
| 8441 | struct job *jp; |
| 8442 | int backgnd = (n->type == NBACKGND); |
| 8443 | int status; |
| 8444 | |
| 8445 | expredir(n->nredir.redirect); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8446 | if (!backgnd && (flags & EV_EXIT) && !may_have_traps) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8447 | goto nofork; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8448 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8449 | jp = makejob(/*n,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8450 | if (forkshell(jp, n, backgnd) == 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8451 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8452 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8453 | flags |= EV_EXIT; |
| 8454 | if (backgnd) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8455 | flags &= ~EV_TESTED; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 8456 | nofork: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8457 | redirect(n->nredir.redirect, 0); |
| 8458 | evaltreenr(n->nredir.n, flags); |
| 8459 | /* never returns */ |
| 8460 | } |
| 8461 | status = 0; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8462 | if (!backgnd) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8463 | status = waitforjob(jp); |
| 8464 | exitstatus = status; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8465 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8466 | } |
| 8467 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8468 | /* |
| 8469 | * Compute the names of the files in a redirection list. |
| 8470 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 8471 | static void fixredir(union node *, const char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8472 | static void |
| 8473 | expredir(union node *n) |
| 8474 | { |
| 8475 | union node *redir; |
| 8476 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8477 | for (redir = n; redir; redir = redir->nfile.next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8478 | struct arglist fn; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8479 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8480 | fn.list = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8481 | fn.lastp = &fn.list; |
| 8482 | switch (redir->type) { |
| 8483 | case NFROMTO: |
| 8484 | case NFROM: |
| 8485 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8486 | #if ENABLE_ASH_BASH_COMPAT |
| 8487 | case NTO2: |
| 8488 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8489 | case NCLOBBER: |
| 8490 | case NAPPEND: |
| 8491 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8492 | #if ENABLE_ASH_BASH_COMPAT |
| 8493 | store_expfname: |
| 8494 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8495 | redir->nfile.expfname = fn.list->text; |
| 8496 | break; |
| 8497 | case NFROMFD: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8498 | case NTOFD: /* >& */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8499 | if (redir->ndup.vname) { |
| 8500 | expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8501 | if (fn.list == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8502 | ash_msg_and_raise_error("redir error"); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8503 | #if ENABLE_ASH_BASH_COMPAT |
| 8504 | //FIXME: we used expandarg with different args! |
| 8505 | if (!isdigit_str9(fn.list->text)) { |
| 8506 | /* >&file, not >&fd */ |
| 8507 | if (redir->nfile.fd != 1) /* 123>&file - BAD */ |
| 8508 | ash_msg_and_raise_error("redir error"); |
| 8509 | redir->type = NTO2; |
| 8510 | goto store_expfname; |
| 8511 | } |
| 8512 | #endif |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8513 | fixredir(redir, fn.list->text, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8514 | } |
| 8515 | break; |
| 8516 | } |
| 8517 | } |
| 8518 | } |
| 8519 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8520 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8521 | * Evaluate a pipeline. All the processes in the pipeline are children |
| 8522 | * of the process creating the pipeline. (This differs from some versions |
| 8523 | * of the shell, which make the last process in a pipeline the parent |
| 8524 | * of all the rest.) |
| 8525 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8526 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8527 | evalpipe(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8528 | { |
| 8529 | struct job *jp; |
| 8530 | struct nodelist *lp; |
| 8531 | int pipelen; |
| 8532 | int prevfd; |
| 8533 | int pip[2]; |
| 8534 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8535 | TRACE(("evalpipe(0x%lx) called\n", (long)n)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8536 | pipelen = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8537 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8538 | pipelen++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8539 | flags |= EV_EXIT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8540 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8541 | jp = makejob(/*n,*/ pipelen); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8542 | prevfd = -1; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8543 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8544 | prehash(lp->n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8545 | pip[1] = -1; |
| 8546 | if (lp->next) { |
| 8547 | if (pipe(pip) < 0) { |
| 8548 | close(prevfd); |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 8549 | ash_msg_and_raise_error("pipe call failed"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8550 | } |
| 8551 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8552 | if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8553 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8554 | if (pip[1] >= 0) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8555 | close(pip[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8556 | } |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8557 | if (prevfd > 0) { |
| 8558 | dup2(prevfd, 0); |
| 8559 | close(prevfd); |
| 8560 | } |
| 8561 | if (pip[1] > 1) { |
| 8562 | dup2(pip[1], 1); |
| 8563 | close(pip[1]); |
| 8564 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8565 | evaltreenr(lp->n, flags); |
| 8566 | /* never returns */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8567 | } |
| 8568 | if (prevfd >= 0) |
| 8569 | close(prevfd); |
| 8570 | prevfd = pip[0]; |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 8571 | /* Don't want to trigger debugging */ |
| 8572 | if (pip[1] != -1) |
| 8573 | close(pip[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8574 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8575 | if (n->npipe.pipe_backgnd == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8576 | exitstatus = waitforjob(jp); |
| 8577 | TRACE(("evalpipe: job done exit status %d\n", exitstatus)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8578 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8579 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8580 | } |
| 8581 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8582 | /* |
| 8583 | * Controls whether the shell is interactive or not. |
| 8584 | */ |
| 8585 | static void |
| 8586 | setinteractive(int on) |
| 8587 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8588 | static smallint is_interactive; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8589 | |
| 8590 | if (++on == is_interactive) |
| 8591 | return; |
| 8592 | is_interactive = on; |
| 8593 | setsignal(SIGINT); |
| 8594 | setsignal(SIGQUIT); |
| 8595 | setsignal(SIGTERM); |
| 8596 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 8597 | if (is_interactive > 1) { |
| 8598 | /* Looks like they want an interactive shell */ |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8599 | static smallint did_banner; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8600 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8601 | if (!did_banner) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8602 | /* note: ash and hush share this string */ |
| 8603 | out1fmt("\n\n%s %s\n" |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8604 | "Enter 'help' for a list of built-in commands." |
| 8605 | "\n\n", |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8606 | bb_banner, |
| 8607 | "built-in shell (ash)" |
| 8608 | ); |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8609 | did_banner = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8610 | } |
| 8611 | } |
| 8612 | #endif |
| 8613 | } |
| 8614 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8615 | static void |
| 8616 | optschanged(void) |
| 8617 | { |
| 8618 | #if DEBUG |
| 8619 | opentrace(); |
| 8620 | #endif |
| 8621 | setinteractive(iflag); |
| 8622 | setjobctl(mflag); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8623 | #if ENABLE_FEATURE_EDITING_VI |
| 8624 | if (viflag) |
| 8625 | line_input_state->flags |= VI_MODE; |
| 8626 | else |
| 8627 | line_input_state->flags &= ~VI_MODE; |
| 8628 | #else |
| 8629 | viflag = 0; /* forcibly keep the option off */ |
| 8630 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8631 | } |
| 8632 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8633 | static struct localvar *localvars; |
| 8634 | |
| 8635 | /* |
| 8636 | * Called after a function returns. |
| 8637 | * Interrupts must be off. |
| 8638 | */ |
| 8639 | static void |
| 8640 | poplocalvars(void) |
| 8641 | { |
| 8642 | struct localvar *lvp; |
| 8643 | struct var *vp; |
| 8644 | |
| 8645 | while ((lvp = localvars) != NULL) { |
| 8646 | localvars = lvp->next; |
| 8647 | vp = lvp->vp; |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 8648 | TRACE(("poplocalvar %s\n", vp ? vp->text : "-")); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8649 | if (vp == NULL) { /* $- saved */ |
| 8650 | memcpy(optlist, lvp->text, sizeof(optlist)); |
| 8651 | free((char*)lvp->text); |
| 8652 | optschanged(); |
| 8653 | } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8654 | unsetvar(vp->var_text); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8655 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8656 | if (vp->var_func) |
| 8657 | vp->var_func(var_end(lvp->text)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8658 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8659 | free((char*)vp->var_text); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8660 | vp->flags = lvp->flags; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8661 | vp->var_text = lvp->text; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8662 | } |
| 8663 | free(lvp); |
| 8664 | } |
| 8665 | } |
| 8666 | |
| 8667 | static int |
| 8668 | evalfun(struct funcnode *func, int argc, char **argv, int flags) |
| 8669 | { |
| 8670 | volatile struct shparam saveparam; |
| 8671 | struct localvar *volatile savelocalvars; |
| 8672 | struct jmploc *volatile savehandler; |
| 8673 | struct jmploc jmploc; |
| 8674 | int e; |
| 8675 | |
| 8676 | saveparam = shellparam; |
| 8677 | savelocalvars = localvars; |
| 8678 | e = setjmp(jmploc.loc); |
| 8679 | if (e) { |
| 8680 | goto funcdone; |
| 8681 | } |
| 8682 | INT_OFF; |
| 8683 | savehandler = exception_handler; |
| 8684 | exception_handler = &jmploc; |
| 8685 | localvars = NULL; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8686 | shellparam.malloced = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8687 | func->count++; |
| 8688 | funcnest++; |
| 8689 | INT_ON; |
| 8690 | shellparam.nparam = argc - 1; |
| 8691 | shellparam.p = argv + 1; |
| 8692 | #if ENABLE_ASH_GETOPTS |
| 8693 | shellparam.optind = 1; |
| 8694 | shellparam.optoff = -1; |
| 8695 | #endif |
| 8696 | evaltree(&func->n, flags & EV_TESTED); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8697 | funcdone: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8698 | INT_OFF; |
| 8699 | funcnest--; |
| 8700 | freefunc(func); |
| 8701 | poplocalvars(); |
| 8702 | localvars = savelocalvars; |
| 8703 | freeparam(&shellparam); |
| 8704 | shellparam = saveparam; |
| 8705 | exception_handler = savehandler; |
| 8706 | INT_ON; |
| 8707 | evalskip &= ~SKIPFUNC; |
| 8708 | return e; |
| 8709 | } |
| 8710 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8711 | #if ENABLE_ASH_CMDCMD |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8712 | static char ** |
| 8713 | parse_command_args(char **argv, const char **path) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8714 | { |
| 8715 | char *cp, c; |
| 8716 | |
| 8717 | for (;;) { |
| 8718 | cp = *++argv; |
| 8719 | if (!cp) |
| 8720 | return 0; |
| 8721 | if (*cp++ != '-') |
| 8722 | break; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8723 | c = *cp++; |
| 8724 | if (!c) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8725 | break; |
| 8726 | if (c == '-' && !*cp) { |
| 8727 | argv++; |
| 8728 | break; |
| 8729 | } |
| 8730 | do { |
| 8731 | switch (c) { |
| 8732 | case 'p': |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 8733 | *path = bb_default_path; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8734 | break; |
| 8735 | default: |
| 8736 | /* run 'typecmd' for other options */ |
| 8737 | return 0; |
| 8738 | } |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 8739 | c = *cp++; |
| 8740 | } while (c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8741 | } |
| 8742 | return argv; |
| 8743 | } |
| 8744 | #endif |
| 8745 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8746 | /* |
| 8747 | * Make a variable a local variable. When a variable is made local, it's |
| 8748 | * value and flags are saved in a localvar structure. The saved values |
| 8749 | * will be restored when the shell function returns. We handle the name |
| 8750 | * "-" as a special case. |
| 8751 | */ |
| 8752 | static void |
| 8753 | mklocal(char *name) |
| 8754 | { |
| 8755 | struct localvar *lvp; |
| 8756 | struct var **vpp; |
| 8757 | struct var *vp; |
| 8758 | |
| 8759 | INT_OFF; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 8760 | lvp = ckzalloc(sizeof(struct localvar)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8761 | if (LONE_DASH(name)) { |
| 8762 | char *p; |
| 8763 | p = ckmalloc(sizeof(optlist)); |
| 8764 | lvp->text = memcpy(p, optlist, sizeof(optlist)); |
| 8765 | vp = NULL; |
| 8766 | } else { |
| 8767 | char *eq; |
| 8768 | |
| 8769 | vpp = hashvar(name); |
| 8770 | vp = *findvar(vpp, name); |
| 8771 | eq = strchr(name, '='); |
| 8772 | if (vp == NULL) { |
| 8773 | if (eq) |
| 8774 | setvareq(name, VSTRFIXED); |
| 8775 | else |
| 8776 | setvar(name, NULL, VSTRFIXED); |
| 8777 | vp = *vpp; /* the new variable */ |
| 8778 | lvp->flags = VUNSET; |
| 8779 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8780 | lvp->text = vp->var_text; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8781 | lvp->flags = vp->flags; |
| 8782 | vp->flags |= VSTRFIXED|VTEXTFIXED; |
| 8783 | if (eq) |
| 8784 | setvareq(name, 0); |
| 8785 | } |
| 8786 | } |
| 8787 | lvp->vp = vp; |
| 8788 | lvp->next = localvars; |
| 8789 | localvars = lvp; |
| 8790 | INT_ON; |
| 8791 | } |
| 8792 | |
| 8793 | /* |
| 8794 | * The "local" command. |
| 8795 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8796 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8797 | localcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8798 | { |
| 8799 | char *name; |
| 8800 | |
| 8801 | argv = argptr; |
| 8802 | while ((name = *argv++) != NULL) { |
| 8803 | mklocal(name); |
| 8804 | } |
| 8805 | return 0; |
| 8806 | } |
| 8807 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8808 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8809 | falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8810 | { |
| 8811 | return 1; |
| 8812 | } |
| 8813 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8814 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8815 | truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8816 | { |
| 8817 | return 0; |
| 8818 | } |
| 8819 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8820 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8821 | execcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8822 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8823 | if (argv[1]) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8824 | iflag = 0; /* exit on error */ |
| 8825 | mflag = 0; |
| 8826 | optschanged(); |
| 8827 | shellexec(argv + 1, pathval(), 0); |
| 8828 | } |
| 8829 | return 0; |
| 8830 | } |
| 8831 | |
| 8832 | /* |
| 8833 | * The return command. |
| 8834 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8835 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8836 | returncmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8837 | { |
| 8838 | /* |
| 8839 | * If called outside a function, do what ksh does; |
| 8840 | * skip the rest of the file. |
| 8841 | */ |
| 8842 | evalskip = funcnest ? SKIPFUNC : SKIPFILE; |
| 8843 | return argv[1] ? number(argv[1]) : exitstatus; |
| 8844 | } |
| 8845 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8846 | /* Forward declarations for builtintab[] */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8847 | static int breakcmd(int, char **) FAST_FUNC; |
| 8848 | static int dotcmd(int, char **) FAST_FUNC; |
| 8849 | static int evalcmd(int, char **) FAST_FUNC; |
| 8850 | static int exitcmd(int, char **) FAST_FUNC; |
| 8851 | static int exportcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8852 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8853 | static int getoptscmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8854 | #endif |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 8855 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8856 | static int helpcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 8857 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 8858 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8859 | static int letcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8860 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8861 | static int readcmd(int, char **) FAST_FUNC; |
| 8862 | static int setcmd(int, char **) FAST_FUNC; |
| 8863 | static int shiftcmd(int, char **) FAST_FUNC; |
| 8864 | static int timescmd(int, char **) FAST_FUNC; |
| 8865 | static int trapcmd(int, char **) FAST_FUNC; |
| 8866 | static int umaskcmd(int, char **) FAST_FUNC; |
| 8867 | static int unsetcmd(int, char **) FAST_FUNC; |
| 8868 | static int ulimitcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8869 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8870 | #define BUILTIN_NOSPEC "0" |
| 8871 | #define BUILTIN_SPECIAL "1" |
| 8872 | #define BUILTIN_REGULAR "2" |
| 8873 | #define BUILTIN_SPEC_REG "3" |
| 8874 | #define BUILTIN_ASSIGN "4" |
| 8875 | #define BUILTIN_SPEC_ASSG "5" |
| 8876 | #define BUILTIN_REG_ASSG "6" |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8877 | #define BUILTIN_SPEC_REG_ASSG "7" |
| 8878 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8879 | /* Stubs for calling non-FAST_FUNC's */ |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 8880 | #if ENABLE_ASH_BUILTIN_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8881 | static int FAST_FUNC echocmd(int argc, char **argv) { return echo_main(argc, argv); } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 8882 | #endif |
| 8883 | #if ENABLE_ASH_BUILTIN_PRINTF |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8884 | static int FAST_FUNC printfcmd(int argc, char **argv) { return printf_main(argc, argv); } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 8885 | #endif |
| 8886 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8887 | static int FAST_FUNC testcmd(int argc, char **argv) { return test_main(argc, argv); } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 8888 | #endif |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 8889 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 8890 | /* Keep these in proper order since it is searched via bsearch() */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8891 | static const struct builtincmd builtintab[] = { |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8892 | { BUILTIN_SPEC_REG "." , dotcmd }, |
| 8893 | { BUILTIN_SPEC_REG ":" , truecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8894 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8895 | { BUILTIN_REGULAR "[" , testcmd }, |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8896 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8897 | { BUILTIN_REGULAR "[[" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8898 | #endif |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8899 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8900 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8901 | { BUILTIN_REG_ASSG "alias" , aliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8902 | #endif |
| 8903 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8904 | { BUILTIN_REGULAR "bg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8905 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8906 | { BUILTIN_SPEC_REG "break" , breakcmd }, |
| 8907 | { BUILTIN_REGULAR "cd" , cdcmd }, |
| 8908 | { BUILTIN_NOSPEC "chdir" , cdcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8909 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8910 | { BUILTIN_REGULAR "command" , commandcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8911 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8912 | { BUILTIN_SPEC_REG "continue", breakcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8913 | #if ENABLE_ASH_BUILTIN_ECHO |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8914 | { BUILTIN_REGULAR "echo" , echocmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8915 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8916 | { BUILTIN_SPEC_REG "eval" , evalcmd }, |
| 8917 | { BUILTIN_SPEC_REG "exec" , execcmd }, |
| 8918 | { BUILTIN_SPEC_REG "exit" , exitcmd }, |
| 8919 | { BUILTIN_SPEC_REG_ASSG "export" , exportcmd }, |
| 8920 | { BUILTIN_REGULAR "false" , falsecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8921 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8922 | { BUILTIN_REGULAR "fg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8923 | #endif |
| 8924 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8925 | { BUILTIN_REGULAR "getopts" , getoptscmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8926 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8927 | { BUILTIN_NOSPEC "hash" , hashcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8928 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8929 | { BUILTIN_NOSPEC "help" , helpcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8930 | #endif |
| 8931 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8932 | { BUILTIN_REGULAR "jobs" , jobscmd }, |
| 8933 | { BUILTIN_REGULAR "kill" , killcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8934 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 8935 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8936 | { BUILTIN_NOSPEC "let" , letcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8937 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8938 | { BUILTIN_ASSIGN "local" , localcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8939 | #if ENABLE_ASH_BUILTIN_PRINTF |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8940 | { BUILTIN_REGULAR "printf" , printfcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 8941 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8942 | { BUILTIN_NOSPEC "pwd" , pwdcmd }, |
| 8943 | { BUILTIN_REGULAR "read" , readcmd }, |
| 8944 | { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd }, |
| 8945 | { BUILTIN_SPEC_REG "return" , returncmd }, |
| 8946 | { BUILTIN_SPEC_REG "set" , setcmd }, |
| 8947 | { BUILTIN_SPEC_REG "shift" , shiftcmd }, |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 8948 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8949 | { BUILTIN_SPEC_REG "source" , dotcmd }, |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 8950 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8951 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8952 | { BUILTIN_REGULAR "test" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8953 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8954 | { BUILTIN_SPEC_REG "times" , timescmd }, |
| 8955 | { BUILTIN_SPEC_REG "trap" , trapcmd }, |
| 8956 | { BUILTIN_REGULAR "true" , truecmd }, |
| 8957 | { BUILTIN_NOSPEC "type" , typecmd }, |
| 8958 | { BUILTIN_NOSPEC "ulimit" , ulimitcmd }, |
| 8959 | { BUILTIN_REGULAR "umask" , umaskcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8960 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8961 | { BUILTIN_REGULAR "unalias" , unaliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8962 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8963 | { BUILTIN_SPEC_REG "unset" , unsetcmd }, |
| 8964 | { BUILTIN_REGULAR "wait" , waitcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8965 | }; |
| 8966 | |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8967 | /* Should match the above table! */ |
| 8968 | #define COMMANDCMD (builtintab + \ |
| 8969 | 2 + \ |
| 8970 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 8971 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 8972 | 1 * ENABLE_ASH_ALIAS + \ |
| 8973 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 8974 | 3) |
| 8975 | #define EXECCMD (builtintab + \ |
| 8976 | 2 + \ |
| 8977 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 8978 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 8979 | 1 * ENABLE_ASH_ALIAS + \ |
| 8980 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 8981 | 3 + \ |
| 8982 | 1 * ENABLE_ASH_CMDCMD + \ |
| 8983 | 1 + \ |
| 8984 | ENABLE_ASH_BUILTIN_ECHO + \ |
| 8985 | 1) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8986 | |
| 8987 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8988 | * Search the table of builtin commands. |
| 8989 | */ |
| 8990 | static struct builtincmd * |
| 8991 | find_builtin(const char *name) |
| 8992 | { |
| 8993 | struct builtincmd *bp; |
| 8994 | |
| 8995 | bp = bsearch( |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 8996 | name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8997 | pstrcmp |
| 8998 | ); |
| 8999 | return bp; |
| 9000 | } |
| 9001 | |
| 9002 | /* |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9003 | * Execute a simple command. |
| 9004 | */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9005 | static int |
| 9006 | isassignment(const char *p) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9007 | { |
| 9008 | const char *q = endofname(p); |
| 9009 | if (p == q) |
| 9010 | return 0; |
| 9011 | return *q == '='; |
| 9012 | } |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9013 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9014 | bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9015 | { |
| 9016 | /* Preserve exitstatus of a previous possible redirection |
| 9017 | * as POSIX mandates */ |
| 9018 | return back_exitstatus; |
| 9019 | } |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 9020 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9021 | evalcommand(union node *cmd, int flags) |
| 9022 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9023 | static const struct builtincmd null_bltin = { |
| 9024 | "\0\0", bltincmd /* why three NULs? */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9025 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9026 | struct stackmark smark; |
| 9027 | union node *argp; |
| 9028 | struct arglist arglist; |
| 9029 | struct arglist varlist; |
| 9030 | char **argv; |
| 9031 | int argc; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9032 | const struct strlist *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9033 | struct cmdentry cmdentry; |
| 9034 | struct job *jp; |
| 9035 | char *lastarg; |
| 9036 | const char *path; |
| 9037 | int spclbltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9038 | int status; |
| 9039 | char **nargv; |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9040 | struct builtincmd *bcmd; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9041 | smallint cmd_is_exec; |
| 9042 | smallint pseudovarflag = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9043 | |
| 9044 | /* First expand the arguments. */ |
| 9045 | TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); |
| 9046 | setstackmark(&smark); |
| 9047 | back_exitstatus = 0; |
| 9048 | |
| 9049 | cmdentry.cmdtype = CMDBUILTIN; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9050 | cmdentry.u.cmd = &null_bltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9051 | varlist.lastp = &varlist.list; |
| 9052 | *varlist.lastp = NULL; |
| 9053 | arglist.lastp = &arglist.list; |
| 9054 | *arglist.lastp = NULL; |
| 9055 | |
| 9056 | argc = 0; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9057 | if (cmd->ncmd.args) { |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9058 | bcmd = find_builtin(cmd->ncmd.args->narg.text); |
| 9059 | pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd); |
| 9060 | } |
| 9061 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9062 | for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) { |
| 9063 | struct strlist **spp; |
| 9064 | |
| 9065 | spp = arglist.lastp; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 9066 | if (pseudovarflag && isassignment(argp->narg.text)) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9067 | expandarg(argp, &arglist, EXP_VARTILDE); |
| 9068 | else |
| 9069 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
| 9070 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9071 | for (sp = *spp; sp; sp = sp->next) |
| 9072 | argc++; |
| 9073 | } |
| 9074 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9075 | argv = nargv = stalloc(sizeof(char *) * (argc + 1)); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9076 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9077 | TRACE(("evalcommand arg: %s\n", sp->text)); |
| 9078 | *nargv++ = sp->text; |
| 9079 | } |
| 9080 | *nargv = NULL; |
| 9081 | |
| 9082 | lastarg = NULL; |
| 9083 | if (iflag && funcnest == 0 && argc > 0) |
| 9084 | lastarg = nargv[-1]; |
| 9085 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9086 | preverrout_fd = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9087 | expredir(cmd->ncmd.redirect); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9088 | status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9089 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9090 | path = vpath.var_text; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9091 | for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) { |
| 9092 | struct strlist **spp; |
| 9093 | char *p; |
| 9094 | |
| 9095 | spp = varlist.lastp; |
| 9096 | expandarg(argp, &varlist, EXP_VARTILDE); |
| 9097 | |
| 9098 | /* |
| 9099 | * Modify the command lookup path, if a PATH= assignment |
| 9100 | * is present |
| 9101 | */ |
| 9102 | p = (*spp)->text; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9103 | if (varcmp(p, path) == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9104 | path = p; |
| 9105 | } |
| 9106 | |
| 9107 | /* Print the command if xflag is set. */ |
| 9108 | if (xflag) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9109 | int n; |
| 9110 | const char *p = " %s"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9111 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9112 | p++; |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 9113 | fdprintf(preverrout_fd, p, expandstr(ps4val())); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9114 | |
| 9115 | sp = varlist.list; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9116 | for (n = 0; n < 2; n++) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9117 | while (sp) { |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 9118 | fdprintf(preverrout_fd, p, sp->text); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9119 | sp = sp->next; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9120 | if (*p == '%') { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9121 | p--; |
| 9122 | } |
| 9123 | } |
| 9124 | sp = arglist.list; |
| 9125 | } |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9126 | safe_write(preverrout_fd, "\n", 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9127 | } |
| 9128 | |
| 9129 | cmd_is_exec = 0; |
| 9130 | spclbltin = -1; |
| 9131 | |
| 9132 | /* Now locate the command. */ |
| 9133 | if (argc) { |
| 9134 | const char *oldpath; |
| 9135 | int cmd_flag = DO_ERR; |
| 9136 | |
| 9137 | path += 5; |
| 9138 | oldpath = path; |
| 9139 | for (;;) { |
| 9140 | find_command(argv[0], &cmdentry, cmd_flag, path); |
| 9141 | if (cmdentry.cmdtype == CMDUNKNOWN) { |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 9142 | flush_stdout_stderr(); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9143 | status = 127; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9144 | goto bail; |
| 9145 | } |
| 9146 | |
| 9147 | /* implement bltin and command here */ |
| 9148 | if (cmdentry.cmdtype != CMDBUILTIN) |
| 9149 | break; |
| 9150 | if (spclbltin < 0) |
| 9151 | spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd); |
| 9152 | if (cmdentry.u.cmd == EXECCMD) |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9153 | cmd_is_exec = 1; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9154 | #if ENABLE_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9155 | if (cmdentry.u.cmd == COMMANDCMD) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9156 | path = oldpath; |
| 9157 | nargv = parse_command_args(argv, &path); |
| 9158 | if (!nargv) |
| 9159 | break; |
| 9160 | argc -= nargv - argv; |
| 9161 | argv = nargv; |
| 9162 | cmd_flag |= DO_NOFUNC; |
| 9163 | } else |
| 9164 | #endif |
| 9165 | break; |
| 9166 | } |
| 9167 | } |
| 9168 | |
| 9169 | if (status) { |
| 9170 | /* We have a redirection error. */ |
| 9171 | if (spclbltin > 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9172 | raise_exception(EXERROR); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9173 | bail: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9174 | exitstatus = status; |
| 9175 | goto out; |
| 9176 | } |
| 9177 | |
| 9178 | /* Execute the command. */ |
| 9179 | switch (cmdentry.cmdtype) { |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9180 | default: { |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9181 | |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9182 | #if ENABLE_FEATURE_SH_NOFORK |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9183 | /* (1) BUG: if variables are set, we need to fork, or save/restore them |
| 9184 | * around run_nofork_applet() call. |
| 9185 | * (2) Should this check also be done in forkshell()? |
| 9186 | * (perhaps it should, so that "VAR=VAL nofork" at least avoids exec...) |
| 9187 | */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9188 | /* find_command() encodes applet_no as (-2 - applet_no) */ |
| 9189 | int applet_no = (- cmdentry.u.index - 2); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9190 | if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) { |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9191 | listsetvar(varlist.list, VEXPORT|VSTACK); |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9192 | /* run <applet>_main() */ |
| 9193 | exitstatus = run_nofork_applet(applet_no, argv); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9194 | break; |
| 9195 | } |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9196 | #endif |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9197 | /* Can we avoid forking off? For example, very last command |
| 9198 | * in a script or a subshell does not need forking, |
| 9199 | * we can just exec it. |
| 9200 | */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9201 | if (!(flags & EV_EXIT) || may_have_traps) { |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9202 | /* No, forking off a child is necessary */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9203 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9204 | jp = makejob(/*cmd,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9205 | if (forkshell(jp, cmd, FORK_FG) != 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9206 | /* parent */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9207 | exitstatus = waitforjob(jp); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9208 | INT_ON; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9209 | TRACE(("forked child exited with %d\n", exitstatus)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9210 | break; |
| 9211 | } |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9212 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9213 | FORCE_INT_ON; |
Denys Vlasenko | c7f95d2 | 2010-05-18 15:52:23 +0200 | [diff] [blame] | 9214 | /* fall through to exec'ing external program */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9215 | } |
| 9216 | listsetvar(varlist.list, VEXPORT|VSTACK); |
| 9217 | shellexec(argv, path, cmdentry.u.index); |
| 9218 | /* NOTREACHED */ |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9219 | } /* default */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9220 | case CMDBUILTIN: |
| 9221 | cmdenviron = varlist.list; |
| 9222 | if (cmdenviron) { |
| 9223 | struct strlist *list = cmdenviron; |
| 9224 | int i = VNOSET; |
| 9225 | if (spclbltin > 0 || argc == 0) { |
| 9226 | i = 0; |
| 9227 | if (cmd_is_exec && argc > 1) |
| 9228 | i = VEXPORT; |
| 9229 | } |
| 9230 | listsetvar(list, i); |
| 9231 | } |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9232 | /* Tight loop with builtins only: |
| 9233 | * "while kill -0 $child; do true; done" |
| 9234 | * will never exit even if $child died, unless we do this |
| 9235 | * to reap the zombie and make kill detect that it's gone: */ |
| 9236 | dowait(DOWAIT_NONBLOCK, NULL); |
| 9237 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9238 | if (evalbltin(cmdentry.u.cmd, argc, argv)) { |
| 9239 | int exit_status; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 9240 | int i = exception_type; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9241 | if (i == EXEXIT) |
| 9242 | goto raise; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9243 | exit_status = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9244 | if (i == EXINT) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 9245 | exit_status = 128 + SIGINT; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9246 | if (i == EXSIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9247 | exit_status = 128 + pending_sig; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9248 | exitstatus = exit_status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9249 | if (i == EXINT || spclbltin > 0) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9250 | raise: |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9251 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9252 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9253 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9254 | } |
| 9255 | break; |
| 9256 | |
| 9257 | case CMDFUNCTION: |
| 9258 | listsetvar(varlist.list, 0); |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9259 | /* See above for the rationale */ |
| 9260 | dowait(DOWAIT_NONBLOCK, NULL); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9261 | if (evalfun(cmdentry.u.func, argc, argv, flags)) |
| 9262 | goto raise; |
| 9263 | break; |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9264 | |
| 9265 | } /* switch */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9266 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9267 | out: |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9268 | popredir(/*drop:*/ cmd_is_exec, /*restore:*/ cmd_is_exec); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9269 | if (lastarg) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9270 | /* dsl: I think this is intended to be used to support |
| 9271 | * '_' in 'vi' command mode during line editing... |
| 9272 | * However I implemented that within libedit itself. |
| 9273 | */ |
| 9274 | setvar("_", lastarg, 0); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9275 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9276 | popstackmark(&smark); |
| 9277 | } |
| 9278 | |
| 9279 | static int |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9280 | evalbltin(const struct builtincmd *cmd, int argc, char **argv) |
| 9281 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9282 | char *volatile savecmdname; |
| 9283 | struct jmploc *volatile savehandler; |
| 9284 | struct jmploc jmploc; |
| 9285 | int i; |
| 9286 | |
| 9287 | savecmdname = commandname; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9288 | i = setjmp(jmploc.loc); |
| 9289 | if (i) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9290 | goto cmddone; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9291 | savehandler = exception_handler; |
| 9292 | exception_handler = &jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9293 | commandname = argv[0]; |
| 9294 | argptr = argv + 1; |
| 9295 | optptr = NULL; /* initialize nextopt */ |
| 9296 | exitstatus = (*cmd->builtin)(argc, argv); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9297 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9298 | cmddone: |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9299 | exitstatus |= ferror(stdout); |
Rob Landley | f296f0b | 2006-07-06 01:09:21 +0000 | [diff] [blame] | 9300 | clearerr(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9301 | commandname = savecmdname; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9302 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9303 | |
| 9304 | return i; |
| 9305 | } |
| 9306 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9307 | static int |
| 9308 | goodname(const char *p) |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9309 | { |
| 9310 | return !*endofname(p); |
| 9311 | } |
| 9312 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9313 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9314 | /* |
| 9315 | * Search for a command. This is called before we fork so that the |
| 9316 | * location of the command will be available in the parent as well as |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9317 | * the child. The check for "goodname" is an overly conservative |
| 9318 | * check that the name will not be subject to expansion. |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9319 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9320 | static void |
| 9321 | prehash(union node *n) |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9322 | { |
| 9323 | struct cmdentry entry; |
| 9324 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9325 | if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text)) |
| 9326 | find_command(n->ncmd.args->narg.text, &entry, 0, pathval()); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9327 | } |
| 9328 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9329 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9330 | /* ============ Builtin commands |
| 9331 | * |
| 9332 | * Builtin commands whose functions are closely tied to evaluation |
| 9333 | * are implemented here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9334 | */ |
| 9335 | |
| 9336 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9337 | * Handle break and continue commands. Break, continue, and return are |
| 9338 | * all handled by setting the evalskip flag. The evaluation routines |
| 9339 | * above all check this flag, and if it is set they start skipping |
| 9340 | * commands rather than executing them. The variable skipcount is |
| 9341 | * the number of loops to break/continue, or the number of function |
| 9342 | * levels to return. (The latter is always 1.) It should probably |
| 9343 | * be an error to break out of more loops than exist, but it isn't |
| 9344 | * in the standard shell so we don't make it one here. |
| 9345 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9346 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9347 | breakcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9348 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9349 | int n = argv[1] ? number(argv[1]) : 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9350 | |
Aaron Lehmann | 2aef3a6 | 2001-12-31 06:03:12 +0000 | [diff] [blame] | 9351 | if (n <= 0) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9352 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9353 | if (n > loopnest) |
| 9354 | n = loopnest; |
| 9355 | if (n > 0) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9356 | evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9357 | skipcount = n; |
| 9358 | } |
| 9359 | return 0; |
| 9360 | } |
| 9361 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9362 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9363 | /* ============ input.c |
| 9364 | * |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 9365 | * This implements the input routines used by the parser. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9366 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9367 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9368 | enum { |
| 9369 | INPUT_PUSH_FILE = 1, |
| 9370 | INPUT_NOFILE_OK = 2, |
| 9371 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9372 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9373 | static smallint checkkwd; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9374 | /* values of checkkwd variable */ |
| 9375 | #define CHKALIAS 0x1 |
| 9376 | #define CHKKWD 0x2 |
| 9377 | #define CHKNL 0x4 |
| 9378 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9379 | /* |
| 9380 | * Push a string back onto the input at this current parsefile level. |
| 9381 | * We handle aliases this way. |
| 9382 | */ |
| 9383 | #if !ENABLE_ASH_ALIAS |
| 9384 | #define pushstring(s, ap) pushstring(s) |
| 9385 | #endif |
| 9386 | static void |
| 9387 | pushstring(char *s, struct alias *ap) |
| 9388 | { |
| 9389 | struct strpush *sp; |
| 9390 | int len; |
| 9391 | |
| 9392 | len = strlen(s); |
| 9393 | INT_OFF; |
| 9394 | if (g_parsefile->strpush) { |
| 9395 | sp = ckzalloc(sizeof(*sp)); |
| 9396 | sp->prev = g_parsefile->strpush; |
| 9397 | } else { |
| 9398 | sp = &(g_parsefile->basestrpush); |
| 9399 | } |
| 9400 | g_parsefile->strpush = sp; |
| 9401 | sp->prev_string = g_parsefile->next_to_pgetc; |
| 9402 | sp->prev_left_in_line = g_parsefile->left_in_line; |
| 9403 | #if ENABLE_ASH_ALIAS |
| 9404 | sp->ap = ap; |
| 9405 | if (ap) { |
| 9406 | ap->flag |= ALIASINUSE; |
| 9407 | sp->string = s; |
| 9408 | } |
| 9409 | #endif |
| 9410 | g_parsefile->next_to_pgetc = s; |
| 9411 | g_parsefile->left_in_line = len; |
| 9412 | INT_ON; |
| 9413 | } |
| 9414 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9415 | static void |
| 9416 | popstring(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9417 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9418 | struct strpush *sp = g_parsefile->strpush; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9419 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9420 | INT_OFF; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9421 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9422 | if (sp->ap) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9423 | if (g_parsefile->next_to_pgetc[-1] == ' ' |
| 9424 | || g_parsefile->next_to_pgetc[-1] == '\t' |
| 9425 | ) { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9426 | checkkwd |= CHKALIAS; |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9427 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9428 | if (sp->string != sp->ap->val) { |
| 9429 | free(sp->string); |
| 9430 | } |
| 9431 | sp->ap->flag &= ~ALIASINUSE; |
| 9432 | if (sp->ap->flag & ALIASDEAD) { |
| 9433 | unalias(sp->ap->name); |
| 9434 | } |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9435 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9436 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9437 | g_parsefile->next_to_pgetc = sp->prev_string; |
| 9438 | g_parsefile->left_in_line = sp->prev_left_in_line; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9439 | g_parsefile->strpush = sp->prev; |
| 9440 | if (sp != &(g_parsefile->basestrpush)) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9441 | free(sp); |
| 9442 | INT_ON; |
| 9443 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9444 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9445 | //FIXME: BASH_COMPAT with "...&" does TWO pungetc(): |
| 9446 | //it peeks whether it is &>, and then pushes back both chars. |
| 9447 | //This function needs to save last *next_to_pgetc to buf[0] |
| 9448 | //to make two pungetc() reliable. Currently, |
| 9449 | // pgetc (out of buf: does preadfd), pgetc, pungetc, pungetc won't work... |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9450 | static int |
| 9451 | preadfd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9452 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9453 | int nr; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 9454 | char *buf = g_parsefile->buf; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9455 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9456 | g_parsefile->next_to_pgetc = buf; |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9457 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 9458 | retry: |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9459 | if (!iflag || g_parsefile->pf_fd != STDIN_FILENO) |
| 9460 | nr = nonblock_safe_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9461 | else { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9462 | #if ENABLE_FEATURE_TAB_COMPLETION |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9463 | line_input_state->path_lookup = pathval(); |
Eric Andersen | 4a79c0e | 2004-09-08 10:01:07 +0000 | [diff] [blame] | 9464 | #endif |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 9465 | nr = read_line_input(cmdedit_prompt, buf, IBUFSIZ, line_input_state); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9466 | if (nr == 0) { |
| 9467 | /* Ctrl+C pressed */ |
| 9468 | if (trap[SIGINT]) { |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9469 | buf[0] = '\n'; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9470 | buf[1] = '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9471 | raise(SIGINT); |
| 9472 | return 1; |
| 9473 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9474 | goto retry; |
| 9475 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9476 | if (nr < 0 && errno == 0) { |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9477 | /* Ctrl+D pressed */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9478 | nr = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9479 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9480 | } |
| 9481 | #else |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 9482 | nr = nonblock_safe_read(g_parsefile->fd, buf, IBUFSIZ - 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9483 | #endif |
| 9484 | |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9485 | #if 0 |
| 9486 | /* nonblock_safe_read() handles this problem */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9487 | if (nr < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9488 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 9489 | int flags = fcntl(0, F_GETFL); |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9490 | if (flags >= 0 && (flags & O_NONBLOCK)) { |
| 9491 | flags &= ~O_NONBLOCK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9492 | if (fcntl(0, F_SETFL, flags) >= 0) { |
| 9493 | out2str("sh: turning off NDELAY mode\n"); |
| 9494 | goto retry; |
| 9495 | } |
| 9496 | } |
| 9497 | } |
| 9498 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9499 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9500 | return nr; |
| 9501 | } |
| 9502 | |
| 9503 | /* |
| 9504 | * Refill the input buffer and return the next input character: |
| 9505 | * |
| 9506 | * 1) If a string was pushed back on the input, pop it; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9507 | * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM) |
| 9508 | * or we are reading from a string so we can't refill the buffer, |
| 9509 | * return EOF. |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 9510 | * 3) If there is more stuff in this buffer, use it else call read to fill it. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9511 | * 4) Process input up to the next newline, deleting nul characters. |
| 9512 | */ |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9513 | //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__) |
| 9514 | #define pgetc_debug(...) ((void)0) |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9515 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9516 | preadbuffer(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9517 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9518 | char *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9519 | int more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9520 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9521 | while (g_parsefile->strpush) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9522 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9523 | if (g_parsefile->left_in_line == -1 |
| 9524 | && g_parsefile->strpush->ap |
| 9525 | && g_parsefile->next_to_pgetc[-1] != ' ' |
| 9526 | && g_parsefile->next_to_pgetc[-1] != '\t' |
Denis Vlasenko | 1689840 | 2008-11-25 01:34:52 +0000 | [diff] [blame] | 9527 | ) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9528 | pgetc_debug("preadbuffer PEOA"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9529 | return PEOA; |
| 9530 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9531 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9532 | popstring(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9533 | /* try "pgetc" now: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9534 | pgetc_debug("preadbuffer internal pgetc at %d:%p'%s'", |
| 9535 | g_parsefile->left_in_line, |
| 9536 | g_parsefile->next_to_pgetc, |
| 9537 | g_parsefile->next_to_pgetc); |
| 9538 | if (--g_parsefile->left_in_line >= 0) |
| 9539 | return (unsigned char)(*g_parsefile->next_to_pgetc++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9540 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9541 | /* on both branches above g_parsefile->left_in_line < 0. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9542 | * "pgetc" needs refilling. |
| 9543 | */ |
| 9544 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9545 | /* -90 is our -BIGNUM. Below we use -99 to mark "EOF on read", |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9546 | * pungetc() may increment it a few times. |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9547 | * Assuming it won't increment it to less than -90. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9548 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9549 | if (g_parsefile->left_in_line < -90 || g_parsefile->buf == NULL) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9550 | pgetc_debug("preadbuffer PEOF1"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9551 | /* even in failure keep left_in_line and next_to_pgetc |
| 9552 | * in lock step, for correct multi-layer pungetc. |
| 9553 | * left_in_line was decremented before preadbuffer(), |
| 9554 | * must inc next_to_pgetc: */ |
| 9555 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9556 | return PEOF; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9557 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9558 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9559 | more = g_parsefile->left_in_buffer; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9560 | if (more <= 0) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9561 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9562 | again: |
| 9563 | more = preadfd(); |
| 9564 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9565 | /* don't try reading again */ |
| 9566 | g_parsefile->left_in_line = -99; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9567 | pgetc_debug("preadbuffer PEOF2"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9568 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9569 | return PEOF; |
| 9570 | } |
| 9571 | } |
| 9572 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9573 | /* Find out where's the end of line. |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9574 | * Set g_parsefile->left_in_line |
| 9575 | * and g_parsefile->left_in_buffer acordingly. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9576 | * NUL chars are deleted. |
| 9577 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9578 | q = g_parsefile->next_to_pgetc; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9579 | for (;;) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9580 | char c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9581 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9582 | more--; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9583 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9584 | c = *q; |
| 9585 | if (c == '\0') { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9586 | memmove(q, q + 1, more); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9587 | } else { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9588 | q++; |
| 9589 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9590 | g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9591 | break; |
| 9592 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9593 | } |
| 9594 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9595 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9596 | g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1; |
| 9597 | if (g_parsefile->left_in_line < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9598 | goto again; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9599 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9600 | } |
| 9601 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9602 | g_parsefile->left_in_buffer = more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9603 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9604 | if (vflag) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9605 | char save = *q; |
| 9606 | *q = '\0'; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9607 | out2str(g_parsefile->next_to_pgetc); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9608 | *q = save; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9609 | } |
| 9610 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9611 | pgetc_debug("preadbuffer at %d:%p'%s'", |
| 9612 | g_parsefile->left_in_line, |
| 9613 | g_parsefile->next_to_pgetc, |
| 9614 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 9615 | return (unsigned char)*g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9616 | } |
| 9617 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9618 | #define pgetc_as_macro() \ |
| 9619 | (--g_parsefile->left_in_line >= 0 \ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 9620 | ? (unsigned char)*g_parsefile->next_to_pgetc++ \ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9621 | : preadbuffer() \ |
| 9622 | ) |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9623 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9624 | static int |
| 9625 | pgetc(void) |
| 9626 | { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9627 | pgetc_debug("pgetc_fast at %d:%p'%s'", |
| 9628 | g_parsefile->left_in_line, |
| 9629 | g_parsefile->next_to_pgetc, |
| 9630 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9631 | return pgetc_as_macro(); |
| 9632 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9633 | |
| 9634 | #if ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9635 | # define pgetc_fast() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9636 | #else |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9637 | # define pgetc_fast() pgetc_as_macro() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9638 | #endif |
| 9639 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9640 | #if ENABLE_ASH_ALIAS |
| 9641 | static int |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9642 | pgetc_without_PEOA(void) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9643 | { |
| 9644 | int c; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9645 | do { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9646 | pgetc_debug("pgetc_fast at %d:%p'%s'", |
| 9647 | g_parsefile->left_in_line, |
| 9648 | g_parsefile->next_to_pgetc, |
| 9649 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 9650 | c = pgetc_fast(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9651 | } while (c == PEOA); |
| 9652 | return c; |
| 9653 | } |
| 9654 | #else |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9655 | # define pgetc_without_PEOA() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9656 | #endif |
| 9657 | |
| 9658 | /* |
| 9659 | * Read a line from the script. |
| 9660 | */ |
| 9661 | static char * |
| 9662 | pfgets(char *line, int len) |
| 9663 | { |
| 9664 | char *p = line; |
| 9665 | int nleft = len; |
| 9666 | int c; |
| 9667 | |
| 9668 | while (--nleft > 0) { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9669 | c = pgetc_without_PEOA(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9670 | if (c == PEOF) { |
| 9671 | if (p == line) |
| 9672 | return NULL; |
| 9673 | break; |
| 9674 | } |
| 9675 | *p++ = c; |
| 9676 | if (c == '\n') |
| 9677 | break; |
| 9678 | } |
| 9679 | *p = '\0'; |
| 9680 | return line; |
| 9681 | } |
| 9682 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9683 | /* |
| 9684 | * Undo the last call to pgetc. Only one character may be pushed back. |
| 9685 | * PEOF may be pushed back. |
| 9686 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9687 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9688 | pungetc(void) |
| 9689 | { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9690 | g_parsefile->left_in_line++; |
| 9691 | g_parsefile->next_to_pgetc--; |
| 9692 | pgetc_debug("pushed back to %d:%p'%s'", |
| 9693 | g_parsefile->left_in_line, |
| 9694 | g_parsefile->next_to_pgetc, |
| 9695 | g_parsefile->next_to_pgetc); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9696 | } |
| 9697 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9698 | /* |
| 9699 | * To handle the "." command, a stack of input files is used. Pushfile |
| 9700 | * adds a new entry to the stack and popfile restores the previous level. |
| 9701 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9702 | static void |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9703 | pushfile(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9704 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9705 | struct parsefile *pf; |
| 9706 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9707 | pf = ckzalloc(sizeof(*pf)); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9708 | pf->prev = g_parsefile; |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9709 | pf->pf_fd = -1; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9710 | /*pf->strpush = NULL; - ckzalloc did it */ |
| 9711 | /*pf->basestrpush.prev = NULL;*/ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9712 | g_parsefile = pf; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9713 | } |
| 9714 | |
| 9715 | static void |
| 9716 | popfile(void) |
| 9717 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9718 | struct parsefile *pf = g_parsefile; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9719 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9720 | INT_OFF; |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9721 | if (pf->pf_fd >= 0) |
| 9722 | close(pf->pf_fd); |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 9723 | free(pf->buf); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9724 | while (pf->strpush) |
| 9725 | popstring(); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9726 | g_parsefile = pf->prev; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9727 | free(pf); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9728 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9729 | } |
| 9730 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9731 | /* |
| 9732 | * Return to top level. |
| 9733 | */ |
| 9734 | static void |
| 9735 | popallfiles(void) |
| 9736 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9737 | while (g_parsefile != &basepf) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9738 | popfile(); |
| 9739 | } |
| 9740 | |
| 9741 | /* |
| 9742 | * Close the file(s) that the shell is reading commands from. Called |
| 9743 | * after a fork is done. |
| 9744 | */ |
| 9745 | static void |
| 9746 | closescript(void) |
| 9747 | { |
| 9748 | popallfiles(); |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9749 | if (g_parsefile->pf_fd > 0) { |
| 9750 | close(g_parsefile->pf_fd); |
| 9751 | g_parsefile->pf_fd = 0; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9752 | } |
| 9753 | } |
| 9754 | |
| 9755 | /* |
| 9756 | * Like setinputfile, but takes an open file descriptor. Call this with |
| 9757 | * interrupts off. |
| 9758 | */ |
| 9759 | static void |
| 9760 | setinputfd(int fd, int push) |
| 9761 | { |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 9762 | close_on_exec_on(fd); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9763 | if (push) { |
| 9764 | pushfile(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9765 | g_parsefile->buf = NULL; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9766 | } |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9767 | g_parsefile->pf_fd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9768 | if (g_parsefile->buf == NULL) |
| 9769 | g_parsefile->buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9770 | g_parsefile->left_in_buffer = 0; |
| 9771 | g_parsefile->left_in_line = 0; |
| 9772 | g_parsefile->linno = 1; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9773 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9774 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9775 | /* |
| 9776 | * Set the input to take input from a file. If push is set, push the |
| 9777 | * old input onto the stack first. |
| 9778 | */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9779 | static int |
| 9780 | setinputfile(const char *fname, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9781 | { |
| 9782 | int fd; |
| 9783 | int fd2; |
| 9784 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9785 | INT_OFF; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9786 | fd = open(fname, O_RDONLY); |
| 9787 | if (fd < 0) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9788 | if (flags & INPUT_NOFILE_OK) |
| 9789 | goto out; |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 9790 | ash_msg_and_raise_error("can't open '%s'", fname); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9791 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9792 | if (fd < 10) { |
| 9793 | fd2 = copyfd(fd, 10); |
| 9794 | close(fd); |
| 9795 | if (fd2 < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 9796 | ash_msg_and_raise_error("out of file descriptors"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9797 | fd = fd2; |
| 9798 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9799 | setinputfd(fd, flags & INPUT_PUSH_FILE); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9800 | out: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9801 | INT_ON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9802 | return fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9803 | } |
| 9804 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9805 | /* |
| 9806 | * Like setinputfile, but takes input from a string. |
| 9807 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9808 | static void |
| 9809 | setinputstring(char *string) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9810 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9811 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9812 | pushfile(); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9813 | g_parsefile->next_to_pgetc = string; |
| 9814 | g_parsefile->left_in_line = strlen(string); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9815 | g_parsefile->buf = NULL; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9816 | g_parsefile->linno = 1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9817 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9818 | } |
| 9819 | |
| 9820 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9821 | /* ============ mail.c |
| 9822 | * |
| 9823 | * Routines to check for mail. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9824 | */ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9825 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9826 | #if ENABLE_ASH_MAIL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9827 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9828 | #define MAXMBOXES 10 |
| 9829 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9830 | /* times of mailboxes */ |
| 9831 | static time_t mailtime[MAXMBOXES]; |
| 9832 | /* Set if MAIL or MAILPATH is changed. */ |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9833 | static smallint mail_var_path_changed; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9834 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9835 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9836 | * Print appropriate message(s) if mail has arrived. |
| 9837 | * If mail_var_path_changed is set, |
| 9838 | * then the value of MAIL has mail_var_path_changed, |
| 9839 | * so we just update the values. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9840 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9841 | static void |
| 9842 | chkmail(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9843 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9844 | const char *mpath; |
| 9845 | char *p; |
| 9846 | char *q; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9847 | time_t *mtp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9848 | struct stackmark smark; |
| 9849 | struct stat statb; |
| 9850 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9851 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9852 | mpath = mpathset() ? mpathval() : mailval(); |
| 9853 | for (mtp = mailtime; mtp < mailtime + MAXMBOXES; mtp++) { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 9854 | p = path_advance(&mpath, nullstr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9855 | if (p == NULL) |
| 9856 | break; |
| 9857 | if (*p == '\0') |
| 9858 | continue; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9859 | for (q = p; *q; q++) |
| 9860 | continue; |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 9861 | #if DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9862 | if (q[-1] != '/') |
| 9863 | abort(); |
| 9864 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9865 | q[-1] = '\0'; /* delete trailing '/' */ |
| 9866 | if (stat(p, &statb) < 0) { |
| 9867 | *mtp = 0; |
| 9868 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9869 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9870 | if (!mail_var_path_changed && statb.st_mtime != *mtp) { |
| 9871 | fprintf( |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 9872 | stderr, "%s\n", |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9873 | pathopt ? pathopt : "you have mail" |
| 9874 | ); |
| 9875 | } |
| 9876 | *mtp = statb.st_mtime; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9877 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9878 | mail_var_path_changed = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9879 | popstackmark(&smark); |
| 9880 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9881 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9882 | static void FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9883 | changemail(const char *val UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9884 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9885 | mail_var_path_changed = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9886 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9887 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9888 | #endif /* ASH_MAIL */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9889 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9890 | |
| 9891 | /* ============ ??? */ |
| 9892 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9893 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9894 | * Set the shell parameters. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9895 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9896 | static void |
| 9897 | setparam(char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9898 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9899 | char **newparam; |
| 9900 | char **ap; |
| 9901 | int nparam; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9902 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9903 | for (nparam = 0; argv[nparam]; nparam++) |
| 9904 | continue; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9905 | ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap)); |
| 9906 | while (*argv) { |
| 9907 | *ap++ = ckstrdup(*argv++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9908 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9909 | *ap = NULL; |
| 9910 | freeparam(&shellparam); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 9911 | shellparam.malloced = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9912 | shellparam.nparam = nparam; |
| 9913 | shellparam.p = newparam; |
| 9914 | #if ENABLE_ASH_GETOPTS |
| 9915 | shellparam.optind = 1; |
| 9916 | shellparam.optoff = -1; |
| 9917 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9918 | } |
| 9919 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9920 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9921 | * Process shell options. The global variable argptr contains a pointer |
| 9922 | * to the argument list; we advance it past the options. |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 9923 | * |
| 9924 | * SUSv3 section 2.8.1 "Consequences of Shell Errors" says: |
| 9925 | * For a non-interactive shell, an error condition encountered |
| 9926 | * by a special built-in ... shall cause the shell to write a diagnostic message |
| 9927 | * to standard error and exit as shown in the following table: |
Denis Vlasenko | 5624473 | 2008-02-17 15:14:04 +0000 | [diff] [blame] | 9928 | * Error Special Built-In |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 9929 | * ... |
| 9930 | * Utility syntax error (option or operand error) Shall exit |
| 9931 | * ... |
| 9932 | * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142) |
| 9933 | * we see that bash does not do that (set "finishes" with error code 1 instead, |
| 9934 | * and shell continues), and people rely on this behavior! |
| 9935 | * Testcase: |
| 9936 | * set -o barfoo 2>/dev/null |
| 9937 | * echo $? |
| 9938 | * |
| 9939 | * Oh well. Let's mimic that. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9940 | */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9941 | static int |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9942 | plus_minus_o(char *name, int val) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9943 | { |
| 9944 | int i; |
| 9945 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9946 | if (name) { |
| 9947 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 9948 | if (strcmp(name, optnames(i)) == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9949 | optlist[i] = val; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9950 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9951 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 9952 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9953 | ash_msg("illegal option %co %s", val ? '-' : '+', name); |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9954 | return 1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9955 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 9956 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9957 | if (val) { |
| 9958 | out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off"); |
| 9959 | } else { |
| 9960 | out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i)); |
| 9961 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 9962 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9963 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9964 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9965 | static void |
| 9966 | setoption(int flag, int val) |
| 9967 | { |
| 9968 | int i; |
| 9969 | |
| 9970 | for (i = 0; i < NOPTS; i++) { |
| 9971 | if (optletters(i) == flag) { |
| 9972 | optlist[i] = val; |
| 9973 | return; |
| 9974 | } |
| 9975 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 9976 | ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9977 | /* NOTREACHED */ |
| 9978 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 9979 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9980 | options(int cmdline) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9981 | { |
| 9982 | char *p; |
| 9983 | int val; |
| 9984 | int c; |
| 9985 | |
| 9986 | if (cmdline) |
| 9987 | minusc = NULL; |
| 9988 | while ((p = *argptr) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9989 | c = *p++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 9990 | if (c != '-' && c != '+') |
| 9991 | break; |
| 9992 | argptr++; |
| 9993 | val = 0; /* val = 0 if c == '+' */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9994 | if (c == '-') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9995 | val = 1; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 9996 | if (p[0] == '\0' || LONE_DASH(p)) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9997 | if (!cmdline) { |
| 9998 | /* "-" means turn off -x and -v */ |
| 9999 | if (p[0] == '\0') |
| 10000 | xflag = vflag = 0; |
| 10001 | /* "--" means reset params */ |
| 10002 | else if (*argptr == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10003 | setparam(argptr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10004 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10005 | break; /* "-" or "--" terminates options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10006 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10007 | } |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10008 | /* first char was + or - */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10009 | while ((c = *p++) != '\0') { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10010 | /* bash 3.2 indeed handles -c CMD and +c CMD the same */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10011 | if (c == 'c' && cmdline) { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10012 | minusc = p; /* command is after shell args */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10013 | } else if (c == 'o') { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10014 | if (plus_minus_o(*argptr, val)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10015 | /* it already printed err message */ |
| 10016 | return 1; /* error */ |
| 10017 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10018 | if (*argptr) |
| 10019 | argptr++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10020 | } else if (cmdline && (c == 'l')) { /* -l or +l == --login */ |
| 10021 | isloginsh = 1; |
| 10022 | /* bash does not accept +-login, we also won't */ |
| 10023 | } else if (cmdline && val && (c == '-')) { /* long options */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10024 | if (strcmp(p, "login") == 0) |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 10025 | isloginsh = 1; |
| 10026 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10027 | } else { |
| 10028 | setoption(c, val); |
| 10029 | } |
| 10030 | } |
| 10031 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10032 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10033 | } |
| 10034 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10035 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10036 | * The shift builtin command. |
| 10037 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10038 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10039 | shiftcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10040 | { |
| 10041 | int n; |
| 10042 | char **ap1, **ap2; |
| 10043 | |
| 10044 | n = 1; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10045 | if (argv[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10046 | n = number(argv[1]); |
| 10047 | if (n > shellparam.nparam) |
Denis Vlasenko | c90e1be | 2008-07-30 15:35:05 +0000 | [diff] [blame] | 10048 | n = 0; /* bash compat, was = shellparam.nparam; */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10049 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10050 | shellparam.nparam -= n; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10051 | for (ap1 = shellparam.p; --n >= 0; ap1++) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 10052 | if (shellparam.malloced) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10053 | free(*ap1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10054 | } |
| 10055 | ap2 = shellparam.p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 10056 | while ((*ap2++ = *ap1++) != NULL) |
| 10057 | continue; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10058 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10059 | shellparam.optind = 1; |
| 10060 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10061 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10062 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10063 | return 0; |
| 10064 | } |
| 10065 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10066 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10067 | * POSIX requires that 'set' (but not export or readonly) output the |
| 10068 | * variables in lexicographic order - by the locale's collating order (sigh). |
| 10069 | * Maybe we could keep them in an ordered balanced binary tree |
| 10070 | * instead of hashed lists. |
| 10071 | * For now just roll 'em through qsort for printing... |
| 10072 | */ |
| 10073 | static int |
| 10074 | showvars(const char *sep_prefix, int on, int off) |
| 10075 | { |
| 10076 | const char *sep; |
| 10077 | char **ep, **epend; |
| 10078 | |
| 10079 | ep = listvars(on, off, &epend); |
| 10080 | qsort(ep, epend - ep, sizeof(char *), vpcmp); |
| 10081 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 10082 | sep = *sep_prefix ? " " : sep_prefix; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10083 | |
| 10084 | for (; ep < epend; ep++) { |
| 10085 | const char *p; |
| 10086 | const char *q; |
| 10087 | |
| 10088 | p = strchrnul(*ep, '='); |
| 10089 | q = nullstr; |
| 10090 | if (*p) |
| 10091 | q = single_quote(++p); |
| 10092 | out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q); |
| 10093 | } |
| 10094 | return 0; |
| 10095 | } |
| 10096 | |
| 10097 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10098 | * The set command builtin. |
| 10099 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10100 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10101 | setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10102 | { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10103 | int retval; |
| 10104 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10105 | if (!argv[1]) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10106 | return showvars(nullstr, 0, VUNSET); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10107 | INT_OFF; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10108 | retval = 1; |
| 10109 | if (!options(0)) { /* if no parse error... */ |
| 10110 | retval = 0; |
| 10111 | optschanged(); |
| 10112 | if (*argptr != NULL) { |
| 10113 | setparam(argptr); |
| 10114 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10115 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10116 | INT_ON; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10117 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10118 | } |
| 10119 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10120 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10121 | static void FAST_FUNC |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 10122 | change_random(const char *value) |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10123 | { |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10124 | uint32_t t; |
| 10125 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10126 | if (value == NULL) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10127 | /* "get", generate */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10128 | t = next_random(&random_gen); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10129 | /* set without recursion */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 10130 | setvar(vrandom.var_text, utoa(t), VNOFUNC); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10131 | vrandom.flags &= ~VNOFUNC; |
| 10132 | } else { |
| 10133 | /* set/reset */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10134 | t = strtoul(value, NULL, 10); |
| 10135 | INIT_RANDOM_T(&random_gen, (t ? t : 1), t); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10136 | } |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10137 | } |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10138 | #endif |
| 10139 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10140 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10141 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10142 | getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10143 | { |
| 10144 | char *p, *q; |
| 10145 | char c = '?'; |
| 10146 | int done = 0; |
| 10147 | int err = 0; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10148 | char s[12]; |
| 10149 | char **optnext; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10150 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10151 | if (*param_optind < 1) |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10152 | return 1; |
| 10153 | optnext = optfirst + *param_optind - 1; |
| 10154 | |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10155 | if (*param_optind <= 1 || *optoff < 0 || (int)strlen(optnext[-1]) < *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10156 | p = NULL; |
| 10157 | else |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10158 | p = optnext[-1] + *optoff; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10159 | if (p == NULL || *p == '\0') { |
| 10160 | /* Current word is done, advance */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10161 | p = *optnext; |
| 10162 | if (p == NULL || *p != '-' || *++p == '\0') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10163 | atend: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10164 | p = NULL; |
| 10165 | done = 1; |
| 10166 | goto out; |
| 10167 | } |
| 10168 | optnext++; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 10169 | if (LONE_DASH(p)) /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10170 | goto atend; |
| 10171 | } |
| 10172 | |
| 10173 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 10174 | for (q = optstr; *q != c;) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10175 | if (*q == '\0') { |
| 10176 | if (optstr[0] == ':') { |
| 10177 | s[0] = c; |
| 10178 | s[1] = '\0'; |
| 10179 | err |= setvarsafe("OPTARG", s, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10180 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10181 | fprintf(stderr, "Illegal option -%c\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10182 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10183 | } |
| 10184 | c = '?'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10185 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10186 | } |
| 10187 | if (*++q == ':') |
| 10188 | q++; |
| 10189 | } |
| 10190 | |
| 10191 | if (*++q == ':') { |
| 10192 | if (*p == '\0' && (p = *optnext) == NULL) { |
| 10193 | if (optstr[0] == ':') { |
| 10194 | s[0] = c; |
| 10195 | s[1] = '\0'; |
| 10196 | err |= setvarsafe("OPTARG", s, 0); |
| 10197 | c = ':'; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10198 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10199 | fprintf(stderr, "No arg for -%c option\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10200 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10201 | c = '?'; |
| 10202 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10203 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10204 | } |
| 10205 | |
| 10206 | if (p == *optnext) |
| 10207 | optnext++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10208 | err |= setvarsafe("OPTARG", p, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10209 | p = NULL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10210 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10211 | err |= setvarsafe("OPTARG", nullstr, 0); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10212 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10213 | *optoff = p ? p - *(optnext - 1) : -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10214 | *param_optind = optnext - optfirst + 1; |
| 10215 | fmtstr(s, sizeof(s), "%d", *param_optind); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10216 | err |= setvarsafe("OPTIND", s, VNOFUNC); |
| 10217 | s[0] = c; |
| 10218 | s[1] = '\0'; |
| 10219 | err |= setvarsafe(optvar, s, 0); |
| 10220 | if (err) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10221 | *param_optind = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10222 | *optoff = -1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10223 | flush_stdout_stderr(); |
| 10224 | raise_exception(EXERROR); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10225 | } |
| 10226 | return done; |
| 10227 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10228 | |
| 10229 | /* |
| 10230 | * The getopts builtin. Shellparam.optnext points to the next argument |
| 10231 | * to be processed. Shellparam.optptr points to the next character to |
| 10232 | * be processed in the current argument. If shellparam.optnext is NULL, |
| 10233 | * then it's the first time getopts has been called. |
| 10234 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10235 | static int FAST_FUNC |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10236 | getoptscmd(int argc, char **argv) |
| 10237 | { |
| 10238 | char **optbase; |
| 10239 | |
| 10240 | if (argc < 3) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 10241 | ash_msg_and_raise_error("usage: getopts optstring var [arg]"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10242 | if (argc == 3) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10243 | optbase = shellparam.p; |
| 10244 | if (shellparam.optind > shellparam.nparam + 1) { |
| 10245 | shellparam.optind = 1; |
| 10246 | shellparam.optoff = -1; |
| 10247 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10248 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10249 | optbase = &argv[3]; |
| 10250 | if (shellparam.optind > argc - 2) { |
| 10251 | shellparam.optind = 1; |
| 10252 | shellparam.optoff = -1; |
| 10253 | } |
| 10254 | } |
| 10255 | |
| 10256 | return getopts(argv[1], argv[2], optbase, &shellparam.optind, |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10257 | &shellparam.optoff); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10258 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10259 | #endif /* ASH_GETOPTS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10260 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10261 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10262 | /* ============ Shell parser */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10263 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10264 | struct heredoc { |
| 10265 | struct heredoc *next; /* next here document in list */ |
| 10266 | union node *here; /* redirection node */ |
| 10267 | char *eofmark; /* string indicating end of input */ |
| 10268 | smallint striptabs; /* if set, strip leading tabs */ |
| 10269 | }; |
| 10270 | |
| 10271 | static smallint tokpushback; /* last token pushed back */ |
| 10272 | static smallint parsebackquote; /* nonzero if we are inside backquotes */ |
| 10273 | static smallint quoteflag; /* set if (part of) last token was quoted */ |
| 10274 | static token_id_t lasttoken; /* last token read (integer id Txxx) */ |
| 10275 | static struct heredoc *heredoclist; /* list of here documents to read */ |
| 10276 | static char *wordtext; /* text of last word returned by readtoken */ |
| 10277 | static struct nodelist *backquotelist; |
| 10278 | static union node *redirnode; |
| 10279 | static struct heredoc *heredoc; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10280 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10281 | static const char * |
| 10282 | tokname(char *buf, int tok) |
| 10283 | { |
| 10284 | if (tok < TSEMI) |
| 10285 | return tokname_array[tok] + 1; |
| 10286 | sprintf(buf, "\"%s\"", tokname_array[tok] + 1); |
| 10287 | return buf; |
| 10288 | } |
| 10289 | |
| 10290 | /* raise_error_unexpected_syntax: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10291 | * Called when an unexpected token is read during the parse. The argument |
| 10292 | * is the token that is expected, or -1 if more than one type of token can |
| 10293 | * occur at this point. |
| 10294 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10295 | static void raise_error_unexpected_syntax(int) NORETURN; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10296 | static void |
| 10297 | raise_error_unexpected_syntax(int token) |
| 10298 | { |
| 10299 | char msg[64]; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10300 | char buf[16]; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10301 | int l; |
| 10302 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10303 | l = sprintf(msg, "unexpected %s", tokname(buf, lasttoken)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10304 | if (token >= 0) |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10305 | sprintf(msg + l, " (expecting %s)", tokname(buf, token)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10306 | raise_error_syntax(msg); |
| 10307 | /* NOTREACHED */ |
| 10308 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10309 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10310 | #define EOFMARKLEN 79 |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10311 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10312 | /* parsing is heavily cross-recursive, need these forward decls */ |
| 10313 | static union node *andor(void); |
| 10314 | static union node *pipeline(void); |
| 10315 | static union node *parse_command(void); |
| 10316 | static void parseheredoc(void); |
| 10317 | static char peektoken(void); |
| 10318 | static int readtoken(void); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10319 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10320 | static union node * |
| 10321 | list(int nlflag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10322 | { |
| 10323 | union node *n1, *n2, *n3; |
| 10324 | int tok; |
| 10325 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10326 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10327 | if (nlflag == 2 && peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10328 | return NULL; |
| 10329 | n1 = NULL; |
| 10330 | for (;;) { |
| 10331 | n2 = andor(); |
| 10332 | tok = readtoken(); |
| 10333 | if (tok == TBACKGND) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10334 | if (n2->type == NPIPE) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10335 | n2->npipe.pipe_backgnd = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10336 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10337 | if (n2->type != NREDIR) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10338 | n3 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10339 | n3->nredir.n = n2; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10340 | /*n3->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10341 | n2 = n3; |
| 10342 | } |
| 10343 | n2->type = NBACKGND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10344 | } |
| 10345 | } |
| 10346 | if (n1 == NULL) { |
| 10347 | n1 = n2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10348 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10349 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10350 | n3->type = NSEMI; |
| 10351 | n3->nbinary.ch1 = n1; |
| 10352 | n3->nbinary.ch2 = n2; |
| 10353 | n1 = n3; |
| 10354 | } |
| 10355 | switch (tok) { |
| 10356 | case TBACKGND: |
| 10357 | case TSEMI: |
| 10358 | tok = readtoken(); |
| 10359 | /* fall through */ |
| 10360 | case TNL: |
| 10361 | if (tok == TNL) { |
| 10362 | parseheredoc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10363 | if (nlflag == 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10364 | return n1; |
| 10365 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10366 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10367 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10368 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10369 | if (peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10370 | return n1; |
| 10371 | break; |
| 10372 | case TEOF: |
| 10373 | if (heredoclist) |
| 10374 | parseheredoc(); |
| 10375 | else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10376 | pungetc(); /* push back EOF on input */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10377 | return n1; |
| 10378 | default: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10379 | if (nlflag == 1) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10380 | raise_error_unexpected_syntax(-1); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10381 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10382 | return n1; |
| 10383 | } |
| 10384 | } |
| 10385 | } |
| 10386 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10387 | static union node * |
| 10388 | andor(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10389 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10390 | union node *n1, *n2, *n3; |
| 10391 | int t; |
| 10392 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10393 | n1 = pipeline(); |
| 10394 | for (;;) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10395 | t = readtoken(); |
| 10396 | if (t == TAND) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10397 | t = NAND; |
| 10398 | } else if (t == TOR) { |
| 10399 | t = NOR; |
| 10400 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10401 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10402 | return n1; |
| 10403 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10404 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10405 | n2 = pipeline(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10406 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10407 | n3->type = t; |
| 10408 | n3->nbinary.ch1 = n1; |
| 10409 | n3->nbinary.ch2 = n2; |
| 10410 | n1 = n3; |
| 10411 | } |
| 10412 | } |
| 10413 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10414 | static union node * |
| 10415 | pipeline(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10416 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10417 | union node *n1, *n2, *pipenode; |
| 10418 | struct nodelist *lp, *prev; |
| 10419 | int negate; |
| 10420 | |
| 10421 | negate = 0; |
| 10422 | TRACE(("pipeline: entered\n")); |
| 10423 | if (readtoken() == TNOT) { |
| 10424 | negate = !negate; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10425 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10426 | } else |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10427 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10428 | n1 = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10429 | if (readtoken() == TPIPE) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10430 | pipenode = stzalloc(sizeof(struct npipe)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10431 | pipenode->type = NPIPE; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10432 | /*pipenode->npipe.pipe_backgnd = 0; - stzalloc did it */ |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10433 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10434 | pipenode->npipe.cmdlist = lp; |
| 10435 | lp->n = n1; |
| 10436 | do { |
| 10437 | prev = lp; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10438 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10439 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10440 | lp->n = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10441 | prev->next = lp; |
| 10442 | } while (readtoken() == TPIPE); |
| 10443 | lp->next = NULL; |
| 10444 | n1 = pipenode; |
| 10445 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10446 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10447 | if (negate) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10448 | n2 = stzalloc(sizeof(struct nnot)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10449 | n2->type = NNOT; |
| 10450 | n2->nnot.com = n1; |
| 10451 | return n2; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10452 | } |
| 10453 | return n1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10454 | } |
| 10455 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10456 | static union node * |
| 10457 | makename(void) |
| 10458 | { |
| 10459 | union node *n; |
| 10460 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10461 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10462 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10463 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10464 | n->narg.text = wordtext; |
| 10465 | n->narg.backquote = backquotelist; |
| 10466 | return n; |
| 10467 | } |
| 10468 | |
| 10469 | static void |
| 10470 | fixredir(union node *n, const char *text, int err) |
| 10471 | { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10472 | int fd; |
| 10473 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10474 | TRACE(("Fix redir %s %d\n", text, err)); |
| 10475 | if (!err) |
| 10476 | n->ndup.vname = NULL; |
| 10477 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10478 | fd = bb_strtou(text, NULL, 10); |
| 10479 | if (!errno && fd >= 0) |
| 10480 | n->ndup.dupfd = fd; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10481 | else if (LONE_DASH(text)) |
| 10482 | n->ndup.dupfd = -1; |
| 10483 | else { |
| 10484 | if (err) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10485 | raise_error_syntax("bad fd number"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10486 | n->ndup.vname = makename(); |
| 10487 | } |
| 10488 | } |
| 10489 | |
| 10490 | /* |
| 10491 | * Returns true if the text contains nothing to expand (no dollar signs |
| 10492 | * or backquotes). |
| 10493 | */ |
| 10494 | static int |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 10495 | noexpand(const char *text) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10496 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10497 | unsigned char c; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10498 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10499 | while ((c = *text++) != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10500 | if (c == CTLQUOTEMARK) |
| 10501 | continue; |
| 10502 | if (c == CTLESC) |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10503 | text++; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 10504 | else if (SIT(c, BASESYNTAX) == CCTL) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10505 | return 0; |
| 10506 | } |
| 10507 | return 1; |
| 10508 | } |
| 10509 | |
| 10510 | static void |
| 10511 | parsefname(void) |
| 10512 | { |
| 10513 | union node *n = redirnode; |
| 10514 | |
| 10515 | if (readtoken() != TWORD) |
| 10516 | raise_error_unexpected_syntax(-1); |
| 10517 | if (n->type == NHERE) { |
| 10518 | struct heredoc *here = heredoc; |
| 10519 | struct heredoc *p; |
| 10520 | int i; |
| 10521 | |
| 10522 | if (quoteflag == 0) |
| 10523 | n->type = NXHERE; |
| 10524 | TRACE(("Here document %d\n", n->type)); |
| 10525 | if (!noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10526 | raise_error_syntax("illegal eof marker for << redirection"); |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 10527 | rmescapes(wordtext, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10528 | here->eofmark = wordtext; |
| 10529 | here->next = NULL; |
| 10530 | if (heredoclist == NULL) |
| 10531 | heredoclist = here; |
| 10532 | else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10533 | for (p = heredoclist; p->next; p = p->next) |
| 10534 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10535 | p->next = here; |
| 10536 | } |
| 10537 | } else if (n->type == NTOFD || n->type == NFROMFD) { |
| 10538 | fixredir(n, wordtext, 0); |
| 10539 | } else { |
| 10540 | n->nfile.fname = makename(); |
| 10541 | } |
| 10542 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10543 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10544 | static union node * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10545 | simplecmd(void) |
| 10546 | { |
| 10547 | union node *args, **app; |
| 10548 | union node *n = NULL; |
| 10549 | union node *vars, **vpp; |
| 10550 | union node **rpp, *redir; |
| 10551 | int savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10552 | #if ENABLE_ASH_BASH_COMPAT |
| 10553 | smallint double_brackets_flag = 0; |
| 10554 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10555 | |
| 10556 | args = NULL; |
| 10557 | app = &args; |
| 10558 | vars = NULL; |
| 10559 | vpp = &vars; |
| 10560 | redir = NULL; |
| 10561 | rpp = &redir; |
| 10562 | |
| 10563 | savecheckkwd = CHKALIAS; |
| 10564 | for (;;) { |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10565 | int t; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10566 | checkkwd = savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10567 | t = readtoken(); |
| 10568 | switch (t) { |
| 10569 | #if ENABLE_ASH_BASH_COMPAT |
| 10570 | case TAND: /* "&&" */ |
| 10571 | case TOR: /* "||" */ |
| 10572 | if (!double_brackets_flag) { |
| 10573 | tokpushback = 1; |
| 10574 | goto out; |
| 10575 | } |
| 10576 | wordtext = (char *) (t == TAND ? "-a" : "-o"); |
| 10577 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10578 | case TWORD: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10579 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10580 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10581 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10582 | n->narg.text = wordtext; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10583 | #if ENABLE_ASH_BASH_COMPAT |
| 10584 | if (strcmp("[[", wordtext) == 0) |
| 10585 | double_brackets_flag = 1; |
| 10586 | else if (strcmp("]]", wordtext) == 0) |
| 10587 | double_brackets_flag = 0; |
| 10588 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10589 | n->narg.backquote = backquotelist; |
| 10590 | if (savecheckkwd && isassignment(wordtext)) { |
| 10591 | *vpp = n; |
| 10592 | vpp = &n->narg.next; |
| 10593 | } else { |
| 10594 | *app = n; |
| 10595 | app = &n->narg.next; |
| 10596 | savecheckkwd = 0; |
| 10597 | } |
| 10598 | break; |
| 10599 | case TREDIR: |
| 10600 | *rpp = n = redirnode; |
| 10601 | rpp = &n->nfile.next; |
| 10602 | parsefname(); /* read name of redirection file */ |
| 10603 | break; |
| 10604 | case TLP: |
| 10605 | if (args && app == &args->narg.next |
| 10606 | && !vars && !redir |
| 10607 | ) { |
| 10608 | struct builtincmd *bcmd; |
| 10609 | const char *name; |
| 10610 | |
| 10611 | /* We have a function */ |
| 10612 | if (readtoken() != TRP) |
| 10613 | raise_error_unexpected_syntax(TRP); |
| 10614 | name = n->narg.text; |
| 10615 | if (!goodname(name) |
| 10616 | || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd)) |
| 10617 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10618 | raise_error_syntax("bad function name"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10619 | } |
| 10620 | n->type = NDEFUN; |
| 10621 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10622 | n->narg.next = parse_command(); |
| 10623 | return n; |
| 10624 | } |
| 10625 | /* fall through */ |
| 10626 | default: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10627 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10628 | goto out; |
| 10629 | } |
| 10630 | } |
| 10631 | out: |
| 10632 | *app = NULL; |
| 10633 | *vpp = NULL; |
| 10634 | *rpp = NULL; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10635 | n = stzalloc(sizeof(struct ncmd)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10636 | n->type = NCMD; |
| 10637 | n->ncmd.args = args; |
| 10638 | n->ncmd.assign = vars; |
| 10639 | n->ncmd.redirect = redir; |
| 10640 | return n; |
| 10641 | } |
| 10642 | |
| 10643 | static union node * |
| 10644 | parse_command(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10645 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10646 | union node *n1, *n2; |
| 10647 | union node *ap, **app; |
| 10648 | union node *cp, **cpp; |
| 10649 | union node *redir, **rpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10650 | union node **rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10651 | int t; |
| 10652 | |
| 10653 | redir = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10654 | rpp2 = &redir; |
Eric Andersen | 88cec25 | 2001-09-06 17:35:20 +0000 | [diff] [blame] | 10655 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10656 | switch (readtoken()) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10657 | default: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10658 | raise_error_unexpected_syntax(-1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10659 | /* NOTREACHED */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10660 | case TIF: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10661 | n1 = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10662 | n1->type = NIF; |
| 10663 | n1->nif.test = list(0); |
| 10664 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10665 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10666 | n1->nif.ifpart = list(0); |
| 10667 | n2 = n1; |
| 10668 | while (readtoken() == TELIF) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10669 | n2->nif.elsepart = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10670 | n2 = n2->nif.elsepart; |
| 10671 | n2->type = NIF; |
| 10672 | n2->nif.test = list(0); |
| 10673 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10674 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10675 | n2->nif.ifpart = list(0); |
| 10676 | } |
| 10677 | if (lasttoken == TELSE) |
| 10678 | n2->nif.elsepart = list(0); |
| 10679 | else { |
| 10680 | n2->nif.elsepart = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10681 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10682 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10683 | t = TFI; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10684 | break; |
| 10685 | case TWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10686 | case TUNTIL: { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10687 | int got; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10688 | n1 = stzalloc(sizeof(struct nbinary)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10689 | n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10690 | n1->nbinary.ch1 = list(0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10691 | got = readtoken(); |
| 10692 | if (got != TDO) { |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10693 | TRACE(("expecting DO got '%s' %s\n", tokname_array[got] + 1, |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10694 | got == TWORD ? wordtext : "")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10695 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10696 | } |
| 10697 | n1->nbinary.ch2 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10698 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10699 | break; |
| 10700 | } |
| 10701 | case TFOR: |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10702 | if (readtoken() != TWORD || quoteflag || !goodname(wordtext)) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10703 | raise_error_syntax("bad for loop variable"); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10704 | n1 = stzalloc(sizeof(struct nfor)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10705 | n1->type = NFOR; |
| 10706 | n1->nfor.var = wordtext; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10707 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10708 | if (readtoken() == TIN) { |
| 10709 | app = ≈ |
| 10710 | while (readtoken() == TWORD) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10711 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10712 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10713 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10714 | n2->narg.text = wordtext; |
| 10715 | n2->narg.backquote = backquotelist; |
| 10716 | *app = n2; |
| 10717 | app = &n2->narg.next; |
| 10718 | } |
| 10719 | *app = NULL; |
| 10720 | n1->nfor.args = ap; |
| 10721 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10722 | raise_error_unexpected_syntax(-1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10723 | } else { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10724 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10725 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10726 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10727 | n2->narg.text = (char *)dolatstr; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10728 | /*n2->narg.backquote = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10729 | n1->nfor.args = n2; |
| 10730 | /* |
| 10731 | * Newline or semicolon here is optional (but note |
| 10732 | * that the original Bourne shell only allowed NL). |
| 10733 | */ |
| 10734 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10735 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10736 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10737 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10738 | if (readtoken() != TDO) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10739 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10740 | n1->nfor.body = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10741 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10742 | break; |
| 10743 | case TCASE: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10744 | n1 = stzalloc(sizeof(struct ncase)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10745 | n1->type = NCASE; |
| 10746 | if (readtoken() != TWORD) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10747 | raise_error_unexpected_syntax(TWORD); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10748 | n1->ncase.expr = n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10749 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10750 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10751 | n2->narg.text = wordtext; |
| 10752 | n2->narg.backquote = backquotelist; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10753 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10754 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10755 | } while (readtoken() == TNL); |
| 10756 | if (lasttoken != TIN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10757 | raise_error_unexpected_syntax(TIN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10758 | cpp = &n1->ncase.cases; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10759 | next_case: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10760 | checkkwd = CHKNL | CHKKWD; |
| 10761 | t = readtoken(); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10762 | while (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10763 | if (lasttoken == TLP) |
| 10764 | readtoken(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10765 | *cpp = cp = stzalloc(sizeof(struct nclist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10766 | cp->type = NCLIST; |
| 10767 | app = &cp->nclist.pattern; |
| 10768 | for (;;) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10769 | *app = ap = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10770 | ap->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10771 | /*ap->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10772 | ap->narg.text = wordtext; |
| 10773 | ap->narg.backquote = backquotelist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10774 | if (readtoken() != TPIPE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10775 | break; |
| 10776 | app = &ap->narg.next; |
| 10777 | readtoken(); |
| 10778 | } |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10779 | //ap->narg.next = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10780 | if (lasttoken != TRP) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10781 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10782 | cp->nclist.body = list(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10783 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10784 | cpp = &cp->nclist.next; |
| 10785 | |
| 10786 | checkkwd = CHKNL | CHKKWD; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10787 | t = readtoken(); |
| 10788 | if (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10789 | if (t != TENDCASE) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10790 | raise_error_unexpected_syntax(TENDCASE); |
| 10791 | goto next_case; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10792 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10793 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10794 | *cpp = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10795 | goto redir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10796 | case TLP: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10797 | n1 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10798 | n1->type = NSUBSHELL; |
| 10799 | n1->nredir.n = list(0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10800 | /*n1->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10801 | t = TRP; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10802 | break; |
| 10803 | case TBEGIN: |
| 10804 | n1 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10805 | t = TEND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10806 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10807 | case TWORD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10808 | case TREDIR: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10809 | tokpushback = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10810 | return simplecmd(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10811 | } |
| 10812 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10813 | if (readtoken() != t) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10814 | raise_error_unexpected_syntax(t); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10815 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10816 | redir: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10817 | /* Now check for redirection which may follow command */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10818 | checkkwd = CHKKWD | CHKALIAS; |
| 10819 | rpp = rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10820 | while (readtoken() == TREDIR) { |
| 10821 | *rpp = n2 = redirnode; |
| 10822 | rpp = &n2->nfile.next; |
| 10823 | parsefname(); |
| 10824 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10825 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10826 | *rpp = NULL; |
| 10827 | if (redir) { |
| 10828 | if (n1->type != NSUBSHELL) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10829 | n2 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10830 | n2->type = NREDIR; |
| 10831 | n2->nredir.n = n1; |
| 10832 | n1 = n2; |
| 10833 | } |
| 10834 | n1->nredir.redirect = redir; |
| 10835 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10836 | return n1; |
| 10837 | } |
| 10838 | |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10839 | #if ENABLE_ASH_BASH_COMPAT |
| 10840 | static int decode_dollar_squote(void) |
| 10841 | { |
| 10842 | static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567"; |
| 10843 | int c, cnt; |
| 10844 | char *p; |
| 10845 | char buf[4]; |
| 10846 | |
| 10847 | c = pgetc(); |
| 10848 | p = strchr(C_escapes, c); |
| 10849 | if (p) { |
| 10850 | buf[0] = c; |
| 10851 | p = buf; |
| 10852 | cnt = 3; |
| 10853 | if ((unsigned char)(c - '0') <= 7) { /* \ooo */ |
| 10854 | do { |
| 10855 | c = pgetc(); |
| 10856 | *++p = c; |
| 10857 | } while ((unsigned char)(c - '0') <= 7 && --cnt); |
| 10858 | pungetc(); |
| 10859 | } else if (c == 'x') { /* \xHH */ |
| 10860 | do { |
| 10861 | c = pgetc(); |
| 10862 | *++p = c; |
| 10863 | } while (isxdigit(c) && --cnt); |
| 10864 | pungetc(); |
| 10865 | if (cnt == 3) { /* \x but next char is "bad" */ |
| 10866 | c = 'x'; |
| 10867 | goto unrecognized; |
| 10868 | } |
| 10869 | } else { /* simple seq like \\ or \t */ |
| 10870 | p++; |
| 10871 | } |
| 10872 | *p = '\0'; |
| 10873 | p = buf; |
| 10874 | c = bb_process_escape_sequence((void*)&p); |
| 10875 | } else { /* unrecognized "\z": print both chars unless ' or " */ |
| 10876 | if (c != '\'' && c != '"') { |
| 10877 | unrecognized: |
| 10878 | c |= 0x100; /* "please encode \, then me" */ |
| 10879 | } |
| 10880 | } |
| 10881 | return c; |
| 10882 | } |
| 10883 | #endif |
| 10884 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10885 | /* |
| 10886 | * If eofmark is NULL, read a word or a redirection symbol. If eofmark |
| 10887 | * is not NULL, read a here document. In the latter case, eofmark is the |
| 10888 | * word which marks the end of the document and striptabs is true if |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10889 | * leading tabs should be stripped from the document. The argument c |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10890 | * is the first character of the input token or document. |
| 10891 | * |
| 10892 | * Because C does not have internal subroutines, I have simulated them |
| 10893 | * using goto's to implement the subroutine linkage. The following macros |
| 10894 | * will run code that appears at the end of readtoken1. |
| 10895 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10896 | #define CHECKEND() {goto checkend; checkend_return:;} |
| 10897 | #define PARSEREDIR() {goto parseredir; parseredir_return:;} |
| 10898 | #define PARSESUB() {goto parsesub; parsesub_return:;} |
| 10899 | #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} |
| 10900 | #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} |
| 10901 | #define PARSEARITH() {goto parsearith; parsearith_return:;} |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10902 | static int |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10903 | readtoken1(int c, int syntax, char *eofmark, int striptabs) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10904 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10905 | /* NB: syntax parameter fits into smallint */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10906 | /* c parameter is an unsigned char or PEOF or PEOA */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10907 | char *out; |
| 10908 | int len; |
| 10909 | char line[EOFMARKLEN + 1]; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10910 | struct nodelist *bqlist; |
| 10911 | smallint quotef; |
| 10912 | smallint dblquote; |
| 10913 | smallint oldstyle; |
| 10914 | smallint prevsyntax; /* syntax before arithmetic */ |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10915 | #if ENABLE_ASH_EXPAND_PRMT |
| 10916 | smallint pssyntax; /* we are expanding a prompt string */ |
| 10917 | #endif |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10918 | int varnest; /* levels of variables expansion */ |
| 10919 | int arinest; /* levels of arithmetic expansion */ |
| 10920 | int parenlevel; /* levels of parens in arithmetic */ |
| 10921 | int dqvarnest; /* levels of variables expansion within double quotes */ |
| 10922 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 10923 | IF_ASH_BASH_COMPAT(smallint bash_dollar_squote = 0;) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10924 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10925 | #if __GNUC__ |
| 10926 | /* Avoid longjmp clobbering */ |
| 10927 | (void) &out; |
| 10928 | (void) "ef; |
| 10929 | (void) &dblquote; |
| 10930 | (void) &varnest; |
| 10931 | (void) &arinest; |
| 10932 | (void) &parenlevel; |
| 10933 | (void) &dqvarnest; |
| 10934 | (void) &oldstyle; |
| 10935 | (void) &prevsyntax; |
| 10936 | (void) &syntax; |
| 10937 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10938 | startlinno = g_parsefile->linno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10939 | bqlist = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10940 | quotef = 0; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10941 | oldstyle = 0; |
| 10942 | prevsyntax = 0; |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10943 | #if ENABLE_ASH_EXPAND_PRMT |
| 10944 | pssyntax = (syntax == PSSYNTAX); |
| 10945 | if (pssyntax) |
| 10946 | syntax = DQSYNTAX; |
| 10947 | #endif |
| 10948 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10949 | varnest = 0; |
| 10950 | arinest = 0; |
| 10951 | parenlevel = 0; |
| 10952 | dqvarnest = 0; |
| 10953 | |
| 10954 | STARTSTACKSTR(out); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 10955 | loop: |
| 10956 | /* For each line, until end of word */ |
| 10957 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10958 | CHECKEND(); /* set c to PEOF if at end of here document */ |
| 10959 | for (;;) { /* until end of line or end of word */ |
| 10960 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 10961 | switch (SIT(c, syntax)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10962 | case CNL: /* '\n' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10963 | if (syntax == BASESYNTAX) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10964 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10965 | USTPUTC(c, out); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10966 | g_parsefile->linno++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10967 | if (doprompt) |
| 10968 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10969 | c = pgetc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10970 | goto loop; /* continue outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10971 | case CWORD: |
| 10972 | USTPUTC(c, out); |
| 10973 | break; |
| 10974 | case CCTL: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10975 | if (eofmark == NULL || dblquote) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10976 | USTPUTC(CTLESC, out); |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10977 | #if ENABLE_ASH_BASH_COMPAT |
| 10978 | if (c == '\\' && bash_dollar_squote) { |
| 10979 | c = decode_dollar_squote(); |
| 10980 | if (c & 0x100) { |
| 10981 | USTPUTC('\\', out); |
| 10982 | c = (unsigned char)c; |
| 10983 | } |
| 10984 | } |
| 10985 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10986 | USTPUTC(c, out); |
| 10987 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10988 | case CBACK: /* backslash */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 10989 | c = pgetc_without_PEOA(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10990 | if (c == PEOF) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10991 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10992 | USTPUTC('\\', out); |
| 10993 | pungetc(); |
| 10994 | } else if (c == '\n') { |
| 10995 | if (doprompt) |
| 10996 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10997 | } else { |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10998 | #if ENABLE_ASH_EXPAND_PRMT |
| 10999 | if (c == '$' && pssyntax) { |
| 11000 | USTPUTC(CTLESC, out); |
| 11001 | USTPUTC('\\', out); |
| 11002 | } |
| 11003 | #endif |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11004 | if (dblquote && c != '\\' |
| 11005 | && c != '`' && c != '$' |
| 11006 | && (c != '"' || eofmark != NULL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11007 | ) { |
| 11008 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11009 | USTPUTC('\\', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11010 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11011 | if (SIT(c, SQSYNTAX) == CCTL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11012 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11013 | USTPUTC(c, out); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11014 | quotef = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11015 | } |
| 11016 | break; |
| 11017 | case CSQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11018 | syntax = SQSYNTAX; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11019 | quotemark: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11020 | if (eofmark == NULL) { |
| 11021 | USTPUTC(CTLQUOTEMARK, out); |
| 11022 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11023 | break; |
| 11024 | case CDQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11025 | syntax = DQSYNTAX; |
| 11026 | dblquote = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11027 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11028 | case CENDQUOTE: |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11029 | IF_ASH_BASH_COMPAT(bash_dollar_squote = 0;) |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11030 | if (eofmark != NULL && arinest == 0 |
| 11031 | && varnest == 0 |
| 11032 | ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11033 | USTPUTC(c, out); |
| 11034 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11035 | if (dqvarnest == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11036 | syntax = BASESYNTAX; |
| 11037 | dblquote = 0; |
| 11038 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11039 | quotef = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11040 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11041 | } |
| 11042 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11043 | case CVAR: /* '$' */ |
| 11044 | PARSESUB(); /* parse substitution */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11045 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11046 | case CENDVAR: /* '}' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11047 | if (varnest > 0) { |
| 11048 | varnest--; |
| 11049 | if (dqvarnest > 0) { |
| 11050 | dqvarnest--; |
| 11051 | } |
| 11052 | USTPUTC(CTLENDVAR, out); |
| 11053 | } else { |
| 11054 | USTPUTC(c, out); |
| 11055 | } |
| 11056 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11057 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11058 | case CLP: /* '(' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11059 | parenlevel++; |
| 11060 | USTPUTC(c, out); |
| 11061 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11062 | case CRP: /* ')' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11063 | if (parenlevel > 0) { |
| 11064 | USTPUTC(c, out); |
| 11065 | --parenlevel; |
| 11066 | } else { |
| 11067 | if (pgetc() == ')') { |
| 11068 | if (--arinest == 0) { |
| 11069 | USTPUTC(CTLENDARI, out); |
| 11070 | syntax = prevsyntax; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11071 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11072 | } else |
| 11073 | USTPUTC(')', out); |
| 11074 | } else { |
| 11075 | /* |
| 11076 | * unbalanced parens |
| 11077 | * (don't 2nd guess - no error) |
| 11078 | */ |
| 11079 | pungetc(); |
| 11080 | USTPUTC(')', out); |
| 11081 | } |
| 11082 | } |
| 11083 | break; |
| 11084 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11085 | case CBQUOTE: /* '`' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11086 | PARSEBACKQOLD(); |
| 11087 | break; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11088 | case CENDFILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11089 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11090 | case CIGN: |
| 11091 | break; |
| 11092 | default: |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11093 | if (varnest == 0) { |
| 11094 | #if ENABLE_ASH_BASH_COMPAT |
| 11095 | if (c == '&') { |
| 11096 | if (pgetc() == '>') |
| 11097 | c = 0x100 + '>'; /* flag &> */ |
| 11098 | pungetc(); |
| 11099 | } |
| 11100 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11101 | goto endword; /* exit outer loop */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11102 | } |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11103 | IF_ASH_ALIAS(if (c != PEOA)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11104 | USTPUTC(c, out); |
Eric Andersen | 3102ac4 | 2001-07-06 04:26:23 +0000 | [diff] [blame] | 11105 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11106 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11107 | c = pgetc_fast(); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11108 | } /* for (;;) */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11109 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11110 | endword: |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11111 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11112 | if (syntax == ARISYNTAX) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11113 | raise_error_syntax("missing '))'"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11114 | #endif |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 11115 | if (syntax != BASESYNTAX && !parsebackquote && eofmark == NULL) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11116 | raise_error_syntax("unterminated quoted string"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11117 | if (varnest != 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11118 | startlinno = g_parsefile->linno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11119 | /* { */ |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11120 | raise_error_syntax("missing '}'"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11121 | } |
| 11122 | USTPUTC('\0', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11123 | len = out - (char *)stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11124 | out = stackblock(); |
| 11125 | if (eofmark == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11126 | if ((c == '>' || c == '<' IF_ASH_BASH_COMPAT( || c == 0x100 + '>')) |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11127 | && quotef == 0 |
| 11128 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11129 | if (isdigit_str9(out)) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11130 | PARSEREDIR(); /* passed as params: out, c */ |
| 11131 | lasttoken = TREDIR; |
| 11132 | return lasttoken; |
| 11133 | } |
| 11134 | /* else: non-number X seen, interpret it |
| 11135 | * as "NNNX>file" = "NNNX >file" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11136 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11137 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11138 | } |
| 11139 | quoteflag = quotef; |
| 11140 | backquotelist = bqlist; |
| 11141 | grabstackblock(len); |
| 11142 | wordtext = out; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11143 | lasttoken = TWORD; |
| 11144 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11145 | /* end of readtoken routine */ |
| 11146 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11147 | /* |
| 11148 | * Check to see whether we are at the end of the here document. When this |
| 11149 | * is called, c is set to the first character of the next input line. If |
| 11150 | * we are at the end of the here document, this routine sets the c to PEOF. |
| 11151 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11152 | checkend: { |
| 11153 | if (eofmark) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11154 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11155 | if (c == PEOA) |
| 11156 | c = pgetc_without_PEOA(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11157 | #endif |
| 11158 | if (striptabs) { |
| 11159 | while (c == '\t') { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11160 | c = pgetc_without_PEOA(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11161 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11162 | } |
| 11163 | if (c == *eofmark) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11164 | if (pfgets(line, sizeof(line)) != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11165 | char *p, *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11166 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11167 | p = line; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11168 | for (q = eofmark + 1; *q && *p == *q; p++, q++) |
| 11169 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11170 | if (*p == '\n' && *q == '\0') { |
| 11171 | c = PEOF; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11172 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11173 | needprompt = doprompt; |
| 11174 | } else { |
| 11175 | pushstring(line, NULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11176 | } |
| 11177 | } |
| 11178 | } |
| 11179 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11180 | goto checkend_return; |
| 11181 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11182 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11183 | /* |
| 11184 | * Parse a redirection operator. The variable "out" points to a string |
| 11185 | * specifying the fd to be redirected. The variable "c" contains the |
| 11186 | * first character of the redirection operator. |
| 11187 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11188 | parseredir: { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11189 | /* out is already checked to be a valid number or "" */ |
| 11190 | int fd = (*out == '\0' ? -1 : atoi(out)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11191 | union node *np; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11192 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11193 | np = stzalloc(sizeof(struct nfile)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11194 | if (c == '>') { |
| 11195 | np->nfile.fd = 1; |
| 11196 | c = pgetc(); |
| 11197 | if (c == '>') |
| 11198 | np->type = NAPPEND; |
| 11199 | else if (c == '|') |
| 11200 | np->type = NCLOBBER; |
| 11201 | else if (c == '&') |
| 11202 | np->type = NTOFD; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11203 | /* it also can be NTO2 (>&file), but we can't figure it out yet */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11204 | else { |
| 11205 | np->type = NTO; |
| 11206 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11207 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11208 | } |
| 11209 | #if ENABLE_ASH_BASH_COMPAT |
| 11210 | else if (c == 0x100 + '>') { /* this flags &> redirection */ |
| 11211 | np->nfile.fd = 1; |
| 11212 | pgetc(); /* this is '>', no need to check */ |
| 11213 | np->type = NTO2; |
| 11214 | } |
| 11215 | #endif |
| 11216 | else { /* c == '<' */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11217 | /*np->nfile.fd = 0; - stzalloc did it */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11218 | c = pgetc(); |
| 11219 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11220 | case '<': |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11221 | if (sizeof(struct nfile) != sizeof(struct nhere)) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11222 | np = stzalloc(sizeof(struct nhere)); |
| 11223 | /*np->nfile.fd = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11224 | } |
| 11225 | np->type = NHERE; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11226 | heredoc = stzalloc(sizeof(struct heredoc)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11227 | heredoc->here = np; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11228 | c = pgetc(); |
| 11229 | if (c == '-') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11230 | heredoc->striptabs = 1; |
| 11231 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11232 | /*heredoc->striptabs = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11233 | pungetc(); |
| 11234 | } |
| 11235 | break; |
| 11236 | |
| 11237 | case '&': |
| 11238 | np->type = NFROMFD; |
| 11239 | break; |
| 11240 | |
| 11241 | case '>': |
| 11242 | np->type = NFROMTO; |
| 11243 | break; |
| 11244 | |
| 11245 | default: |
| 11246 | np->type = NFROM; |
| 11247 | pungetc(); |
| 11248 | break; |
| 11249 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11250 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11251 | if (fd >= 0) |
| 11252 | np->nfile.fd = fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11253 | redirnode = np; |
| 11254 | goto parseredir_return; |
| 11255 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11256 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11257 | /* |
| 11258 | * Parse a substitution. At this point, we have read the dollar sign |
| 11259 | * and nothing else. |
| 11260 | */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11261 | |
| 11262 | /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise |
| 11263 | * (assuming ascii char codes, as the original implementation did) */ |
| 11264 | #define is_special(c) \ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11265 | (((unsigned)(c) - 33 < 32) \ |
| 11266 | && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11267 | parsesub: { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11268 | unsigned char subtype; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11269 | int typeloc; |
| 11270 | int flags; |
| 11271 | char *p; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11272 | static const char types[] ALIGN1 = "}-+?="; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11273 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11274 | c = pgetc(); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11275 | if (c > 255 /* PEOA or PEOF */ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11276 | || (c != '(' && c != '{' && !is_name(c) && !is_special(c)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11277 | ) { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11278 | #if ENABLE_ASH_BASH_COMPAT |
| 11279 | if (c == '\'') |
| 11280 | bash_dollar_squote = 1; |
| 11281 | else |
| 11282 | #endif |
| 11283 | USTPUTC('$', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11284 | pungetc(); |
| 11285 | } else if (c == '(') { /* $(command) or $((arith)) */ |
| 11286 | if (pgetc() == '(') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11287 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11288 | PARSEARITH(); |
| 11289 | #else |
Mike Frysinger | 98a6f56 | 2008-06-09 09:38:45 +0000 | [diff] [blame] | 11290 | raise_error_syntax("you disabled math support for $((arith)) syntax"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11291 | #endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11292 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11293 | pungetc(); |
| 11294 | PARSEBACKQNEW(); |
| 11295 | } |
| 11296 | } else { |
| 11297 | USTPUTC(CTLVAR, out); |
| 11298 | typeloc = out - (char *)stackblock(); |
| 11299 | USTPUTC(VSNORMAL, out); |
| 11300 | subtype = VSNORMAL; |
| 11301 | if (c == '{') { |
| 11302 | c = pgetc(); |
| 11303 | if (c == '#') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11304 | c = pgetc(); |
| 11305 | if (c == '}') |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11306 | c = '#'; |
| 11307 | else |
| 11308 | subtype = VSLENGTH; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11309 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11310 | subtype = 0; |
| 11311 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11312 | if (c <= 255 /* not PEOA or PEOF */ && is_name(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11313 | do { |
| 11314 | STPUTC(c, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11315 | c = pgetc(); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11316 | } while (c <= 255 /* not PEOA or PEOF */ && is_in_name(c)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11317 | } else if (isdigit(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11318 | do { |
| 11319 | STPUTC(c, out); |
| 11320 | c = pgetc(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11321 | } while (isdigit(c)); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11322 | } else if (is_special(c)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11323 | USTPUTC(c, out); |
| 11324 | c = pgetc(); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11325 | } else { |
| 11326 | badsub: |
| 11327 | raise_error_syntax("bad substitution"); |
| 11328 | } |
Cristian Ionescu-Idbohrn | 301f5ec | 2009-10-05 02:07:23 +0200 | [diff] [blame] | 11329 | if (c != '}' && subtype == VSLENGTH) |
| 11330 | goto badsub; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11331 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11332 | STPUTC('=', out); |
| 11333 | flags = 0; |
| 11334 | if (subtype == 0) { |
| 11335 | switch (c) { |
| 11336 | case ':': |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11337 | c = pgetc(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11338 | #if ENABLE_ASH_BASH_COMPAT |
| 11339 | if (c == ':' || c == '$' || isdigit(c)) { |
| 11340 | pungetc(); |
| 11341 | subtype = VSSUBSTR; |
| 11342 | break; |
| 11343 | } |
| 11344 | #endif |
| 11345 | flags = VSNUL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11346 | /*FALLTHROUGH*/ |
| 11347 | default: |
| 11348 | p = strchr(types, c); |
| 11349 | if (p == NULL) |
| 11350 | goto badsub; |
| 11351 | subtype = p - types + VSNORMAL; |
| 11352 | break; |
| 11353 | case '%': |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11354 | case '#': { |
| 11355 | int cc = c; |
| 11356 | subtype = c == '#' ? VSTRIMLEFT : VSTRIMRIGHT; |
| 11357 | c = pgetc(); |
| 11358 | if (c == cc) |
| 11359 | subtype++; |
| 11360 | else |
| 11361 | pungetc(); |
| 11362 | break; |
| 11363 | } |
| 11364 | #if ENABLE_ASH_BASH_COMPAT |
| 11365 | case '/': |
| 11366 | subtype = VSREPLACE; |
| 11367 | c = pgetc(); |
| 11368 | if (c == '/') |
| 11369 | subtype++; /* VSREPLACEALL */ |
| 11370 | else |
| 11371 | pungetc(); |
| 11372 | break; |
| 11373 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11374 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11375 | } else { |
| 11376 | pungetc(); |
| 11377 | } |
| 11378 | if (dblquote || arinest) |
| 11379 | flags |= VSQUOTE; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11380 | ((unsigned char *)stackblock())[typeloc] = subtype | flags; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11381 | if (subtype != VSNORMAL) { |
| 11382 | varnest++; |
| 11383 | if (dblquote || arinest) { |
| 11384 | dqvarnest++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11385 | } |
| 11386 | } |
| 11387 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11388 | goto parsesub_return; |
| 11389 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11390 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11391 | /* |
| 11392 | * Called to parse command substitutions. Newstyle is set if the command |
| 11393 | * is enclosed inside $(...); nlpp is a pointer to the head of the linked |
| 11394 | * list of commands (passed by reference), and savelen is the number of |
| 11395 | * characters on the top of the stack which must be preserved. |
| 11396 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11397 | parsebackq: { |
| 11398 | struct nodelist **nlpp; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11399 | smallint savepbq; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11400 | union node *n; |
| 11401 | char *volatile str; |
| 11402 | struct jmploc jmploc; |
| 11403 | struct jmploc *volatile savehandler; |
| 11404 | size_t savelen; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11405 | smallint saveprompt = 0; |
| 11406 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11407 | #ifdef __GNUC__ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11408 | (void) &saveprompt; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11409 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11410 | savepbq = parsebackquote; |
| 11411 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 11412 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11413 | parsebackquote = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11414 | exception_handler = savehandler; |
| 11415 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11416 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11417 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11418 | str = NULL; |
| 11419 | savelen = out - (char *)stackblock(); |
| 11420 | if (savelen > 0) { |
| 11421 | str = ckmalloc(savelen); |
| 11422 | memcpy(str, stackblock(), savelen); |
| 11423 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11424 | savehandler = exception_handler; |
| 11425 | exception_handler = &jmploc; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11426 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11427 | if (oldstyle) { |
| 11428 | /* We must read until the closing backquote, giving special |
| 11429 | treatment to some slashes, and then push the string and |
| 11430 | reread it as input, interpreting it normally. */ |
| 11431 | char *pout; |
| 11432 | int pc; |
| 11433 | size_t psavelen; |
| 11434 | char *pstr; |
| 11435 | |
| 11436 | |
| 11437 | STARTSTACKSTR(pout); |
| 11438 | for (;;) { |
| 11439 | if (needprompt) { |
| 11440 | setprompt(2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11441 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11442 | pc = pgetc(); |
| 11443 | switch (pc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11444 | case '`': |
| 11445 | goto done; |
| 11446 | |
| 11447 | case '\\': |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11448 | pc = pgetc(); |
| 11449 | if (pc == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11450 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11451 | if (doprompt) |
| 11452 | setprompt(2); |
| 11453 | /* |
| 11454 | * If eating a newline, avoid putting |
| 11455 | * the newline into the new character |
| 11456 | * stream (via the STPUTC after the |
| 11457 | * switch). |
| 11458 | */ |
| 11459 | continue; |
| 11460 | } |
| 11461 | if (pc != '\\' && pc != '`' && pc != '$' |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 11462 | && (!dblquote || pc != '"') |
| 11463 | ) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11464 | STPUTC('\\', pout); |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 11465 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11466 | if (pc <= 255 /* not PEOA or PEOF */) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11467 | break; |
| 11468 | } |
| 11469 | /* fall through */ |
| 11470 | |
| 11471 | case PEOF: |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11472 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11473 | startlinno = g_parsefile->linno; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11474 | raise_error_syntax("EOF in backquote substitution"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11475 | |
| 11476 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11477 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11478 | needprompt = doprompt; |
| 11479 | break; |
| 11480 | |
| 11481 | default: |
| 11482 | break; |
| 11483 | } |
| 11484 | STPUTC(pc, pout); |
| 11485 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11486 | done: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11487 | STPUTC('\0', pout); |
| 11488 | psavelen = pout - (char *)stackblock(); |
| 11489 | if (psavelen > 0) { |
| 11490 | pstr = grabstackstr(pout); |
| 11491 | setinputstring(pstr); |
| 11492 | } |
| 11493 | } |
| 11494 | nlpp = &bqlist; |
| 11495 | while (*nlpp) |
| 11496 | nlpp = &(*nlpp)->next; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11497 | *nlpp = stzalloc(sizeof(**nlpp)); |
| 11498 | /* (*nlpp)->next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11499 | parsebackquote = oldstyle; |
| 11500 | |
| 11501 | if (oldstyle) { |
| 11502 | saveprompt = doprompt; |
| 11503 | doprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11504 | } |
| 11505 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11506 | n = list(2); |
| 11507 | |
| 11508 | if (oldstyle) |
| 11509 | doprompt = saveprompt; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11510 | else if (readtoken() != TRP) |
| 11511 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11512 | |
| 11513 | (*nlpp)->n = n; |
| 11514 | if (oldstyle) { |
| 11515 | /* |
| 11516 | * Start reading from old file again, ignoring any pushed back |
| 11517 | * tokens left from the backquote parsing |
| 11518 | */ |
| 11519 | popfile(); |
| 11520 | tokpushback = 0; |
| 11521 | } |
| 11522 | while (stackblocksize() <= savelen) |
| 11523 | growstackblock(); |
| 11524 | STARTSTACKSTR(out); |
| 11525 | if (str) { |
| 11526 | memcpy(out, str, savelen); |
| 11527 | STADJUST(savelen, out); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11528 | INT_OFF; |
| 11529 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11530 | str = NULL; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11531 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11532 | } |
| 11533 | parsebackquote = savepbq; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11534 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11535 | if (arinest || dblquote) |
| 11536 | USTPUTC(CTLBACKQ | CTLQUOTE, out); |
| 11537 | else |
| 11538 | USTPUTC(CTLBACKQ, out); |
| 11539 | if (oldstyle) |
| 11540 | goto parsebackq_oldreturn; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11541 | goto parsebackq_newreturn; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11542 | } |
| 11543 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11544 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11545 | /* |
| 11546 | * Parse an arithmetic expansion (indicate start of one and set state) |
| 11547 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11548 | parsearith: { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11549 | if (++arinest == 1) { |
| 11550 | prevsyntax = syntax; |
| 11551 | syntax = ARISYNTAX; |
| 11552 | USTPUTC(CTLARI, out); |
| 11553 | if (dblquote) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11554 | USTPUTC('"', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11555 | else |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11556 | USTPUTC(' ', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11557 | } else { |
| 11558 | /* |
| 11559 | * we collapse embedded arithmetic expansion to |
| 11560 | * parenthesis, which should be equivalent |
| 11561 | */ |
| 11562 | USTPUTC('(', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11563 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11564 | goto parsearith_return; |
| 11565 | } |
| 11566 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11567 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11568 | } /* end of readtoken */ |
| 11569 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11570 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11571 | * Read the next input token. |
| 11572 | * If the token is a word, we set backquotelist to the list of cmds in |
| 11573 | * backquotes. We set quoteflag to true if any part of the word was |
| 11574 | * quoted. |
| 11575 | * If the token is TREDIR, then we set redirnode to a structure containing |
| 11576 | * the redirection. |
| 11577 | * In all cases, the variable startlinno is set to the number of the line |
| 11578 | * on which the token starts. |
| 11579 | * |
| 11580 | * [Change comment: here documents and internal procedures] |
| 11581 | * [Readtoken shouldn't have any arguments. Perhaps we should make the |
| 11582 | * word parsing code into a separate routine. In this case, readtoken |
| 11583 | * doesn't need to have any internal procedures, but parseword does. |
| 11584 | * We could also make parseoperator in essence the main routine, and |
| 11585 | * have parseword (readtoken1?) handle both words and redirection.] |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11586 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11587 | #define NEW_xxreadtoken |
| 11588 | #ifdef NEW_xxreadtoken |
| 11589 | /* singles must be first! */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11590 | static const char xxreadtoken_chars[7] ALIGN1 = { |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11591 | '\n', '(', ')', /* singles */ |
| 11592 | '&', '|', ';', /* doubles */ |
| 11593 | 0 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11594 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11595 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11596 | #define xxreadtoken_singles 3 |
| 11597 | #define xxreadtoken_doubles 3 |
| 11598 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11599 | static const char xxreadtoken_tokens[] ALIGN1 = { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11600 | TNL, TLP, TRP, /* only single occurrence allowed */ |
| 11601 | TBACKGND, TPIPE, TSEMI, /* if single occurrence */ |
| 11602 | TEOF, /* corresponds to trailing nul */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11603 | TAND, TOR, TENDCASE /* if double occurrence */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11604 | }; |
| 11605 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11606 | static int |
| 11607 | xxreadtoken(void) |
| 11608 | { |
| 11609 | int c; |
| 11610 | |
| 11611 | if (tokpushback) { |
| 11612 | tokpushback = 0; |
| 11613 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11614 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11615 | if (needprompt) { |
| 11616 | setprompt(2); |
| 11617 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11618 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11619 | for (;;) { /* until token or start of word found */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11620 | c = pgetc_fast(); |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11621 | if (c == ' ' || c == '\t' IF_ASH_ALIAS( || c == PEOA)) |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11622 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11623 | |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11624 | if (c == '#') { |
| 11625 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11626 | continue; |
| 11627 | pungetc(); |
| 11628 | } else if (c == '\\') { |
| 11629 | if (pgetc() != '\n') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11630 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11631 | break; /* return readtoken1(...) */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11632 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11633 | startlinno = ++g_parsefile->linno; |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11634 | if (doprompt) |
| 11635 | setprompt(2); |
| 11636 | } else { |
| 11637 | const char *p; |
| 11638 | |
| 11639 | p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1; |
| 11640 | if (c != PEOF) { |
| 11641 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11642 | g_parsefile->linno++; |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11643 | needprompt = doprompt; |
| 11644 | } |
| 11645 | |
| 11646 | p = strchr(xxreadtoken_chars, c); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11647 | if (p == NULL) |
| 11648 | break; /* return readtoken1(...) */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11649 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11650 | if ((int)(p - xxreadtoken_chars) >= xxreadtoken_singles) { |
| 11651 | int cc = pgetc(); |
| 11652 | if (cc == c) { /* double occurrence? */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11653 | p += xxreadtoken_doubles + 1; |
| 11654 | } else { |
| 11655 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11656 | #if ENABLE_ASH_BASH_COMPAT |
| 11657 | if (c == '&' && cc == '>') /* &> */ |
| 11658 | break; /* return readtoken1(...) */ |
| 11659 | #endif |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11660 | } |
| 11661 | } |
| 11662 | } |
| 11663 | lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars]; |
| 11664 | return lasttoken; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11665 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11666 | } /* for (;;) */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11667 | |
| 11668 | return readtoken1(c, BASESYNTAX, (char *) NULL, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11669 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11670 | #else /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11671 | #define RETURN(token) return lasttoken = token |
| 11672 | static int |
| 11673 | xxreadtoken(void) |
| 11674 | { |
| 11675 | int c; |
| 11676 | |
| 11677 | if (tokpushback) { |
| 11678 | tokpushback = 0; |
| 11679 | return lasttoken; |
| 11680 | } |
| 11681 | if (needprompt) { |
| 11682 | setprompt(2); |
| 11683 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11684 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11685 | for (;;) { /* until token or start of word found */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11686 | c = pgetc_fast(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11687 | switch (c) { |
| 11688 | case ' ': case '\t': |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11689 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11690 | continue; |
| 11691 | case '#': |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11692 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11693 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11694 | pungetc(); |
| 11695 | continue; |
| 11696 | case '\\': |
| 11697 | if (pgetc() == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11698 | startlinno = ++g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11699 | if (doprompt) |
| 11700 | setprompt(2); |
| 11701 | continue; |
| 11702 | } |
| 11703 | pungetc(); |
| 11704 | goto breakloop; |
| 11705 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11706 | g_parsefile->linno++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11707 | needprompt = doprompt; |
| 11708 | RETURN(TNL); |
| 11709 | case PEOF: |
| 11710 | RETURN(TEOF); |
| 11711 | case '&': |
| 11712 | if (pgetc() == '&') |
| 11713 | RETURN(TAND); |
| 11714 | pungetc(); |
| 11715 | RETURN(TBACKGND); |
| 11716 | case '|': |
| 11717 | if (pgetc() == '|') |
| 11718 | RETURN(TOR); |
| 11719 | pungetc(); |
| 11720 | RETURN(TPIPE); |
| 11721 | case ';': |
| 11722 | if (pgetc() == ';') |
| 11723 | RETURN(TENDCASE); |
| 11724 | pungetc(); |
| 11725 | RETURN(TSEMI); |
| 11726 | case '(': |
| 11727 | RETURN(TLP); |
| 11728 | case ')': |
| 11729 | RETURN(TRP); |
| 11730 | default: |
| 11731 | goto breakloop; |
| 11732 | } |
| 11733 | } |
| 11734 | breakloop: |
| 11735 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
| 11736 | #undef RETURN |
| 11737 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11738 | #endif /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11739 | |
| 11740 | static int |
| 11741 | readtoken(void) |
| 11742 | { |
| 11743 | int t; |
| 11744 | #if DEBUG |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11745 | smallint alreadyseen = tokpushback; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11746 | #endif |
| 11747 | |
| 11748 | #if ENABLE_ASH_ALIAS |
| 11749 | top: |
| 11750 | #endif |
| 11751 | |
| 11752 | t = xxreadtoken(); |
| 11753 | |
| 11754 | /* |
| 11755 | * eat newlines |
| 11756 | */ |
| 11757 | if (checkkwd & CHKNL) { |
| 11758 | while (t == TNL) { |
| 11759 | parseheredoc(); |
| 11760 | t = xxreadtoken(); |
| 11761 | } |
| 11762 | } |
| 11763 | |
| 11764 | if (t != TWORD || quoteflag) { |
| 11765 | goto out; |
| 11766 | } |
| 11767 | |
| 11768 | /* |
| 11769 | * check for keywords |
| 11770 | */ |
| 11771 | if (checkkwd & CHKKWD) { |
| 11772 | const char *const *pp; |
| 11773 | |
| 11774 | pp = findkwd(wordtext); |
| 11775 | if (pp) { |
| 11776 | lasttoken = t = pp - tokname_array; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11777 | TRACE(("keyword '%s' recognized\n", tokname_array[t] + 1)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11778 | goto out; |
| 11779 | } |
| 11780 | } |
| 11781 | |
| 11782 | if (checkkwd & CHKALIAS) { |
| 11783 | #if ENABLE_ASH_ALIAS |
| 11784 | struct alias *ap; |
| 11785 | ap = lookupalias(wordtext, 1); |
| 11786 | if (ap != NULL) { |
| 11787 | if (*ap->val) { |
| 11788 | pushstring(ap->val, ap); |
| 11789 | } |
| 11790 | goto top; |
| 11791 | } |
| 11792 | #endif |
| 11793 | } |
| 11794 | out: |
| 11795 | checkkwd = 0; |
| 11796 | #if DEBUG |
| 11797 | if (!alreadyseen) |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11798 | TRACE(("token '%s' %s\n", tokname_array[t] + 1, t == TWORD ? wordtext : "")); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11799 | else |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11800 | TRACE(("reread token '%s' %s\n", tokname_array[t] + 1, t == TWORD ? wordtext : "")); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11801 | #endif |
| 11802 | return t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11803 | } |
| 11804 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11805 | static char |
| 11806 | peektoken(void) |
| 11807 | { |
| 11808 | int t; |
| 11809 | |
| 11810 | t = readtoken(); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11811 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11812 | return tokname_array[t][0]; |
| 11813 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11814 | |
| 11815 | /* |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11816 | * Read and parse a command. Returns NODE_EOF on end of file. |
| 11817 | * (NULL is a valid parse tree indicating a blank line.) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11818 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11819 | static union node * |
| 11820 | parsecmd(int interact) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11821 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11822 | int t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11823 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11824 | tokpushback = 0; |
| 11825 | doprompt = interact; |
| 11826 | if (doprompt) |
| 11827 | setprompt(doprompt); |
| 11828 | needprompt = 0; |
| 11829 | t = readtoken(); |
| 11830 | if (t == TEOF) |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11831 | return NODE_EOF; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11832 | if (t == TNL) |
| 11833 | return NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11834 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11835 | return list(1); |
| 11836 | } |
| 11837 | |
| 11838 | /* |
| 11839 | * Input any here documents. |
| 11840 | */ |
| 11841 | static void |
| 11842 | parseheredoc(void) |
| 11843 | { |
| 11844 | struct heredoc *here; |
| 11845 | union node *n; |
| 11846 | |
| 11847 | here = heredoclist; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11848 | heredoclist = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11849 | |
| 11850 | while (here) { |
| 11851 | if (needprompt) { |
| 11852 | setprompt(2); |
| 11853 | } |
| 11854 | readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, |
| 11855 | here->eofmark, here->striptabs); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11856 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11857 | n->narg.type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11858 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11859 | n->narg.text = wordtext; |
| 11860 | n->narg.backquote = backquotelist; |
| 11861 | here->here->nhere.doc = n; |
| 11862 | here = here->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11863 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11864 | } |
| 11865 | |
| 11866 | |
| 11867 | /* |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 11868 | * called by editline -- any expansions to the prompt should be added here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11869 | */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11870 | #if ENABLE_ASH_EXPAND_PRMT |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11871 | static const char * |
| 11872 | expandstr(const char *ps) |
| 11873 | { |
| 11874 | union node n; |
| 11875 | |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 11876 | /* XXX Fix (char *) cast. It _is_ a bug. ps is variable's value, |
| 11877 | * and token processing _can_ alter it (delete NULs etc). */ |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11878 | setinputstring((char *)ps); |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11879 | readtoken1(pgetc(), PSSYNTAX, nullstr, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11880 | popfile(); |
| 11881 | |
| 11882 | n.narg.type = NARG; |
| 11883 | n.narg.next = NULL; |
| 11884 | n.narg.text = wordtext; |
| 11885 | n.narg.backquote = backquotelist; |
| 11886 | |
| 11887 | expandarg(&n, NULL, 0); |
| 11888 | return stackblock(); |
| 11889 | } |
| 11890 | #endif |
| 11891 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11892 | /* |
| 11893 | * Execute a command or commands contained in a string. |
| 11894 | */ |
| 11895 | static int |
| 11896 | evalstring(char *s, int mask) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11897 | { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11898 | union node *n; |
| 11899 | struct stackmark smark; |
| 11900 | int skip; |
| 11901 | |
| 11902 | setinputstring(s); |
| 11903 | setstackmark(&smark); |
| 11904 | |
| 11905 | skip = 0; |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11906 | while ((n = parsecmd(0)) != NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11907 | evaltree(n, 0); |
| 11908 | popstackmark(&smark); |
| 11909 | skip = evalskip; |
| 11910 | if (skip) |
| 11911 | break; |
| 11912 | } |
| 11913 | popfile(); |
| 11914 | |
| 11915 | skip &= mask; |
| 11916 | evalskip = skip; |
| 11917 | return skip; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11918 | } |
| 11919 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11920 | /* |
| 11921 | * The eval command. |
| 11922 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11923 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11924 | evalcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11925 | { |
| 11926 | char *p; |
| 11927 | char *concat; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11928 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11929 | if (argv[1]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11930 | p = argv[1]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11931 | argv += 2; |
| 11932 | if (argv[0]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11933 | STARTSTACKSTR(concat); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11934 | for (;;) { |
| 11935 | concat = stack_putstr(p, concat); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11936 | p = *argv++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11937 | if (p == NULL) |
| 11938 | break; |
| 11939 | STPUTC(' ', concat); |
| 11940 | } |
| 11941 | STPUTC('\0', concat); |
| 11942 | p = grabstackstr(concat); |
| 11943 | } |
| 11944 | evalstring(p, ~SKIPEVAL); |
| 11945 | |
| 11946 | } |
| 11947 | return exitstatus; |
| 11948 | } |
| 11949 | |
| 11950 | /* |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 11951 | * Read and execute commands. |
| 11952 | * "Top" is nonzero for the top level command loop; |
| 11953 | * it turns on prompting if the shell is interactive. |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11954 | */ |
| 11955 | static int |
| 11956 | cmdloop(int top) |
| 11957 | { |
| 11958 | union node *n; |
| 11959 | struct stackmark smark; |
| 11960 | int inter; |
| 11961 | int numeof = 0; |
| 11962 | |
| 11963 | TRACE(("cmdloop(%d) called\n", top)); |
| 11964 | for (;;) { |
| 11965 | int skip; |
| 11966 | |
| 11967 | setstackmark(&smark); |
| 11968 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11969 | if (doing_jobctl) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11970 | showjobs(stderr, SHOW_CHANGED); |
| 11971 | #endif |
| 11972 | inter = 0; |
| 11973 | if (iflag && top) { |
| 11974 | inter++; |
| 11975 | #if ENABLE_ASH_MAIL |
| 11976 | chkmail(); |
| 11977 | #endif |
| 11978 | } |
| 11979 | n = parsecmd(inter); |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 11980 | #if DEBUG |
| 11981 | if (DEBUG > 2 && debug && (n != NODE_EOF)) |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 11982 | showtree(n); |
Denis Vlasenko | 135cecb | 2009-04-12 00:00:57 +0000 | [diff] [blame] | 11983 | #endif |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11984 | if (n == NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11985 | if (!top || numeof >= 50) |
| 11986 | break; |
| 11987 | if (!stoppedjobs()) { |
| 11988 | if (!Iflag) |
| 11989 | break; |
| 11990 | out2str("\nUse \"exit\" to leave shell.\n"); |
| 11991 | } |
| 11992 | numeof++; |
| 11993 | } else if (nflag == 0) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 11994 | /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */ |
| 11995 | job_warning >>= 1; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11996 | numeof = 0; |
| 11997 | evaltree(n, 0); |
| 11998 | } |
| 11999 | popstackmark(&smark); |
| 12000 | skip = evalskip; |
| 12001 | |
| 12002 | if (skip) { |
| 12003 | evalskip = 0; |
| 12004 | return skip & SKIPEVAL; |
| 12005 | } |
| 12006 | } |
| 12007 | return 0; |
| 12008 | } |
| 12009 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12010 | /* |
| 12011 | * Take commands from a file. To be compatible we should do a path |
| 12012 | * search for the file, which is necessary to find sub-commands. |
| 12013 | */ |
| 12014 | static char * |
| 12015 | find_dot_file(char *name) |
| 12016 | { |
| 12017 | char *fullname; |
| 12018 | const char *path = pathval(); |
| 12019 | struct stat statb; |
| 12020 | |
| 12021 | /* don't try this for absolute or relative paths */ |
| 12022 | if (strchr(name, '/')) |
| 12023 | return name; |
| 12024 | |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 12025 | /* IIRC standards do not say whether . is to be searched. |
| 12026 | * And it is even smaller this way, making it unconditional for now: |
| 12027 | */ |
| 12028 | if (1) { /* ENABLE_ASH_BASH_COMPAT */ |
| 12029 | fullname = name; |
| 12030 | goto try_cur_dir; |
| 12031 | } |
| 12032 | |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12033 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 12034 | try_cur_dir: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12035 | if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { |
| 12036 | /* |
| 12037 | * Don't bother freeing here, since it will |
| 12038 | * be freed by the caller. |
| 12039 | */ |
| 12040 | return fullname; |
| 12041 | } |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12042 | if (fullname != name) |
| 12043 | stunalloc(fullname); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12044 | } |
| 12045 | |
| 12046 | /* not found in the PATH */ |
| 12047 | ash_msg_and_raise_error("%s: not found", name); |
| 12048 | /* NOTREACHED */ |
| 12049 | } |
| 12050 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12051 | static int FAST_FUNC |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12052 | dotcmd(int argc, char **argv) |
| 12053 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12054 | char *fullname; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12055 | struct strlist *sp; |
| 12056 | volatile struct shparam saveparam; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12057 | |
| 12058 | for (sp = cmdenviron; sp; sp = sp->next) |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 12059 | setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12060 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12061 | if (!argv[1]) { |
| 12062 | /* bash says: "bash: .: filename argument required" */ |
| 12063 | return 2; /* bash compat */ |
| 12064 | } |
| 12065 | |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12066 | /* "false; . empty_file; echo $?" should print 0, not 1: */ |
| 12067 | exitstatus = 0; |
| 12068 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12069 | fullname = find_dot_file(argv[1]); |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12070 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12071 | argv += 2; |
| 12072 | argc -= 2; |
| 12073 | if (argc) { /* argc > 0, argv[0] != NULL */ |
| 12074 | saveparam = shellparam; |
| 12075 | shellparam.malloced = 0; |
| 12076 | shellparam.nparam = argc; |
| 12077 | shellparam.p = argv; |
| 12078 | }; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12079 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12080 | setinputfile(fullname, INPUT_PUSH_FILE); |
| 12081 | commandname = fullname; |
| 12082 | cmdloop(0); |
| 12083 | popfile(); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12084 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12085 | if (argc) { |
| 12086 | freeparam(&shellparam); |
| 12087 | shellparam = saveparam; |
| 12088 | }; |
| 12089 | |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12090 | return exitstatus; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12091 | } |
| 12092 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12093 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12094 | exitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12095 | { |
| 12096 | if (stoppedjobs()) |
| 12097 | return 0; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12098 | if (argv[1]) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12099 | exitstatus = number(argv[1]); |
| 12100 | raise_exception(EXEXIT); |
| 12101 | /* NOTREACHED */ |
| 12102 | } |
| 12103 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12104 | /* |
| 12105 | * Read a file containing shell functions. |
| 12106 | */ |
| 12107 | static void |
| 12108 | readcmdfile(char *name) |
| 12109 | { |
| 12110 | setinputfile(name, INPUT_PUSH_FILE); |
| 12111 | cmdloop(0); |
| 12112 | popfile(); |
| 12113 | } |
| 12114 | |
| 12115 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12116 | /* ============ find_command inplementation */ |
| 12117 | |
| 12118 | /* |
| 12119 | * Resolve a command name. If you change this routine, you may have to |
| 12120 | * change the shellexec routine as well. |
| 12121 | */ |
| 12122 | static void |
| 12123 | find_command(char *name, struct cmdentry *entry, int act, const char *path) |
| 12124 | { |
| 12125 | struct tblentry *cmdp; |
| 12126 | int idx; |
| 12127 | int prev; |
| 12128 | char *fullname; |
| 12129 | struct stat statb; |
| 12130 | int e; |
| 12131 | int updatetbl; |
| 12132 | struct builtincmd *bcmd; |
| 12133 | |
| 12134 | /* If name contains a slash, don't use PATH or hash table */ |
| 12135 | if (strchr(name, '/') != NULL) { |
| 12136 | entry->u.index = -1; |
| 12137 | if (act & DO_ABS) { |
| 12138 | while (stat(name, &statb) < 0) { |
| 12139 | #ifdef SYSV |
| 12140 | if (errno == EINTR) |
| 12141 | continue; |
| 12142 | #endif |
| 12143 | entry->cmdtype = CMDUNKNOWN; |
| 12144 | return; |
| 12145 | } |
| 12146 | } |
| 12147 | entry->cmdtype = CMDNORMAL; |
| 12148 | return; |
| 12149 | } |
| 12150 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12151 | /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12152 | |
| 12153 | updatetbl = (path == pathval()); |
| 12154 | if (!updatetbl) { |
| 12155 | act |= DO_ALTPATH; |
| 12156 | if (strstr(path, "%builtin") != NULL) |
| 12157 | act |= DO_ALTBLTIN; |
| 12158 | } |
| 12159 | |
| 12160 | /* If name is in the table, check answer will be ok */ |
| 12161 | cmdp = cmdlookup(name, 0); |
| 12162 | if (cmdp != NULL) { |
| 12163 | int bit; |
| 12164 | |
| 12165 | switch (cmdp->cmdtype) { |
| 12166 | default: |
| 12167 | #if DEBUG |
| 12168 | abort(); |
| 12169 | #endif |
| 12170 | case CMDNORMAL: |
| 12171 | bit = DO_ALTPATH; |
| 12172 | break; |
| 12173 | case CMDFUNCTION: |
| 12174 | bit = DO_NOFUNC; |
| 12175 | break; |
| 12176 | case CMDBUILTIN: |
| 12177 | bit = DO_ALTBLTIN; |
| 12178 | break; |
| 12179 | } |
| 12180 | if (act & bit) { |
| 12181 | updatetbl = 0; |
| 12182 | cmdp = NULL; |
| 12183 | } else if (cmdp->rehash == 0) |
| 12184 | /* if not invalidated by cd, we're done */ |
| 12185 | goto success; |
| 12186 | } |
| 12187 | |
| 12188 | /* If %builtin not in path, check for builtin next */ |
| 12189 | bcmd = find_builtin(name); |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12190 | if (bcmd) { |
| 12191 | if (IS_BUILTIN_REGULAR(bcmd)) |
| 12192 | goto builtin_success; |
| 12193 | if (act & DO_ALTPATH) { |
| 12194 | if (!(act & DO_ALTBLTIN)) |
| 12195 | goto builtin_success; |
| 12196 | } else if (builtinloc <= 0) { |
| 12197 | goto builtin_success; |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 12198 | } |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12199 | } |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12200 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12201 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 12202 | { |
| 12203 | int applet_no = find_applet_by_name(name); |
| 12204 | if (applet_no >= 0) { |
| 12205 | entry->cmdtype = CMDNORMAL; |
| 12206 | entry->u.index = -2 - applet_no; |
| 12207 | return; |
| 12208 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12209 | } |
| 12210 | #endif |
| 12211 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12212 | /* We have to search path. */ |
| 12213 | prev = -1; /* where to start */ |
| 12214 | if (cmdp && cmdp->rehash) { /* doing a rehash */ |
| 12215 | if (cmdp->cmdtype == CMDBUILTIN) |
| 12216 | prev = builtinloc; |
| 12217 | else |
| 12218 | prev = cmdp->param.index; |
| 12219 | } |
| 12220 | |
| 12221 | e = ENOENT; |
| 12222 | idx = -1; |
| 12223 | loop: |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12224 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12225 | stunalloc(fullname); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12226 | /* NB: code below will still use fullname |
| 12227 | * despite it being "unallocated" */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12228 | idx++; |
| 12229 | if (pathopt) { |
| 12230 | if (prefix(pathopt, "builtin")) { |
| 12231 | if (bcmd) |
| 12232 | goto builtin_success; |
| 12233 | continue; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 12234 | } |
| 12235 | if ((act & DO_NOFUNC) |
| 12236 | || !prefix(pathopt, "func") |
| 12237 | ) { /* ignore unimplemented options */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12238 | continue; |
| 12239 | } |
| 12240 | } |
| 12241 | /* if rehash, don't redo absolute path names */ |
| 12242 | if (fullname[0] == '/' && idx <= prev) { |
| 12243 | if (idx < prev) |
| 12244 | continue; |
| 12245 | TRACE(("searchexec \"%s\": no change\n", name)); |
| 12246 | goto success; |
| 12247 | } |
| 12248 | while (stat(fullname, &statb) < 0) { |
| 12249 | #ifdef SYSV |
| 12250 | if (errno == EINTR) |
| 12251 | continue; |
| 12252 | #endif |
| 12253 | if (errno != ENOENT && errno != ENOTDIR) |
| 12254 | e = errno; |
| 12255 | goto loop; |
| 12256 | } |
| 12257 | e = EACCES; /* if we fail, this will be the error */ |
| 12258 | if (!S_ISREG(statb.st_mode)) |
| 12259 | continue; |
| 12260 | if (pathopt) { /* this is a %func directory */ |
| 12261 | stalloc(strlen(fullname) + 1); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12262 | /* NB: stalloc will return space pointed by fullname |
| 12263 | * (because we don't have any intervening allocations |
| 12264 | * between stunalloc above and this stalloc) */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12265 | readcmdfile(fullname); |
| 12266 | cmdp = cmdlookup(name, 0); |
| 12267 | if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION) |
| 12268 | ash_msg_and_raise_error("%s not defined in %s", name, fullname); |
| 12269 | stunalloc(fullname); |
| 12270 | goto success; |
| 12271 | } |
| 12272 | TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); |
| 12273 | if (!updatetbl) { |
| 12274 | entry->cmdtype = CMDNORMAL; |
| 12275 | entry->u.index = idx; |
| 12276 | return; |
| 12277 | } |
| 12278 | INT_OFF; |
| 12279 | cmdp = cmdlookup(name, 1); |
| 12280 | cmdp->cmdtype = CMDNORMAL; |
| 12281 | cmdp->param.index = idx; |
| 12282 | INT_ON; |
| 12283 | goto success; |
| 12284 | } |
| 12285 | |
| 12286 | /* We failed. If there was an entry for this command, delete it */ |
| 12287 | if (cmdp && updatetbl) |
| 12288 | delete_cmd_entry(); |
| 12289 | if (act & DO_ERR) |
| 12290 | ash_msg("%s: %s", name, errmsg(e, "not found")); |
| 12291 | entry->cmdtype = CMDUNKNOWN; |
| 12292 | return; |
| 12293 | |
| 12294 | builtin_success: |
| 12295 | if (!updatetbl) { |
| 12296 | entry->cmdtype = CMDBUILTIN; |
| 12297 | entry->u.cmd = bcmd; |
| 12298 | return; |
| 12299 | } |
| 12300 | INT_OFF; |
| 12301 | cmdp = cmdlookup(name, 1); |
| 12302 | cmdp->cmdtype = CMDBUILTIN; |
| 12303 | cmdp->param.cmd = bcmd; |
| 12304 | INT_ON; |
| 12305 | success: |
| 12306 | cmdp->rehash = 0; |
| 12307 | entry->cmdtype = cmdp->cmdtype; |
| 12308 | entry->u = cmdp->param; |
| 12309 | } |
| 12310 | |
| 12311 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12312 | /* ============ trap.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12313 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12314 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12315 | * The trap builtin. |
| 12316 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12317 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12318 | trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12319 | { |
| 12320 | char *action; |
| 12321 | char **ap; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12322 | int signo, exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12323 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12324 | nextopt(nullstr); |
| 12325 | ap = argptr; |
| 12326 | if (!*ap) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12327 | for (signo = 0; signo < NSIG; signo++) { |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12328 | char *tr = trap_ptr[signo]; |
| 12329 | if (tr) { |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 12330 | /* note: bash adds "SIG", but only if invoked |
| 12331 | * as "bash". If called as "sh", or if set -o posix, |
| 12332 | * then it prints short signal names. |
| 12333 | * We are printing short names: */ |
| 12334 | out1fmt("trap -- %s %s\n", |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12335 | single_quote(tr), |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12336 | get_signame(signo)); |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12337 | /* trap_ptr != trap only if we are in special-cased `trap` code. |
| 12338 | * In this case, we will exit very soon, no need to free(). */ |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 12339 | /* if (trap_ptr != trap && tp[0]) */ |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12340 | /* free(tr); */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12341 | } |
| 12342 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12343 | /* |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12344 | if (trap_ptr != trap) { |
| 12345 | free(trap_ptr); |
| 12346 | trap_ptr = trap; |
| 12347 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12348 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12349 | return 0; |
| 12350 | } |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12351 | |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12352 | action = NULL; |
| 12353 | if (ap[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12354 | action = *ap++; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12355 | exitcode = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12356 | while (*ap) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12357 | signo = get_signum(*ap); |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12358 | if (signo < 0) { |
| 12359 | /* Mimic bash message exactly */ |
| 12360 | ash_msg("%s: invalid signal specification", *ap); |
| 12361 | exitcode = 1; |
| 12362 | goto next; |
| 12363 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12364 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12365 | if (action) { |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 12366 | if (LONE_DASH(action)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12367 | action = NULL; |
| 12368 | else |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12369 | action = ckstrdup(action); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12370 | } |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 12371 | free(trap[signo]); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 12372 | if (action) |
| 12373 | may_have_traps = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12374 | trap[signo] = action; |
| 12375 | if (signo != 0) |
| 12376 | setsignal(signo); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12377 | INT_ON; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12378 | next: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12379 | ap++; |
| 12380 | } |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12381 | return exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12382 | } |
| 12383 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12384 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12385 | /* ============ Builtins */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12386 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 12387 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12388 | /* |
| 12389 | * Lists available builtins |
| 12390 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12391 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12392 | helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12393 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 12394 | unsigned col; |
| 12395 | unsigned i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12396 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 12397 | out1fmt( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 12398 | "Built-in commands:\n" |
| 12399 | "------------------\n"); |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 12400 | for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12401 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 12402 | builtintab[i].name + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12403 | if (col > 60) { |
| 12404 | out1fmt("\n"); |
| 12405 | col = 0; |
| 12406 | } |
| 12407 | } |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 12408 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 12409 | { |
| 12410 | const char *a = applet_names; |
| 12411 | while (*a) { |
| 12412 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); |
| 12413 | if (col > 60) { |
| 12414 | out1fmt("\n"); |
| 12415 | col = 0; |
| 12416 | } |
| 12417 | a += strlen(a) + 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12418 | } |
| 12419 | } |
| 12420 | #endif |
| 12421 | out1fmt("\n\n"); |
| 12422 | return EXIT_SUCCESS; |
| 12423 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12424 | #endif /* FEATURE_SH_EXTRA_QUIET */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12425 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12426 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12427 | * The export and readonly commands. |
| 12428 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12429 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12430 | exportcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12431 | { |
| 12432 | struct var *vp; |
| 12433 | char *name; |
| 12434 | const char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12435 | char **aptr; |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 12436 | int flag = argv[0][0] == 'r' ? VREADONLY : VEXPORT; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12437 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12438 | if (nextopt("p") != 'p') { |
| 12439 | aptr = argptr; |
| 12440 | name = *aptr; |
| 12441 | if (name) { |
| 12442 | do { |
| 12443 | p = strchr(name, '='); |
| 12444 | if (p != NULL) { |
| 12445 | p++; |
| 12446 | } else { |
| 12447 | vp = *findvar(hashvar(name), name); |
| 12448 | if (vp) { |
| 12449 | vp->flags |= flag; |
| 12450 | continue; |
| 12451 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12452 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12453 | setvar(name, p, flag); |
| 12454 | } while ((name = *++aptr) != NULL); |
| 12455 | return 0; |
| 12456 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12457 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12458 | showvars(argv[0], flag, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12459 | return 0; |
| 12460 | } |
| 12461 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12462 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12463 | * Delete a function if it exists. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12464 | */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12465 | static void |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12466 | unsetfunc(const char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12467 | { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12468 | struct tblentry *cmdp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12469 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12470 | cmdp = cmdlookup(name, 0); |
| 12471 | if (cmdp!= NULL && cmdp->cmdtype == CMDFUNCTION) |
| 12472 | delete_cmd_entry(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12473 | } |
| 12474 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12475 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12476 | * The unset builtin command. We unset the function before we unset the |
| 12477 | * variable to allow a function to be unset when there is a readonly variable |
| 12478 | * with the same name. |
| 12479 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12480 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12481 | unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12482 | { |
| 12483 | char **ap; |
| 12484 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12485 | int flag = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12486 | int ret = 0; |
| 12487 | |
| 12488 | while ((i = nextopt("vf")) != '\0') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12489 | flag = i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12490 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12491 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12492 | for (ap = argptr; *ap; ap++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12493 | if (flag != 'f') { |
| 12494 | i = unsetvar(*ap); |
| 12495 | ret |= i; |
| 12496 | if (!(i & 2)) |
| 12497 | continue; |
| 12498 | } |
| 12499 | if (flag != 'v') |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12500 | unsetfunc(*ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12501 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12502 | return ret & 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12503 | } |
| 12504 | |
| 12505 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 12506 | /* setmode.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12507 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12508 | #include <sys/times.h> |
| 12509 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12510 | static const unsigned char timescmd_str[] ALIGN1 = { |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12511 | ' ', offsetof(struct tms, tms_utime), |
| 12512 | '\n', offsetof(struct tms, tms_stime), |
| 12513 | ' ', offsetof(struct tms, tms_cutime), |
| 12514 | '\n', offsetof(struct tms, tms_cstime), |
| 12515 | 0 |
| 12516 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12517 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12518 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12519 | timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12520 | { |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12521 | long clk_tck, s, t; |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12522 | const unsigned char *p; |
| 12523 | struct tms buf; |
| 12524 | |
| 12525 | clk_tck = sysconf(_SC_CLK_TCK); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12526 | times(&buf); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12527 | |
| 12528 | p = timescmd_str; |
| 12529 | do { |
| 12530 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 12531 | s = t / clk_tck; |
| 12532 | out1fmt("%ldm%ld.%.3lds%c", |
| 12533 | s/60, s%60, |
| 12534 | ((t - s * clk_tck) * 1000) / clk_tck, |
| 12535 | p[0]); |
| 12536 | } while (*(p += 2)); |
| 12537 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12538 | return 0; |
| 12539 | } |
| 12540 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12541 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12542 | /* |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12543 | * The let builtin. partial stolen from GNU Bash, the Bourne Again SHell. |
| 12544 | * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12545 | * |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12546 | * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12547 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12548 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12549 | letcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12550 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12551 | arith_t i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12552 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12553 | argv++; |
| 12554 | if (!*argv) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12555 | ash_msg_and_raise_error("expression expected"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12556 | do { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12557 | i = ash_arith(*argv); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12558 | } while (*++argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12559 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 12560 | return !i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12561 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12562 | #endif /* SH_MATH_SUPPORT */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12563 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12564 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12565 | /* ============ miscbltin.c |
| 12566 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 12567 | * Miscellaneous builtins. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12568 | */ |
| 12569 | |
| 12570 | #undef rflag |
| 12571 | |
Denis Vlasenko | 83e5d6f | 2006-12-18 21:49:06 +0000 | [diff] [blame] | 12572 | #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 1 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12573 | typedef enum __rlimit_resource rlim_t; |
| 12574 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 12575 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12576 | /* |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12577 | * The read builtin. Options: |
| 12578 | * -r Do not interpret '\' specially |
| 12579 | * -s Turn off echo (tty only) |
| 12580 | * -n NCHARS Read NCHARS max |
| 12581 | * -p PROMPT Display PROMPT on stderr (if input is from tty) |
| 12582 | * -t SECONDS Timeout after SECONDS (tty or pipe only) |
| 12583 | * -u FD Read from given FD instead of fd 0 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12584 | * This uses unbuffered input, which may be avoidable in some cases. |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12585 | * TODO: bash also has: |
| 12586 | * -a ARRAY Read into array[0],[1],etc |
| 12587 | * -d DELIM End on DELIM char, not newline |
| 12588 | * -e Use line editing (tty only) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12589 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12590 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12591 | readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12592 | { |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12593 | char *opt_n = NULL; |
| 12594 | char *opt_p = NULL; |
| 12595 | char *opt_t = NULL; |
| 12596 | char *opt_u = NULL; |
| 12597 | int read_flags = 0; |
| 12598 | const char *r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12599 | int i; |
| 12600 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12601 | while ((i = nextopt("p:u:rt:n:s")) != '\0') { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 12602 | switch (i) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12603 | case 'p': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12604 | opt_p = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12605 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12606 | case 'n': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12607 | opt_n = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12608 | break; |
| 12609 | case 's': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12610 | read_flags |= BUILTIN_READ_SILENT; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12611 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12612 | case 't': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12613 | opt_t = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12614 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12615 | case 'r': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12616 | read_flags |= BUILTIN_READ_RAW; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12617 | break; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12618 | case 'u': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12619 | opt_u = optionarg; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12620 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12621 | default: |
| 12622 | break; |
| 12623 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12624 | } |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12625 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 12626 | r = shell_builtin_read(setvar2, |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12627 | argptr, |
| 12628 | bltinlookup("IFS"), /* can be NULL */ |
| 12629 | read_flags, |
| 12630 | opt_n, |
| 12631 | opt_p, |
| 12632 | opt_t, |
| 12633 | opt_u |
| 12634 | ); |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12635 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12636 | if ((uintptr_t)r > 1) |
| 12637 | ash_msg_and_raise_error(r); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12638 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12639 | return (uintptr_t)r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12640 | } |
| 12641 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12642 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12643 | umaskcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12644 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12645 | static const char permuser[3] ALIGN1 = "ugo"; |
| 12646 | static const char permmode[3] ALIGN1 = "rwx"; |
| 12647 | static const short permmask[] ALIGN2 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12648 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 12649 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 12650 | S_IROTH, S_IWOTH, S_IXOTH |
| 12651 | }; |
| 12652 | |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 12653 | /* TODO: use bb_parse_mode() instead */ |
| 12654 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12655 | char *ap; |
| 12656 | mode_t mask; |
| 12657 | int i; |
| 12658 | int symbolic_mode = 0; |
| 12659 | |
| 12660 | while (nextopt("S") != '\0') { |
| 12661 | symbolic_mode = 1; |
| 12662 | } |
| 12663 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12664 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12665 | mask = umask(0); |
| 12666 | umask(mask); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12667 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12668 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12669 | ap = *argptr; |
| 12670 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12671 | if (symbolic_mode) { |
| 12672 | char buf[18]; |
| 12673 | char *p = buf; |
| 12674 | |
| 12675 | for (i = 0; i < 3; i++) { |
| 12676 | int j; |
| 12677 | |
| 12678 | *p++ = permuser[i]; |
| 12679 | *p++ = '='; |
| 12680 | for (j = 0; j < 3; j++) { |
| 12681 | if ((mask & permmask[3 * i + j]) == 0) { |
| 12682 | *p++ = permmode[j]; |
| 12683 | } |
| 12684 | } |
| 12685 | *p++ = ','; |
| 12686 | } |
| 12687 | *--p = 0; |
| 12688 | puts(buf); |
| 12689 | } else { |
| 12690 | out1fmt("%.4o\n", mask); |
| 12691 | } |
| 12692 | } else { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12693 | if (isdigit((unsigned char) *ap)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12694 | mask = 0; |
| 12695 | do { |
| 12696 | if (*ap >= '8' || *ap < '0') |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 12697 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12698 | mask = (mask << 3) + (*ap - '0'); |
| 12699 | } while (*++ap != '\0'); |
| 12700 | umask(mask); |
| 12701 | } else { |
| 12702 | mask = ~mask & 0777; |
| 12703 | if (!bb_parse_mode(ap, &mask)) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 12704 | ash_msg_and_raise_error("illegal mode: %s", ap); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12705 | } |
| 12706 | umask(~mask & 0777); |
| 12707 | } |
| 12708 | } |
| 12709 | return 0; |
| 12710 | } |
| 12711 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12712 | static int FAST_FUNC |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 12713 | ulimitcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12714 | { |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 12715 | return shell_builtin_ulimit(argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12716 | } |
| 12717 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12718 | /* ============ main() and helpers */ |
| 12719 | |
| 12720 | /* |
| 12721 | * Called to exit the shell. |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12722 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12723 | static void exitshell(void) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12724 | static void |
| 12725 | exitshell(void) |
| 12726 | { |
| 12727 | struct jmploc loc; |
| 12728 | char *p; |
| 12729 | int status; |
| 12730 | |
| 12731 | status = exitstatus; |
| 12732 | TRACE(("pid %d, exitshell(%d)\n", getpid(), status)); |
| 12733 | if (setjmp(loc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12734 | if (exception_type == EXEXIT) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12735 | /* dash bug: it just does _exit(exitstatus) here |
| 12736 | * but we have to do setjobctl(0) first! |
| 12737 | * (bug is still not fixed in dash-0.5.3 - if you run dash |
| 12738 | * under Midnight Commander, on exit from dash MC is backgrounded) */ |
| 12739 | status = exitstatus; |
| 12740 | goto out; |
| 12741 | } |
| 12742 | exception_handler = &loc; |
| 12743 | p = trap[0]; |
| 12744 | if (p) { |
| 12745 | trap[0] = NULL; |
| 12746 | evalstring(p, 0); |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 12747 | free(p); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12748 | } |
| 12749 | flush_stdout_stderr(); |
| 12750 | out: |
| 12751 | setjobctl(0); |
| 12752 | _exit(status); |
| 12753 | /* NOTREACHED */ |
| 12754 | } |
| 12755 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12756 | static void |
| 12757 | init(void) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12758 | { |
| 12759 | /* from input.c: */ |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 12760 | /* we will never free this */ |
| 12761 | basepf.next_to_pgetc = basepf.buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12762 | |
| 12763 | /* from trap.c: */ |
| 12764 | signal(SIGCHLD, SIG_DFL); |
Denys Vlasenko | 7a7b034 | 2009-12-04 04:18:31 +0100 | [diff] [blame] | 12765 | /* bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 12766 | * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$" |
| 12767 | */ |
| 12768 | signal(SIGHUP, SIG_DFL); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12769 | |
| 12770 | /* from var.c: */ |
| 12771 | { |
| 12772 | char **envp; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12773 | const char *p; |
| 12774 | struct stat st1, st2; |
| 12775 | |
| 12776 | initvar(); |
| 12777 | for (envp = environ; envp && *envp; envp++) { |
| 12778 | if (strchr(*envp, '=')) { |
| 12779 | setvareq(*envp, VEXPORT|VTEXTFIXED); |
| 12780 | } |
| 12781 | } |
| 12782 | |
Denys Vlasenko | 7bb346f | 2009-10-06 22:09:50 +0200 | [diff] [blame] | 12783 | setvar("PPID", utoa(getppid()), 0); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12784 | |
| 12785 | p = lookupvar("PWD"); |
| 12786 | if (p) |
| 12787 | if (*p != '/' || stat(p, &st1) || stat(".", &st2) |
| 12788 | || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) |
| 12789 | p = '\0'; |
| 12790 | setpwd(p, 0); |
| 12791 | } |
| 12792 | } |
| 12793 | |
| 12794 | /* |
| 12795 | * Process the shell command line arguments. |
| 12796 | */ |
| 12797 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12798 | procargs(char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12799 | { |
| 12800 | int i; |
| 12801 | const char *xminusc; |
| 12802 | char **xargv; |
| 12803 | |
| 12804 | xargv = argv; |
| 12805 | arg0 = xargv[0]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12806 | /* if (xargv[0]) - mmm, this is always true! */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12807 | xargv++; |
| 12808 | for (i = 0; i < NOPTS; i++) |
| 12809 | optlist[i] = 2; |
| 12810 | argptr = xargv; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 12811 | if (options(1)) { |
| 12812 | /* it already printed err message */ |
| 12813 | raise_exception(EXERROR); |
| 12814 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12815 | xargv = argptr; |
| 12816 | xminusc = minusc; |
| 12817 | if (*xargv == NULL) { |
| 12818 | if (xminusc) |
| 12819 | ash_msg_and_raise_error(bb_msg_requires_arg, "-c"); |
| 12820 | sflag = 1; |
| 12821 | } |
| 12822 | if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) |
| 12823 | iflag = 1; |
| 12824 | if (mflag == 2) |
| 12825 | mflag = iflag; |
| 12826 | for (i = 0; i < NOPTS; i++) |
| 12827 | if (optlist[i] == 2) |
| 12828 | optlist[i] = 0; |
| 12829 | #if DEBUG == 2 |
| 12830 | debug = 1; |
| 12831 | #endif |
| 12832 | /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ |
| 12833 | if (xminusc) { |
| 12834 | minusc = *xargv++; |
| 12835 | if (*xargv) |
| 12836 | goto setarg0; |
| 12837 | } else if (!sflag) { |
| 12838 | setinputfile(*xargv, 0); |
| 12839 | setarg0: |
| 12840 | arg0 = *xargv++; |
| 12841 | commandname = arg0; |
| 12842 | } |
| 12843 | |
| 12844 | shellparam.p = xargv; |
| 12845 | #if ENABLE_ASH_GETOPTS |
| 12846 | shellparam.optind = 1; |
| 12847 | shellparam.optoff = -1; |
| 12848 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12849 | /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12850 | while (*xargv) { |
| 12851 | shellparam.nparam++; |
| 12852 | xargv++; |
| 12853 | } |
| 12854 | optschanged(); |
| 12855 | } |
| 12856 | |
| 12857 | /* |
| 12858 | * Read /etc/profile or .profile. |
| 12859 | */ |
| 12860 | static void |
| 12861 | read_profile(const char *name) |
| 12862 | { |
| 12863 | int skip; |
| 12864 | |
| 12865 | if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0) |
| 12866 | return; |
| 12867 | skip = cmdloop(0); |
| 12868 | popfile(); |
| 12869 | if (skip) |
| 12870 | exitshell(); |
| 12871 | } |
| 12872 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12873 | /* |
| 12874 | * This routine is called when an error or an interrupt occurs in an |
| 12875 | * interactive shell and control is returned to the main command loop. |
| 12876 | */ |
| 12877 | static void |
| 12878 | reset(void) |
| 12879 | { |
| 12880 | /* from eval.c: */ |
| 12881 | evalskip = 0; |
| 12882 | loopnest = 0; |
| 12883 | /* from input.c: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 12884 | g_parsefile->left_in_buffer = 0; |
| 12885 | g_parsefile->left_in_line = 0; /* clear input buffer */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12886 | popallfiles(); |
| 12887 | /* from parser.c: */ |
| 12888 | tokpushback = 0; |
| 12889 | checkkwd = 0; |
| 12890 | /* from redir.c: */ |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 12891 | clearredir(/*drop:*/ 0); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12892 | } |
| 12893 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12894 | #if PROFILE |
| 12895 | static short profile_buf[16384]; |
| 12896 | extern int etext(); |
| 12897 | #endif |
| 12898 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12899 | /* |
| 12900 | * Main routine. We initialize things, parse the arguments, execute |
| 12901 | * profiles if we're a login shell, and then call cmdloop to execute |
| 12902 | * commands. The setjmp call sets up the location to jump to when an |
| 12903 | * exception occurs. When an exception occurs the variable "state" |
| 12904 | * is used to figure out how far we had gotten. |
| 12905 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 12906 | int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12907 | int ash_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12908 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12909 | const char *shinit; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 12910 | volatile smallint state; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12911 | struct jmploc jmploc; |
| 12912 | struct stackmark smark; |
| 12913 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12914 | /* Initialize global data */ |
| 12915 | INIT_G_misc(); |
| 12916 | INIT_G_memstack(); |
| 12917 | INIT_G_var(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 12918 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12919 | INIT_G_alias(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 12920 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12921 | INIT_G_cmdtable(); |
| 12922 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12923 | #if PROFILE |
| 12924 | monitor(4, etext, profile_buf, sizeof(profile_buf), 50); |
| 12925 | #endif |
| 12926 | |
| 12927 | #if ENABLE_FEATURE_EDITING |
| 12928 | line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP); |
| 12929 | #endif |
| 12930 | state = 0; |
| 12931 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12932 | smallint e; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 12933 | smallint s; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12934 | |
| 12935 | reset(); |
| 12936 | |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12937 | e = exception_type; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12938 | if (e == EXERROR) |
| 12939 | exitstatus = 2; |
| 12940 | s = state; |
| 12941 | if (e == EXEXIT || s == 0 || iflag == 0 || shlvl) |
| 12942 | exitshell(); |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12943 | if (e == EXINT) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12944 | outcslow('\n', stderr); |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12945 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12946 | popstackmark(&smark); |
| 12947 | FORCE_INT_ON; /* enable interrupts */ |
| 12948 | if (s == 1) |
| 12949 | goto state1; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 12950 | if (s == 2) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12951 | goto state2; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 12952 | if (s == 3) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12953 | goto state3; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 12954 | goto state4; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12955 | } |
| 12956 | exception_handler = &jmploc; |
| 12957 | #if DEBUG |
| 12958 | opentrace(); |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 12959 | TRACE(("Shell args: ")); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 12960 | trace_puts_args(argv); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12961 | #endif |
| 12962 | rootpid = getpid(); |
| 12963 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12964 | init(); |
| 12965 | setstackmark(&smark); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12966 | procargs(argv); |
| 12967 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12968 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
| 12969 | if (iflag) { |
| 12970 | const char *hp = lookupvar("HISTFILE"); |
| 12971 | |
| 12972 | if (hp == NULL) { |
| 12973 | hp = lookupvar("HOME"); |
| 12974 | if (hp != NULL) { |
| 12975 | char *defhp = concat_path_file(hp, ".ash_history"); |
| 12976 | setvar("HISTFILE", defhp, 0); |
| 12977 | free(defhp); |
| 12978 | } |
| 12979 | } |
| 12980 | } |
| 12981 | #endif |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 12982 | if (/* argv[0] && */ argv[0][0] == '-') |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12983 | isloginsh = 1; |
| 12984 | if (isloginsh) { |
| 12985 | state = 1; |
| 12986 | read_profile("/etc/profile"); |
| 12987 | state1: |
| 12988 | state = 2; |
| 12989 | read_profile(".profile"); |
| 12990 | } |
| 12991 | state2: |
| 12992 | state = 3; |
| 12993 | if ( |
| 12994 | #ifndef linux |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12995 | getuid() == geteuid() && getgid() == getegid() && |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12996 | #endif |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12997 | iflag |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12998 | ) { |
| 12999 | shinit = lookupvar("ENV"); |
| 13000 | if (shinit != NULL && *shinit != '\0') { |
| 13001 | read_profile(shinit); |
| 13002 | } |
| 13003 | } |
| 13004 | state3: |
| 13005 | state = 4; |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13006 | if (minusc) { |
| 13007 | /* evalstring pushes parsefile stack. |
| 13008 | * Ensure we don't falsely claim that 0 (stdin) |
Denis Vlasenko | 5368ad5 | 2009-03-20 10:20:08 +0000 | [diff] [blame] | 13009 | * is one of stacked source fds. |
| 13010 | * Testcase: ash -c 'exec 1>&0' must not complain. */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 13011 | // if (!sflag) g_parsefile->pf_fd = -1; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 13012 | // ^^ not necessary since now we special-case fd 0 |
| 13013 | // in is_hidden_fd() to not be considered "hidden fd" |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13014 | evalstring(minusc, 0); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13015 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13016 | |
| 13017 | if (sflag || minusc == NULL) { |
Denys Vlasenko | 0337e03 | 2009-11-29 00:12:30 +0100 | [diff] [blame] | 13018 | #if defined MAX_HISTORY && MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 13019 | if (iflag) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13020 | const char *hp = lookupvar("HISTFILE"); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13021 | if (hp) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13022 | line_input_state->hist_file = hp; |
| 13023 | } |
| 13024 | #endif |
| 13025 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
| 13026 | cmdloop(1); |
| 13027 | } |
| 13028 | #if PROFILE |
| 13029 | monitor(0); |
| 13030 | #endif |
| 13031 | #ifdef GPROF |
| 13032 | { |
| 13033 | extern void _mcleanup(void); |
| 13034 | _mcleanup(); |
| 13035 | } |
| 13036 | #endif |
| 13037 | exitshell(); |
| 13038 | /* NOTREACHED */ |
| 13039 | } |
| 13040 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13041 | |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13042 | /*- |
| 13043 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 13044 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13045 | * |
| 13046 | * This code is derived from software contributed to Berkeley by |
| 13047 | * Kenneth Almquist. |
| 13048 | * |
| 13049 | * Redistribution and use in source and binary forms, with or without |
| 13050 | * modification, are permitted provided that the following conditions |
| 13051 | * are met: |
| 13052 | * 1. Redistributions of source code must retain the above copyright |
| 13053 | * notice, this list of conditions and the following disclaimer. |
| 13054 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13055 | * notice, this list of conditions and the following disclaimer in the |
| 13056 | * documentation and/or other materials provided with the distribution. |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13057 | * 3. Neither the name of the University nor the names of its contributors |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13058 | * may be used to endorse or promote products derived from this software |
| 13059 | * without specific prior written permission. |
| 13060 | * |
| 13061 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 13062 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 13063 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 13064 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 13065 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 13066 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 13067 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 13068 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 13069 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 13070 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 13071 | * SUCH DAMAGE. |
| 13072 | */ |